From be75a9b70b4a5d29692a4f7802fa4b57ace5a467 Mon Sep 17 00:00:00 2001 From: DiogoAndre Date: Fri, 5 Oct 2012 15:33:22 -0300 Subject: [PATCH] Improving issue #574 implementation. This improves the implementation of issue #574 (twitter-compatible API: /statuses/show/:id.json ), including support for the trim_user option. Removed json_api/statuses_test and included tests in api/twitter_api_test, which already contained tests to the Twitter-API features. Signed-off-by: DiogoAndre --- app/controllers/api/statuses_controller.rb | 16 ++++-- .../update_twitter_json_decorator.rb | 22 ++++++-- config/routes.rb | 2 +- test/acceptance/api/twitter_api_test.rb | 51 +++++++++++++++++++ test/acceptance/json_api/statuses_test.rb | 14 ----- 5 files changed, 82 insertions(+), 23 deletions(-) delete mode 100644 test/acceptance/json_api/statuses_test.rb diff --git a/app/controllers/api/statuses_controller.rb b/app/controllers/api/statuses_controller.rb index adf609ab..27bdbcd7 100644 --- a/app/controllers/api/statuses_controller.rb +++ b/app/controllers/api/statuses_controller.rb @@ -13,12 +13,20 @@ class StatusesController < ApplicationController before_filter :require_user def show - @update = Update.first(:id => params[:id]) - respond_to do |format| - format.json do - render :json => UpdateJsonDecorator.decorate(@update) + update = Update.find(params[:id]) + if !update.nil? + respond_to do |format| + format.json do + include_entities = (params[:include_entities] == "true") + trim_user = (params[:trim_user] == "true") + update = UpdateTwitterJsonDecorator.decorate(update) + render :json => update.as_json(:include_entities => include_entities,:trim_user => trim_user) + end end + else + render :nothing => true, :status => 404 end + end # # POST /api/statuses/update.json diff --git a/app/decorators/update_twitter_json_decorator.rb b/app/decorators/update_twitter_json_decorator.rb index cab45c11..30931059 100644 --- a/app/decorators/update_twitter_json_decorator.rb +++ b/app/decorators/update_twitter_json_decorator.rb @@ -3,6 +3,7 @@ class UpdateTwitterJsonDecorator < ApplicationDecorator def as_json(options={}) referral = (update.referral ? update.referral : nil) + author = update.author result = { :id => update.id, :id_str => update.id.to_s, @@ -20,8 +21,8 @@ def as_json(options={}) :in_reply_to_screen_name => nil, :place => nil, :user => { - :id_str => update.id.to_s, - :id => update.id + :id_str => author.id.to_s, + :id => author.id } } if options[:include_entities] @@ -32,8 +33,21 @@ def as_json(options={}) :user_mentions => [] } end - unless result[:trim_user] - # TODO expand response[:user] + unless options[:trim_user] + author_decorator = AuthorDecorator.decorate(author) + author_info = { + :url => author_decorator.absolute_website_url, + :screen_name => author.username, + :name => author.display_name, + :profile_image_url => author_decorator.absolute_avatar_url, + :created_at => format_timestamp(author.created_at), + :description => author.bio, + :statuses_count => author.feed.updates.count, + :friends_count => author.user.following.length, + :followers_count => author.user.followers.length + } + author_info[:profile_image_url].prepend("http://rstat.us") if author_info[:profile_image_url] == ActionController::Base.helpers.asset_path(RstatUs::DEFAULT_AVATAR) + result[:user].merge!(author_info) end result end diff --git a/config/routes.rb b/config/routes.rb index a3689513..dba97614 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -76,6 +76,6 @@ match 'statuses/home_timeline.:format', :to => "statuses#home_timeline", :via => :get, :constraints => { :format => "json" } match 'statuses/user_timeline.:format', :to => "statuses#user_timeline", :via => :get, :constraints => { :format => "json" } match 'statuses/mention.:format', :to => "statuses#mention", :via => :get, :constraints => { :format => "json" } - match "statuses/show/:id", :to => "statuses#show" + match 'statuses/show/:id.:format', :to => "statuses#show", :via => :get, :constraints => { :id => /[^\/]+/, :format => 'json'} end end diff --git a/test/acceptance/api/twitter_api_test.rb b/test/acceptance/api/twitter_api_test.rb index 31d7c3ac..a6293805 100644 --- a/test/acceptance/api/twitter_api_test.rb +++ b/test/acceptance/api/twitter_api_test.rb @@ -28,6 +28,57 @@ end end + describe "statuses" do + it "returns a single status" do + log_in_as_some_user + u = Fabricate(:user) + + update = Fabricate(:update, + :text => "Hello World I'm on RStatus", + :author => u.author) + u.feed.updates << update + author_decorator = AuthorDecorator.decorate(u.author) + + visit "/api/statuses/show/#{u.feed.updates.first.id}.json" + + parsed_json = JSON.parse(source) + parsed_json["text"].must_equal(update.text) + parsed_json["user"]["url"].must_equal(author_decorator.absolute_website_url) + parsed_json["user"]["screen_name"].must_equal(author_decorator.username) + parsed_json["user"]["name"].must_equal(author_decorator.display_name) + parsed_json["user"]["profile_image_url"].wont_be_nil + Time.parse(parsed_json["user"]["created_at"]).to_i.must_equal(author_decorator.created_at.to_i) + parsed_json["user"]["description"].must_equal(author_decorator.bio) + parsed_json["user"]["statuses_count"].must_equal(author_decorator.feed.updates.count) + parsed_json["user"]["friends_count"].must_equal(u.following.length) + parsed_json["user"]["followers_count"].must_equal(u.followers.length) + end + + it "returns a single status with trimmed user" do + log_in_as_some_user + u = Fabricate(:user) + + update = Fabricate(:update, + :text => "Hello World I'm on RStatus", + :author => u.author) + u.feed.updates << update + + visit "/api/statuses/show/#{u.feed.updates.first.id}.json?trim_user=true" + + parsed_json = JSON.parse(source) + parsed_json["text"].must_equal(update.text) + parsed_json["user"].wont_include("url","url should not be included in trimmed status") + parsed_json["user"].wont_include("screen_name","screen_name should not be included in trimmed status") + parsed_json["user"].wont_include("name","name should not be included in trimmed status") + parsed_json["user"].wont_include("profile_image_url","profile_image_url should not be included in trimmed status") + parsed_json["user"].wont_include("created_at","created_at should not be included in trimmed status") + parsed_json["user"].wont_include("description","description should not be included in trimmed status") + parsed_json["user"].wont_include("statuses_count","statuses_count should not be included in trimmed status") + parsed_json["user"].wont_include("friends_count","friends_count should not be included in trimmed status") + parsed_json["user"].wont_include("followers_count","followers_count should not be included in trimmed status") + end + end + describe "home_timeline" do it "it returns the home timeline for the user" do u = Fabricate(:user) diff --git a/test/acceptance/json_api/statuses_test.rb b/test/acceptance/json_api/statuses_test.rb deleted file mode 100644 index 90c26b88..00000000 --- a/test/acceptance/json_api/statuses_test.rb +++ /dev/null @@ -1,14 +0,0 @@ -require 'require_relative' if RUBY_VERSION[0,3] == '1.8' -require_relative '../acceptance_helper' - -describe "JSON get statos" do - include AcceptanceHelper - - it "returns the update" do - log_in_as_some_user - status = Fabricate(:update) - visit "/api/statuses/show/#{status.id}.json" - parsed_json = JSON.parse(source) - parsed_json["text"].must_equal(status.text) - end -end