diff --git a/app/controllers/feeds_controller.rb b/app/controllers/feeds_controller.rb index 1cbbd8a..b947c64 100644 --- a/app/controllers/feeds_controller.rb +++ b/app/controllers/feeds_controller.rb @@ -85,6 +85,11 @@ class FeedsController < ApplicationController def fetch @feed = Feed.find(params[:id]) @feed.get - redirect_to '/', notice: 'Feed fetched OK' + redirect_to :back, notice: 'Feed fetched OK' + end + + def fetch_all + Feed.get_all + redirect_to :back, notice: 'All feeds fetched OK' end end diff --git a/app/models/feed.rb b/app/models/feed.rb index dec2f30..4d73c86 100644 --- a/app/models/feed.rb +++ b/app/models/feed.rb @@ -5,6 +5,8 @@ class Feed 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 :guid, String # Atom id or RSS guid + key :generator, String key :last_fetched, Time, :default => nil timestamps! @@ -16,8 +18,15 @@ class Feed after_create :get # Fetch and parse feed contents from web + + def self.get_all + Feed.all.each { |f| f.get } + end + def get + puts "Fetching feed: #{@url}" Feedzirra::Feed.add_common_feed_entry_element('georss:point', :as => :point) + Feedzirra::Feed.add_common_feed_element('generator', :as => :generator) feed = Feedzirra::Feed.fetch_and_parse(@feed_url) @@ -25,26 +34,33 @@ class Feed :title => feed.title, :url => feed.url, :description => feed.description, + :generator => feed.generator, :last_fetched => Time.now ) - feed.entries.each do |e| -# puts "#{e.title} point: #{e.point}" - + feed.entries.each do |e| latlng = e.point.split(' ') - - self.posts << Post.create( + + attrs = { :title => e.title, :url => e.url, :author => e.author, :summary => e.summary, :content => e.content, :published => e.published, + :guid => e.id, :loc => { :lng => latlng[1].to_f, :lat => latlng[0].to_f } - ) + } + + if Post.where(:url => e.url).size == 0 + self.posts << Post.create(attrs) + else + Post.set({:url => e.url}, attrs) + end + end end diff --git a/app/views/feeds/index.html.haml b/app/views/feeds/index.html.haml index 944f52d..99677e0 100644 --- a/app/views/feeds/index.html.haml +++ b/app/views/feeds/index.html.haml @@ -1,5 +1,7 @@ %h1 Feeds +%p= link_to "Fetch all", :fetch_all, :class => "button" + %table %tr %th Title @@ -22,9 +24,9 @@ - else = time_ago_in_words f.last_fetched ago - %td= link_to "Fetch", fetch_feed_url(f) - %td= link_to 'Edit', edit_feed_path(f) - %td= link_to 'Delete', f, :confirm => "Delete #{f.title} and all its posts?", :method => :delete + %td= link_to "Fetch", fetch_feed_url(f), :class => "button" + %td= link_to 'Edit', edit_feed_path(f), :class => "button" + %td= link_to 'Delete', f, :confirm => "Delete #{f.title} and all its posts?", :method => :delete, :class => "button" #new_feed = render 'form' diff --git a/app/views/feeds/show.html.haml b/app/views/feeds/show.html.haml index 38d8546..ab08d4f 100644 --- a/app/views/feeds/show.html.haml +++ b/app/views/feeds/show.html.haml @@ -2,6 +2,8 @@ %p= @feed.description +%p= link_to "Fetch", fetch_feed_url(@feed), :class => "button" + %p = pluralize(@feed.posts.size, "post") diff --git a/config/routes.rb b/config/routes.rb index a87de55..5054179 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,6 +16,8 @@ Apollo::Application.routes.draw do # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase # This route can be invoked with purchase_url(:id => product.id) + match '/fetch_all' => 'feeds#fetch_all', :as => :fetch_all + # Sample resource route (maps HTTP verbs to controller actions automatically): # resources :products