diff options
author | re4k <re4k@re4k.info> | 2013-03-23 22:07:25 +0900 |
---|---|---|
committer | re4k <re4k@re4k.info> | 2013-03-23 22:07:25 +0900 |
commit | c40c306bac93950ff83e65d05eede0e5cf54edc3 (patch) | |
tree | e3022aa78f0db7359258cd56eb23fb4b1f432263 | |
parent | 80eca202c9ae3b3a6098c1654a530b1cd46326cf (diff) | |
download | aclog-c40c306bac93950ff83e65d05eede0e5cf54edc3.tar.gz |
Implement JSON API
18 files changed, 138 insertions, 63 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 93eb126..b6320a7 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -9,11 +9,22 @@ class ApplicationController < ActionController::Base end def get_page_number(params) - if params[:page] && i = params[:page].to_i + if params[:page] + i = params[:page].to_i if i > 0 return i end end return 1 end + + def get_page_count(params) + if params[:count] + i = params[:count].to_i + if i && i > 0 && i <= 100 + return i + end + end + return Settings.page_per + end end diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb index 170f2aa..1fa42cb 100644 --- a/app/controllers/errors_controller.rb +++ b/app/controllers/errors_controller.rb @@ -1,7 +1,16 @@ class ErrorsController < ApplicationController - def error_404 - end + def render_error + @exception = env["action_dispatch.exception"] + @status = ActionDispatch::ExceptionWrapper.new(env, @exception).status_code + + respond_to do |format| + format.html do + render "error_#{@status}", :status => @status + end - def error_500 + format.json do + render "error_#{@status}", :status => @status + end + end end end diff --git a/app/controllers/i_controller.rb b/app/controllers/i_controller.rb index 7ee38da..90e1d01 100644 --- a/app/controllers/i_controller.rb +++ b/app/controllers/i_controller.rb @@ -3,7 +3,7 @@ class IController < ApplicationController @items = Tweet .reacted .order_by_reactions - .limit(Settings.page_per) + .limit(get_page_count(params)) end def recent @@ -11,6 +11,23 @@ class IController < ApplicationController .recent .reacted .order_by_reactions - .limit(Settings.page_per) + .limit(get_page_count(params)) + end + + def show + tweet_id = Integer(params[:id]) + + @item = Tweet.find(tweet_id) + @user = @item.user + helpers = ApplicationController.helpers + @title = "\"#{helpers.strip_tags(helpers.format_tweet_text(@item.text))[0...30]}\" from @#{@item.user.screen_name}" + + respond_to do |format| + format.html + + format.json do + @trim_user = params[:trim_user] == "true" + end + end end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 1ebb5a6..cc11c2e 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,23 +1,4 @@ class UsersController < ApplicationController - def show - tweet_id = Integer(params[:id]) - - @item = Tweet.find(tweet_id) - @user = @item.user - helpers = ApplicationController.helpers - @title = "\"#{helpers.strip_tags(helpers.format_tweet_text(@item.text))[0...30]}\" from @#{@item.user.screen_name}" - - respond_to do |format| - format.html do - prepare_info - end - - format.json do - @trim_user = params[:trim_user] == "true" - end - end - end - def best user = render_timeline(params) do |tweets, user| tweets.of(user).reacted.order_by_reactions @@ -47,9 +28,16 @@ class UsersController < ApplicationController end end - def my + def discovered user = render_timeline(params) do |tweets, user| - tweets.discovered(user).order_by_id + case params[:tweets] + when "favorite" + tweets.favorited_by(user).order_by_id + when "retweet" + tweets.retweeted_by(user).order_by_id + else + tweets.discovered_by(user).order_by_id + end end @title = "@#{user.screen_name}'s Recent Discoveries" end @@ -57,24 +45,27 @@ class UsersController < ApplicationController private def render_timeline(params, &g) page = get_page_number(params) + count = get_page_count(params) screen_name = params[:screen_name] + user_id = params[:user_id] @user = User.where(:screen_name => screen_name).first unless @user - raise ActiveRecord::RecordNotFound.new(screen_name) + @user = User.where(:id => user_id).first + unless @user + raise ActiveRecord::RecordNotFound.new("screen_name=#{screen_name}&user_id=#{user_id}") + end end @items = g.call(Tweet, @user) .page(page) - .per(Settings.page_per) + .per(count) respond_to do |format| - format.html do - prepare_info - end + format.html format.json do - @trim_user = params[:trim_user] == "true" + @include_user = params[:include_user] == "true" end end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 6b6311d..d20377b 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -19,7 +19,15 @@ class Tweet < ActiveRecord::Base order("COALESCE(favorites_count, 0) + COALESCE(retweets_count, 0) DESC") end - scope :discovered, -> user do + scope :favorited_by, -> user do + where("id IN (SELECT tweet_id FROM favorites WHERE user_id = ?)", user.id) + end + + scope :retweeted_by, -> user do + where("id IN (SELECT tweet_id FROM retweets WHERE user_id = ?)", user.id) + end + + scope :discovered_by, -> user do where("id IN (" + "SELECT tweet_id FROM favorites WHERE user_id = ?" + " UNION ALL " + diff --git a/app/views/users/show.html.haml b/app/views/i/show.html.haml index f56e0c0..f56e0c0 100644 --- a/app/views/users/show.html.haml +++ b/app/views/i/show.html.haml diff --git a/app/views/i/show.json.jbuilder b/app/views/i/show.json.jbuilder new file mode 100644 index 0000000..bca1b00 --- /dev/null +++ b/app/views/i/show.json.jbuilder @@ -0,0 +1,2 @@ +json.partial! "shared/tweet", :item => @item + diff --git a/app/views/main/index.html.haml b/app/views/main/index.html.haml index dfebde1..6580d77 100644 --- a/app/views/main/index.html.haml +++ b/app/views/main/index.html.haml @@ -7,8 +7,6 @@ %p %strong まだ開発途中段階のものです。うごかなかったらごめんね %p - Read/Write 要求するけど今のところ UserStreams しか使ってないです。こんど Favstar みたいにページ内から Fav/RT できるようにしたいなあと… -%p %strong 動作のテスト中ですので、途中で登録をうちきったりあまりにふぁぼられ・ふぁぼりが多いユーザーの登録を削除する可能性もあります。 %p 登録ユーザーだったら一番下に統計(?)が表示されてるんじゃないですかね @@ -21,11 +19,14 @@ recent(最新3日のbest): = link_to "/cat/recent", "/cat/recent" %div - timeline(Favstar の Recent、ただ0Fav0RTも含む): + timeline(Favstar の Recent): = link_to "/cat/timeline", "/cat/timeline" %div + timeline(ユーザータイムライン): + = link_to "/cat/timeline/all", "/cat/timeline/all" + %div discovery: - = link_to "/cat/my", "/cat/my" + = link_to "/cat/discovered(/{favorite|retweet})", "/cat/discovered" %div %div 全体のbest: @@ -36,7 +37,26 @@ %div URL 変えちゃうかもしれないです %div - API つくりたいですねハイ + API っていえるのかな…? + %div + %p + \/i/show.json?id=[tweet_id] + %p + \/users/{best|recent|timeline|discovered}.json + %p + screen_name + %p + user_id + %p + count + %p + include_user=true|false ← fav/RTしたユーザーを取得するか(false で ID のみ + %p + tweets={*tweet*|all} ← /users/timeline + %p + tweets={favorite|retweet|*all*} ← discovered + %p + page %div = link_to "@cat", "https://twitter.com/cat" diff --git a/app/views/shared/_tweet.html.haml b/app/views/shared/_tweet.html.haml index e706079..4d40f69 100644 --- a/app/views/shared/_tweet.html.haml +++ b/app/views/shared/_tweet.html.haml @@ -15,7 +15,7 @@ %span.twitter_bird = link_to image_tag("bird_gray_16.png", :alt => "Twitter"), twitter_status_url(item), :target => "_blank" %span.created_at - = link_to format_tweet_created_at(item.tweeted_at), :controller => "users", :action => "show", :id => item.id + = link_to format_tweet_created_at(item.tweeted_at), :controller => "i", :action => "show", :id => item.id %span.source = raw format_source_text(item.source) .stats diff --git a/app/views/shared/_tweet.json.jbuilder b/app/views/shared/_tweet.json.jbuilder index a6c22ae..467249a 100644 --- a/app/views/shared/_tweet.json.jbuilder +++ b/app/views/shared/_tweet.json.jbuilder @@ -1,18 +1,28 @@ json.(item, :id, :text, :source, :tweeted_at, :favorites_count, :retweets_count) json.user do |json| - json.partial! "shared/user", :user => item.user + unless @include_user + json.id item.user_id + else + json.partial! "shared/user", :user => item.user + end end -unless @trim_user - json.favorites item.favorites.order("id") do |json, favorite| - json.user do |json| +json.favorites item.favorites.order("id") do |json, favorite| + json.user do |json| + unless @include_user + json.id favorite.user_id + else json.partial! "shared/user", :user => favorite.user || User.new end end - json.retweets item.retweets.order("id") do |json, retweet| - json.id retweet.id - json.user do |json| +end +json.retweets item.retweets.order("id") do |json, retweet| + json.id retweet.id + json.user do |json| + unless @include_user + json.id retweet.user_id + else json.partial! "shared/user", :user => retweet.user || User.new end end diff --git a/app/views/shared/_user_nav.html.haml b/app/views/shared/_user_nav.html.haml index 93c7d35..f7a3c7b 100644 --- a/app/views/shared/_user_nav.html.haml +++ b/app/views/shared/_user_nav.html.haml @@ -8,4 +8,4 @@ %li = link_to "Timeline", :controller => "users", :action => "timeline", :screen_name => user.screen_name %li - = link_to "Discovered", :controller => "users", :action => "my", :screen_name => user.screen_name + = link_to "Discovered", :controller => "users", :action => "discovered", :screen_name => user.screen_name diff --git a/app/views/users/my.html.haml b/app/views/users/discovered.html.haml index 1410df9..1410df9 100644 --- a/app/views/users/my.html.haml +++ b/app/views/users/discovered.html.haml diff --git a/app/views/users/my.json.jbuilder b/app/views/users/discovered.json.jbuilder index d2637d3..d2637d3 100644 --- a/app/views/users/my.json.jbuilder +++ b/app/views/users/discovered.json.jbuilder diff --git a/app/views/users/show.json.jbuilder b/app/views/users/show.json.jbuilder deleted file mode 100644 index 049306e..0000000 --- a/app/views/users/show.json.jbuilder +++ /dev/null @@ -1,2 +0,0 @@ -json.partial! "shared/tweet", :item => @items.first - diff --git a/config/application.rb b/config/application.rb index ab8b888..68cbb89 100644 --- a/config/application.rb +++ b/config/application.rb @@ -22,6 +22,8 @@ module Aclog # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] # config.i18n.default_locale = :de - config.exceptions_app = self.routes + config.exceptions_app = -> env do + ErrorsController.action(:render_error).call(env) + end end end diff --git a/config/routes.rb b/config/routes.rb index 66a28c7..7f3b727 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,11 +1,12 @@ Aclog::Application.routes.draw do constraints = { :id => /[0-9]+/, + :user_id => /[0-9]+/, :screen_name => /[a-zA-Z0-9_]{1,20}/, :page => /[0-9]+/, + :count => /[0-9]+/, + :tweets => /(all|favorite|retweet)/ } - get "/404" => "errors#error_404" - get "/500" => "errors#error_500" root :to => "main#index" @@ -15,20 +16,26 @@ Aclog::Application.routes.draw do get "/i/best" => "i#best" get "/i/recent" => "i#recent" - get "/i/:id" => "users#show", :constraints => constraints - get "/(users)/:screen_name/status(es)/:id" => redirect("/i/%{id}") + get "/i/:id" => "i#show", :constraints => constraints + get "/i/show" => "i#show", :constraints => constraints get "/:screen_name(/:page)" => "users#best", :constraints => constraints + get "/users/best" => "users#best", :constraints => constraints get "/:screen_name/best" => redirect("/%{screen_name}") - get "/users/:screen_name" => redirect("/%{screen_name}") - get "/:screen_name/discovered(/:page)" => "users#my", :constraints => constraints - get "/:screen_name/my(/:page)" => "users#my", :constraints => constraints - get "/users/:screen_name/discovered" => redirect("/%{screen_name}/discovered") + get "/:screen_name/recent(/:page)" => "users#recent", :constraints => constraints + get "/users/recent" => "users#recent", :constraints => constraints get "/:screen_name/timeline(/:page)" => "users#timeline", :constraints => constraints - get "/:screen_name/timeline/all(/:page)" => "users#timeline", :constraints => constraints, :defaults => {:tweets => "all"} - get "/users/:screen_name/recent" => redirect("/%{screen_name}/timeline") + get "/:screen_name/timeline/:tweets(/:page)" => "users#timeline", :constraints => constraints + get "/users/timeline" => "users#timeline", :constraints => constraints - get "/:screen_name/recent(/:page)" => "users#recent", :constraints => constraints + get "/:screen_name/discovered(/:page)" => "users#discovered", :constraints => constraints + get "/:screen_name/discovered/:tweets(/:page)" => "users#discovered", :constraints => constraints + get "/users/discovered" => "users#discovered", :constraints => constraints + + get "/(users)/:screen_name/status(es)/:id" => redirect("/i/%{id}") + get "/users/:screen_name" => redirect("/%{screen_name}") + get "/users/:screen_name/discovered" => redirect("/%{screen_name}/discovered") + get "/users/:screen_name/recent" => redirect("/%{screen_name}/timeline") end diff --git a/config/unicorn.rb b/config/unicorn.rb index 27b275d..b8e045a 100644 --- a/config/unicorn.rb +++ b/config/unicorn.rb @@ -1,4 +1,4 @@ -worker_processes 4 +worker_processes 8 working_directory File.expand_path("../../", __FILE__) diff --git a/public/assets/manifest-53d4de075a772d1eda47ac6355907a22.json b/public/assets/manifest-197176d2ea77846e2a59c4ba04910ece.json index db1f481..db1f481 100644 --- a/public/assets/manifest-53d4de075a772d1eda47ac6355907a22.json +++ b/public/assets/manifest-197176d2ea77846e2a59c4ba04910ece.json |