Skip to content
Browse files

Fixed error handling

  • Loading branch information...
1 parent 7eb79d3 commit 727f1dbd53de9be16de7cd74473c749964e3ac80 @yayugu committed Mar 3, 2014
Showing with 25 additions and 10 deletions.
  1. +16 −5 app/controllers/feed_controller.rb
  2. +2 −0 app/models/twitter_api_response.rb
  3. +7 −5 lib/twitter_client.rb
View
21 app/controllers/feed_controller.rb
@@ -5,24 +5,30 @@ class FeedController < ApplicationController
def user
res = @client.user(params[:name], TWEET_COUNT)
- @image_url = res.first['user']['profile_image_url']
- @name = res.first['user']['screen_name']
- @tweets = Tweet.tweets_from_hash_list(res)
+ render_error and return if res.error
+
+ @image_url = res.body.first['user']['profile_image_url']
+ @name = res.body.first['user']['screen_name']
+ @tweets = Tweet.tweets_from_hash_list(res.body)
render_rss
end
def list
res = @client.list(params[:name], params[:slug], TWEET_COUNT)
+ render_error and return if res.error
+
@name = params[:name]
@slug = params[:slug]
- @tweets = Tweet.tweets_from_hash_list(res)
+ @tweets = Tweet.tweets_from_hash_list(res.body)
render_rss
end
def search
res = @client.search(params[:q], TWEET_COUNT)
+ render_error and return if res.error
+
@query = params[:q]
- @tweets = Tweet.tweets_from_hash_list(res)
+ @tweets = Tweet.tweets_from_hash_list(res.body)
render_rss
end
@@ -37,11 +43,16 @@ def prepare_twitter_client
user = user_for_api_token
unless user
render :file => "public/404.html", :status => :unauthorized
+ return
end
@client = TwitterClient.new(user.twitter_access_token, user.twitter_access_secret)
end
def render_rss
render layout: false, content_type: 'application/rss+xml'
end
+
+ def render_error
+ render :file => "public/500.html", :status => 500
+ end
end
View
2 app/models/twitter_api_response.rb
@@ -0,0 +1,2 @@
+class TwitterAPIResponse < Struct.new(:body, :error)
+end
View
12 lib/twitter_client.rb
@@ -16,7 +16,9 @@ def list(name, slug, count)
end
def search(query, count)
- get_and_json_parse(search_sub_url(query, count))['statuses']
+ response = get_and_json_parse(search_sub_url(query, count))
+ response.body = response.body['statuses']
+ response
end
private
@@ -34,10 +36,10 @@ def search_sub_url(query, count)
end
def get_and_json_parse(sub_url)
- body = @access_token
- .get(api_url(sub_url))
- .body
- JSON.parse(body)
+ # Net::HTTPResponse
+ response = @access_token.get(api_url(sub_url))
+ body = JSON.parse(response.body)
+ TwitterAPIResponse.new(body, response.value)
end
def api_url(sub_url)

0 comments on commit 727f1db

Please sign in to comment.
Something went wrong with that request. Please try again.