浏览代码

Added Post model and Feed#get to fetch/parse from web

master
Adrian Short 12 年前
父节点
当前提交
41c4b0a9f7
共有 9 个文件被更改,包括 117 次插入10 次删除
  1. +1
    -0
      Gemfile
  2. +17
    -0
      Gemfile.lock
  3. +7
    -1
      app/controllers/feeds_controller.rb
  4. +45
    -4
      app/models/feed.rb
  5. +14
    -0
      app/models/post.rb
  6. +1
    -1
      app/views/feeds/_form.html.haml
  7. +6
    -4
      app/views/feeds/index.html.haml
  8. +19
    -0
      test/fixtures/posts.yml
  9. +7
    -0
      test/unit/post_test.rb

+ 1
- 0
Gemfile 查看文件

@@ -40,3 +40,4 @@ gem 'mongo_mapper'
gem 'bson_ext'
gem 'haml-rails'
gem 'mm-multi-parameter-attributes'
gem 'feedzirra'

+ 17
- 0
Gemfile.lock 查看文件

@@ -40,9 +40,20 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.3.3)
curb (0.7.18)
erubis (2.7.0)
execjs (1.4.0)
multi_json (~> 1.0)
feedzirra (0.0.31)
activesupport (>= 3.0.8)
builder (~> 3.0.0)
curb (~> 0.7.15)
i18n (>= 0.5.0)
loofah (~> 1.0.0)
nokogiri (~> 1.4.4)
rake (>= 0.9.2)
rdoc (~> 3.8)
sax-machine (~> 0.0.20)
haml (3.1.6)
haml-rails (0.3.4)
actionpack (~> 3.0)
@@ -56,6 +67,8 @@ GEM
railties (>= 3.2.0, < 5.0)
thor (~> 0.14)
json (1.7.3)
loofah (1.0.0)
nokogiri (>= 1.3.3)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
@@ -71,6 +84,7 @@ GEM
activesupport (~> 3.0)
plucky (~> 0.4.0)
multi_json (1.3.6)
nokogiri (1.4.7)
plucky (0.4.4)
mongo (~> 1.5)
polyglot (0.3.3)
@@ -104,6 +118,8 @@ GEM
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
sax-machine (0.0.20)
nokogiri (> 0.0.0)
sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
@@ -124,6 +140,7 @@ PLATFORMS
DEPENDENCIES
bson_ext
coffee-rails (~> 3.2.1)
feedzirra
haml-rails
jquery-rails
mm-multi-parameter-attributes


+ 7
- 1
app/controllers/feeds_controller.rb 查看文件

@@ -45,7 +45,7 @@ class FeedsController < ApplicationController

respond_to do |format|
if @feed.save
format.html { redirect_to'/', notice: 'Feed was successfully created.' }
format.html { redirect_to '/', notice: 'Feed was successfully created.' }
format.json { render json: @feed, status: :created, location: @feed }
else
format.html { render action: "new" }
@@ -81,4 +81,10 @@ class FeedsController < ApplicationController
format.json { head :no_content }
end
end
def fetch
@feed = Feed.find(params[:id])
@feed.get
redirect_to '/', notice: 'Feed fetched OK'
end
end

+ 45
- 4
app/models/feed.rb 查看文件

@@ -1,11 +1,52 @@
class Feed
include MongoMapper::Document

key :title, String, :default => "[New feed - hasn't been fetched yet]"
key :link, String
key :last_fetched, Time, :default => nil
key :title, String, :default => "[New feed - hasn't been fetched yet]"
key :feed_url, String # The URL of the RSS feed, not the website that owns it
key :url, String # The URL of website. Called "link" in RSS 2.0
key :description, String
key :last_fetched, Time, :default => nil
timestamps!

many :posts

validates :title, :presence => true
validates_format_of :link, :with => URI::regexp(%w(http https)), :message => "must be a valid URL"
validates_format_of :feed_url, :with => URI::regexp(%w(http https)), :message => "must be a valid URL"
after_create :get
# Fetch and parse feed contents from web
def get
Feedzirra::Feed.add_common_feed_entry_element('georss:point', :as => :point)

feed = Feedzirra::Feed.fetch_and_parse(@feed_url)

self.set(
:title => feed.title,
:url => feed.url,
:description => feed.description,
:last_fetched => Time.now
)

feed.entries.each do |e|
# puts "#{e.title} point: #{e.point}"
latlng = e.point.split(' ')
self.posts << Post.create(
:title => e.title,
:url => e.url,
:author => e.author,
:summary => e.summary,
:content => e.content,
:published => e.published,
:loc => {
:lng => latlng[1].to_f,
:lat => latlng[0].to_f
}
)
end
end
end

+ 14
- 0
app/models/post.rb 查看文件

@@ -0,0 +1,14 @@
class Post
include MongoMapper::Document

key :title, String
key :url, String
key :author, String
key :summary, String
key :content, String
key :published, Time
key :loc, Hash # { lng, lat }
timestamps!

belongs_to :feed
end

+ 1
- 1
app/views/feeds/_form.html.haml 查看文件

@@ -14,7 +14,7 @@
= f.text_field :title
.field
= f.label "URL"
= f.text_field :link, :size => 120
= f.text_field :feed_url, :size => 120
= f.submit 'Save'
-#
.field


+ 6
- 4
app/views/feeds/index.html.haml 查看文件

@@ -3,8 +3,9 @@
%table
%tr
%th Title
%th Link
%th Last fetched
%th Feed URL
%th Posts
%th Fetched
%th
%th
%th
@@ -12,7 +13,8 @@
- @feeds.each do |feed|
%tr
%td= link_to feed.title, feed
%td= link_to feed.link, feed.link
%td= link_to feed.feed_url, feed.feed_url
%td= feed.posts.size
%td
- if feed.last_fetched.nil?
never
@@ -20,7 +22,7 @@
= time_ago_in_words feed.last_fetched
ago
%td= link_to 'Edit', edit_feed_path(feed)
%td= link_to 'Destroy', feed, :confirm => 'Are you sure?', :method => :delete
%td= link_to 'Delete', feed, :confirm => 'Are you sure?', :method => :delete

#new_feed
= render 'form'

+ 19
- 0
test/fixtures/posts.yml 查看文件

@@ -0,0 +1,19 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
title: MyString
url: MyString
author: MyString
summary: MyText
content: MyText
published: 2012-06-21 12:03:03
loc:

two:
title: MyString
url: MyString
author: MyString
summary: MyText
content: MyText
published: 2012-06-21 12:03:03
loc:

+ 7
- 0
test/unit/post_test.rb 查看文件

@@ -0,0 +1,7 @@
require 'test_helper'

class PostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

正在加载...
取消
保存