diff --git a/Gemfile b/Gemfile index 30c7b18..77125fb 100644 --- a/Gemfile +++ b/Gemfile @@ -40,3 +40,4 @@ gem 'mongo_mapper' gem 'bson_ext' gem 'haml-rails' gem 'mm-multi-parameter-attributes' +gem 'feedzirra' diff --git a/Gemfile.lock b/Gemfile.lock index 51f008f..559c3b2 100644 --- a/Gemfile.lock +++ b/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 diff --git a/app/controllers/feeds_controller.rb b/app/controllers/feeds_controller.rb index dbcb4eb..874b3cb 100644 --- a/app/controllers/feeds_controller.rb +++ b/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 diff --git a/app/models/feed.rb b/app/models/feed.rb index 87361d3..1176fc5 100644 --- a/app/models/feed.rb +++ b/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 diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 0000000..50cf3db --- /dev/null +++ b/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 diff --git a/app/views/feeds/_form.html.haml b/app/views/feeds/_form.html.haml index 073b795..014322b 100644 --- a/app/views/feeds/_form.html.haml +++ b/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 diff --git a/app/views/feeds/index.html.haml b/app/views/feeds/index.html.haml index 243d466..60c99da 100644 --- a/app/views/feeds/index.html.haml +++ b/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' diff --git a/test/fixtures/posts.yml b/test/fixtures/posts.yml new file mode 100644 index 0000000..f1b5c8c --- /dev/null +++ b/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: diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb new file mode 100644 index 0000000..6d9d463 --- /dev/null +++ b/test/unit/post_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class PostTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end