Permalink
Browse files

Tweak API, add creation methods

  • Loading branch information...
1 parent d763663 commit 2735ff8622abfb936bb70893b1e2b2e20ae14dbc @mattyoho mattyoho committed May 15, 2012
@@ -1,34 +1,67 @@
module Api
class FeedItemsController < ApiController
- def index
- if params[:feed_id]
- feed = Feed.find_by_name!(params[:feed_id])
- @feed_items = feed.feed_items.last_first(params[:page])
-
- last_page = feed.items_page_count(10)
- next_page = [(params[:page] || 1).to_i + 1, last_page].min
+ def all
+ @feed_items = FeedItem.last_first(params[:page])
- next_url = api_feed_feed_items_url(feed, page: next_page)
- last_url = api_feed_feed_items_url(feed, page: last_page)
- link_header = construct_link_header(next_url, last_url)
- else
- @feed_items = FeedItem.last_first(params[:page])
+ last_page = FeedItem.items_page_count(10)
+ next_page = [(params[:page] || 1).to_i + 1, last_page].min
- last_page = FeedItem.items_page_count(10)
- next_page = [(params[:page] || 1).to_i + 1, last_page].min
+ next_url = api_feed_items_url(page: next_page)
+ last_url = api_feed_items_url(page: last_page)
+ link_header = construct_link_header(next_url, last_url)
- next_url = api_feed_items_url(page: next_page)
- last_url = api_feed_items_url(page: last_page)
- link_header = construct_link_header(next_url, last_url)
+ respond_to do |format|
+ headers["Link"] = link_header
+ format.json
end
+ end
+
+ def index
+ feed = Feed.find_by_name!(params[:feed_id])
+ @feed_items = feed.feed_items.last_first(params[:page])
+
+ last_page = feed.items_page_count(10)
+ next_page = [(params[:page] || 1).to_i + 1, last_page].min
+
+ next_url = api_feed_feed_items_url(feed, page: next_page)
+ last_url = api_feed_feed_items_url(feed, page: last_page)
+ link_header = construct_link_header(next_url, last_url)
respond_to do |format|
headers["Link"] = link_header
format.json
end
end
+ def create
+ feed = Feed.find_by_name!(params[:feed_id])
+ kind = params[:item].delete(:type)
+ @feed_item = feed.feed_item_of(kind).new(params[:item])
+
+ respond_to do |format|
+ if @feed_item.save
+ format.json { render status: :created, location: @feed_item }
+ else
+ format.json { render status: :unprocessable_entity }
+ end
+ end
+ end
+
+ def update
+ feed = Feed.find_by_name!(params[:feed_id])
+ kind = params[:item].delete(:type)
+ @feed_item = feed.feed_item_of(kind).new(params[:item])
+
+ respond_to do |format|
+ if @feed_item.save
+ format.json { head :no_content }
+ else
+ format.json { render status: :unprocessable_entity }
+ end
+ end
+ end
+
private
def construct_link_header(next_url, last_url)
@@ -0,0 +1,20 @@
+if @feed_item.persisted?
+ json.type @feed_item.type
+ json.image_url @feed_item.image_url if @feed_item.image_url.present?
+ json.link_url @feed_item.link_url if @feed_item.link_url.present?
+ json.text @feed_item.body
+ json.(@feed_item, :id, :created_at)
+ json.link api_feed_feed_item_url(@feed_item.feed, @feed_item, format: :json)
+ json.feed do |json|
+ json.link api_feed_url(@feed_item.feed, format: :json)
+ json.name @feed_item.feed.name
+ end
+ json.user do |json|
+ json.username @feed_item.feed.user.username
+ json.email @feed_item.feed.user.email
+ json.gravatar @feed_item.feed.user.gravatar
+ end
+else
+ json.errors(@feed_item.errors.full_messages)
+end
+
@@ -0,0 +1,20 @@
+if @feed_item.persisted?
+ json.type @feed_item.type
+ json.image_url @feed_item.image_url if @feed_item.image_url.present?
+ json.link_url @feed_item.link_url if @feed_item.link_url.present?
+ json.text @feed_item.body
+ json.(@feed_item, :id, :created_at)
+ json.link api_feed_feed_item_url(@feed_item.feed, @feed_item, format: :json)
+ json.feed do |json|
+ json.link api_feed_url(@feed_item.feed, format: :json)
+ json.name @feed_item.feed.name
+ end
+ json.user do |json|
+ json.username @feed_item.feed.user.username
+ json.email @feed_item.feed.user.email
+ json.gravatar @feed_item.feed.user.gravatar
+ end
+else
+ json.errors(@feed_item.errors.full_messages)
+end
+
View
@@ -4,17 +4,19 @@
resources :sessions, only: [:create, :destroy, :new]
resources :feeds, only: [:index, :show]
- resources :feed_items, except: [:index, :show]
+ resources :feed_items, except: [:index, :show], path: 'items'
resources :users, only: [:create, :new]
match 'signup' => 'users#new', :as => :signup
match 'login' => 'sessions#new', :as => :login
match 'logout' => 'sessions#destroy', :as => :logout
namespace :api do
- resources :feed_items, only: :index
resources :feeds, only: :show do
- resources :feed_items, only: [:index, :create, :update]
+ collection do
+ get :items
+ end
+ resources :feed_items, path: 'items', only: [:index, :create, :update]
end
end

0 comments on commit 2735ff8

Please sign in to comment.