Skip to content

Commit

Permalink
Merge pull request #674 from DiogoAndre/show-status-ext
Browse files Browse the repository at this point in the history
Improving issue #574 implementation.
  • Loading branch information
wilkie committed Oct 5, 2012
2 parents 92ee7fb + eb430c5 commit b324ef7
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 23 deletions.
16 changes: 12 additions & 4 deletions app/controllers/api/statuses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 18 additions & 4 deletions app/decorators/update_twitter_json_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
51 changes: 51 additions & 0 deletions test/acceptance/api/twitter_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 0 additions & 14 deletions test/acceptance/json_api/statuses_test.rb

This file was deleted.

0 comments on commit b324ef7

Please sign in to comment.