--- /dev/null
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://coffeescript.org/
--- /dev/null
+// Place all the styles related to the feed controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
--- /dev/null
+class FeedController < ApplicationController
+
+ skip_before_filter :verify_authenticity_token
+
+ def index
+ # @feeds = FeedItem.find(:all, :select => 'DISTINCT feed_name')
+ @feeds = FeedItem.select(:feed_name).distinct
+ respond_to do |format|
+ format.html
+ format.rss { render :layout => false }
+ end
+ end
+
+
+ def show
+ if FeedItem::valid_feed_name?(params[:feed_name])
+ @feed_name = params[:feed_name]
+ @feed_items = FeedItem.in_feed(@feed_name)
+ respond_to do |format|
+ if @feed_items == []
+ flash[:notice] = "No items in feed #{@feed_name}"
+ format.html { redirect_to index_url }
+ format.rss { render :layout => false }
+ else
+ format.html
+ format.rss { render :layout => false }
+ end
+ end
+ else
+ respond_to do |format|
+ flash[:notice] = "Invalid feed name"
+ format.html { redirect_to index_url }
+ format.rss { head :not_found}
+ end
+ end
+ end
+
+
+ def update
+ if FeedItem::valid_feed_name?(params[:feed_name])
+ item = FeedItem.in_feed(params[:feed_name]).entitled(params[:title]).take
+ if item
+ if params[:description].empty?
+ destroy_item(item)
+ else
+ update_item(item)
+ end
+ else
+ create_item
+ end
+ else
+ respond_to do |format|
+ flash[:notice] = "Invalid feed name"
+ format.html { redirect_to index_url }
+ format.rss { head :not_found }
+ end
+ end
+ end
+
+
+ private
+
+
+ def create_item
+ item = FeedItem.new(:feed_name => params[:feed_name],
+ :title => params[:title],
+ :description => params[:description])
+ item.save!
+ flash[:notice] = "Element #{params[:title]} created"
+ respond_to do |format|
+ format.html { redirect_to feed_url(params[:feed_name]) }
+ format.rss { head :ok }
+ end
+ rescue ActiveRecord::RecordInvalid => error
+ flash[:notice] = "Element #{params[:title]} could not be created"
+ respond_to do |format|
+ format.html { redirect_to feed_url(params[:feed_name]) }
+ format.rss { head :unprocessable_entity }
+ end
+ end
+
+
+ def update_item(item)
+ if item.update_attribute(:description, params[:description])
+ flash[:notice] = "Element #{params[:title]} updated"
+ respond_to do |format|
+ format.html { redirect_to feed_url(params[:feed_name]) }
+ format.rss { head :ok }
+ end
+ else
+ flash[:notice] = "Element #{params[:title]} could not be updated"
+ respond_to do |format|
+ format.html { redirect_to feed_url(params[:feed_name]) }
+ format.rss { head :unprocessable_entity }
+ end
+ end
+ end
+
+
+ def destroy_item(item)
+ if item.destroy
+ flash[:notice] = "Element #{params[:title]} deleted"
+ respond_to do |format|
+ format.html { redirect_to feed_url(params[:feed_name]) }
+ format.rss { head :ok }
+ end
+ else
+ flash[:notice] = "Element #{params[:title]} could not be deleted"
+ respond_to do |format|
+ format.html { redirect_to feed_url(params[:feed_name]) }
+ format.rss { head :unprocessable_entity }
+ end
+ end
+ end
+
+end
--- /dev/null
+module FeedHelper
+end
class FeedItem < ActiveRecord::Base
+
+ def self.in_feed(name)
+ where('feed_name = ?', name)
+ end
+
+ def self.entitled(title)
+ where('title = ?', title)
+ end
+
validates_presence_of :feed_name, :title, :description
validates_uniqueness_of :title, :scope => :feed_name
validate :feed_name_must_be_legal
--- /dev/null
+<h1>Feeds available</h1>
+
+<% form_tag :action => 'update' do %>
+ <p>Set feed <%= text_field_tag :feed_name, '', :size => 20 %>
+ to include <%= text_field_tag :title, '', :size => 30 %>
+ containing <%= text_field_tag :description, '', :size => 50 %>
+ <%= submit_tag 'Update' %></p>
+<% end %>
+
+
+<ul>
+<% for feed in @feeds %>
+ <li><%= link_to( h(feed.feed_name), (feed_path(:feed_name => feed.feed_name))) %></li>
+<% end %>
+</ul>
\ No newline at end of file
--- /dev/null
+# index.rss.builder
+xml.instruct! :xml, :version => "1.0"
+xml.rss :version => "2.0" do
+ xml.channel do
+ xml.title "Feedcatcher"
+ xml.link index_url(:rss)
+ xml.description "Feeds available"
+
+ for feed in @feeds
+ xml.item do
+ xml.title feed.feed_name
+ xml.link feed_url(feed.feed_name, :rss)
+ xml.guid feed_url(feed.feed_name, :rss)
+ end
+ end
+ end
+end
\ No newline at end of file
--- /dev/null
+<h1>Contents of feed <%= h params[:feed_name] %></h1>
+<p><%= link_to("List of all feeds", index_path) %></p>
+
+<% form_tag :action => 'update' do %>
+ <p>Set feed <%= text_field_tag :feed_name, h(params[:feed_name]), :size => 20 %>
+ to include <%= text_field_tag :title, '', :size => 30 %>
+ containing <%= text_field_tag :description, '', :size => 50 %>
+ <%= submit_tag 'Update' %></p>
+<% end %>
+
+<dl>
+ <% for item in @feed_items %>
+ <dt><%= h item.title %></dt>
+ <dd><em><%= h item.description %></em></dd>
+ <% end %>
+</dl>
--- /dev/null
+# show.rss.builder
+xml.instruct! :xml, :version => "1.0"
+xml.rss :version => "2.0" do
+ xml.channel do
+ xml.title @feed_name
+ xml.link feed_url(@feed_name, :rss)
+ xml.description "The #{h @feed_name} feed"
+
+ for item in @feed_items
+ xml.item do
+ xml.title item.title
+ xml.description item.description
+ xml.guid item.id, :isPermaLink => 'false'
+ xml.pubDate(item.updated_at.to_s(:rfc822))
+ end
+ end
+ end
+end
<%= csrf_meta_tags %>
</head>
<body>
+ <% if flash[:notice] -%>
+ <div id="notice"><%= h flash[:notice] %></div>
+ <% end -%>
-<%= yield %>
+ <%= yield %>
</body>
</html>
# You can have the root of your site routed with "root"
# root 'welcome#index'
+ root 'feed#index'
+
+ get 'index', to: 'feed#index', as: :index
+ get ':feed_name', to: 'feed#show', as: :feed
+ post '*ignored', to: 'feed#update', as: :update
# Example of regular route:
# get 'products/:id' => 'catalog#view'
--- /dev/null
+require 'spec_helper'
+
+describe FeedController do
+
+end
--- /dev/null
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the FeedHelper. For example:
+#
+# describe FeedHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# expect(helper.concat_strings("this","that")).to eq("this that")
+# end
+# end
+# end
+describe FeedHelper do
+ pending "add some examples to (or delete) #{__FILE__}"
+end