Skip to content

Commit

Permalink
Implement GET /statuses/user_timeline.json.
Browse files Browse the repository at this point in the history
Almost identical to GET /statuses/home_timeline.json (see hotsh#570), but
the user is explicitly specified by the caller.

Added a bit of error handling guff relating to users that can't be found
and/or when the caller provides us with questionable input.

[hotsh#562]
  • Loading branch information
thomaslee committed Aug 20, 2012
1 parent 487313f commit 634638a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
70 changes: 65 additions & 5 deletions app/controllers/api/statuses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,67 @@ def update
end

def home_timeline
options = home_timeline_options
updates = current_user.timeline(options)
timeline_for current_user
rescue => e
handle_error e
end

def user_timeline
timeline_for requested_user!
rescue => e
handle_error e
end

protected

def handle_error(e)
respond_to do |fmt|
fmt.json do
status = {
NotFound => :not_found,
BadRequest => :bad_request
}[e.class]
raise e if status.nil?
render :status => status, :json => [e.message]
end
end
end

def requested_user!
if params[:user_id].blank? && params[:screen_name].blank?
#
# TODO this is an assumption. Verify against Twitter API.
#
raise BadRequest, "You must specify either user_id or screen_name"
elsif !params[:user_id].blank? && !params[:screen_name].blank?
#
# TODO verify if/how Twitter deals with this. Edge case, anyway.
#
raise BadRequest, "You can't specify both user_id and screen_name"
end

#
# Try to find a user by user_id first, then screen_name
#
user = nil
user = User.first(params[:user_id]) if !params[:user_id].blank?
if user.nil?
if !params[:screen_name].blank?
user = User.first(:username => params[:screen_name])
if user.nil?
raise NotFound, "User does not exist: #{params[:screen_name]}"
end
else
raise NotFound, "User ID does not exist: #{params[:user_id]}"
end
end

user
end

def timeline_for(user)
options = timeline_options
updates = user.timeline(options)
respond_to do |fmt|
fmt.json do
json = updates.map do |update|
Expand All @@ -54,8 +113,6 @@ def home_timeline
end
end

protected

def format_errors(errors)
errors = errors.full_messages.map do |error|
#
Expand All @@ -72,7 +129,7 @@ def format_errors(errors)
{:errors => errors}.to_json
end

def home_timeline_options
def timeline_options
options = {}
attrs = [
[:count, :int],
Expand Down Expand Up @@ -110,6 +167,9 @@ def update_options
:twitter => (params[:tweet] == "true")
}
end

class BadRequest < StandardError; end
class NotFound < StandardError; end
end
end

1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@
namespace :api do
match 'statuses/update.:format', :to => "statuses#update", :via => :post, :constraints => { :format => "json" }
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" }
end
end

0 comments on commit 634638a

Please sign in to comment.