Skip to content

Commit

Permalink
Remove from_response method
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jan 29, 2014
1 parent be2b502 commit 6f9a352
Show file tree
Hide file tree
Showing 17 changed files with 31 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ end

require 'yardstick/rake/verify'
Yardstick::Rake::Verify.new do |verify|
verify.threshold = 59.8
verify.threshold = 59.7
end

task :default => [:spec, :rubocop, :verify_measurements]
8 changes: 0 additions & 8 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ class Base
deprecate_alias :to_hsh, :to_h

class << self
# Construct an object from a response hash
#
# @param response [Hash]
# @return [Twitter::Base]
def from_response(response = {})
new(response[:body])
end

# Define methods that retrieve the value from attributes
#
# @param attrs [Array, Symbol]
Expand Down
17 changes: 2 additions & 15 deletions lib/twitter/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,6 @@ class Cursor
deprecate_alias :to_hash, :to_h
deprecate_alias :to_hsh, :to_h

class << self
# Construct a new Cursor object from a response hash
#
# @param response [Hash]
# @param key [String, Symbol] The key to fetch the data from the response
# @param klass [Class] The class to instantiate objects in the response
# @param request [Twitter::Request]
# @return [Twitter::Cursor]
def from_response(response, key, klass, request)
new(response[:body], key, klass, request)
end
end

# Initializes a new Cursor
#
# @param attrs [Hash]
Expand Down Expand Up @@ -56,8 +43,8 @@ def last?

# @return [Hash]
def fetch_next_page
response = @client.send(@request_method, @path, @options.merge(:cursor => next_cursor))
self.attrs = response[:body]
response = @client.send(@request_method, @path, @options.merge(:cursor => next_cursor)).body
self.attrs = response
end

# @param attrs [Hash]
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class << self
# @param response [Hash]
# @return [Twitter::Error]
def from_response(response = {})
error, code = parse_error(response[:body])
new(error, response[:response_headers], code)
error, code = parse_error(response.body)
new(error, response.response_headers, code)
end

# @return [Hash]
Expand Down
10 changes: 0 additions & 10 deletions lib/twitter/geo_results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ class GeoResults
deprecate_alias :to_hash, :to_h
deprecate_alias :to_hsh, :to_h

class << self
# Construct a new GeoResults object from a response hash
#
# @param response [Hash]
# @return [Twitter::Base]
def from_response(response = {})
new(response[:body])
end
end

# Initializes a new GeoResults object
#
# @param attrs [Hash]
Expand Down
8 changes: 4 additions & 4 deletions lib/twitter/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ def initialize(client, request_method, path, options = {})

# @return [Hash]
def perform
@client.send(@request_method, @path, @options)
@client.send(@request_method, @path, @options).body
end

# @param klass [Class]
# @param request [Twitter::Request]
# @return [Object]
def perform_with_object(klass)
klass.from_response(perform)
klass.new(perform)
end

# @param collection_name [Symbol]
# @param klass [Class]
# @return [Twitter::Cursor]
def perform_with_cursor(collection_name, klass = nil)
Twitter::Cursor.from_response(perform, collection_name.to_sym, klass, self)
Twitter::Cursor.new(perform, collection_name.to_sym, klass, self)
end

# @param klass [Class]
# @return [Array]
def perform_with_objects(klass)
perform[:body].collect do |element|
perform.collect do |element|
klass.new(element)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/rest/friends_and_followers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def friends(*args)
# @return [Array<Integer>]
# @param options [Hash] A customizable set of options.
def no_retweet_ids(options = {})
get('/1.1/friendships/no_retweets/ids.json', options)[:body].collect(&:to_i)
get('/1.1/friendships/no_retweets/ids.json', options).body.collect(&:to_i)
end
alias_method :no_retweets_ids, :no_retweet_ids
end
Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/rest/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def languages(options = {})
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @return [String]
def privacy(options = {})
get('/1.1/help/privacy.json', options)[:body][:privacy]
get('/1.1/help/privacy.json', options).body[:privacy]
end

# Returns {https://twitter.com/tos Twitter's Terms of Service}
Expand All @@ -49,7 +49,7 @@ def privacy(options = {})
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @return [String]
def tos(options = {})
get('/1.1/help/tos.json', options)[:body][:tos]
get('/1.1/help/tos.json', options).body[:tos]
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/twitter/rest/request/multipart_with_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class MultipartWithFile < Faraday::Middleware
PNG_REGEX = /\.png$/i

def call(env)
env[:body].each do |key, value|
env.body.each do |key, value|
if value.respond_to?(:to_io)
env[:body][key] = Faraday::UploadIO.new(value, mime_type(value.path), value.path)
env.body[key] = Faraday::UploadIO.new(value, mime_type(value.path), value.path)
end
end if env[:body].is_a?(::Hash)
end if env.body.is_a?(::Hash)
@app.call(env)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/rest/response/parse_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def parse(body)

def on_complete(env)
if respond_to?(:parse)
env[:body] = parse(env[:body]) unless unparsable_status_codes.include?(env[:status])
env.body = parse(env.body) unless unparsable_status_codes.include?(env[:status])
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/twitter/rest/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ module Search
def search(q, options = {})
options[:count] ||= MAX_TWEETS_PER_REQUEST
request = Twitter::Request.new(self, :get, '/1.1/search/tweets.json', options.merge(:q => q))
response = get(request.path, request.options)
Twitter::SearchResults.from_response(response, request)
response = get(request.path, request.options).body
Twitter::SearchResults.new(response, request)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/twitter/rest/trends.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module Trends
# @return [Array<Twitter::Trend>]
def trends(id = 1, options = {})
options[:id] = id
perform_with_object(:get, '/1.1/trends/place.json', options, Twitter::TrendResults)
response = get('/1.1/trends/place.json', options).body.first
Twitter::TrendResults.new(response)
end
alias_method :local_trends, :trends
alias_method :trends_place, :trends
Expand Down
9 changes: 4 additions & 5 deletions lib/twitter/rest/tweets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,10 @@ def parallel_tweets_from_response(request_method, path, args)
end

def post_retweet(tweet, options)
response = post("/1.1/statuses/retweet/#{extract_id(tweet)}.json", options)
retweeted_status = response.dup
retweeted_status[:body] = response[:body].delete(:retweeted_status)
retweeted_status[:body][:retweeted_status] = response[:body]
Twitter::Tweet.from_response(retweeted_status)
response = post("/1.1/statuses/retweet/#{extract_id(tweet)}.json", options).body
retweeted_status = response.delete(:retweeted_status)
retweeted_status[:retweeted_status] = response
Twitter::Tweet.new(retweeted_status)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/twitter/rest/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ module Users
# @option options [String] :lang The language which Twitter should render in for this user. The language must be specified by the appropriate two letter ISO 639-1 representation. Currently supported languages are provided by {https://dev.twitter.com/docs/api/1.1/get/help/languages GET help/languages}.
def settings(options = {})
request_method = options.size.zero? ? :get : :post
response = send(request_method.to_sym, '/1.1/account/settings.json', options)
response = send(request_method.to_sym, '/1.1/account/settings.json', options).body
# https://dev.twitter.com/issues/59
response.update(:trend_location => Array(response[:trend_location]).first)
Twitter::Settings.from_response(response)
Twitter::Settings.new(response)
end

# Returns the requesting user if authentication was successful, otherwise raises {Twitter::Error::Unauthorized}
Expand Down Expand Up @@ -335,7 +335,7 @@ def contributors(*args)
# @return [nil]
# @param options [Hash] A customizable set of options.
def remove_profile_banner(options = {})
post('/1.1/account/remove_profile_banner.json', options)[:body]
post('/1.1/account/remove_profile_banner.json', options).body
end
deprecate_alias :profile_banner_remove, :remove_profile_banner

Expand All @@ -357,7 +357,7 @@ def remove_profile_banner(options = {})
# @option options [Integer] :offset_left The number of pixels by which to offset the uploaded image from the left. Use with height, width, and offset_top to select the desired region of the image to use.
# @option options [Integer] :offset_top The number of pixels by which to offset the uploaded image from the top. Use with height, width, and offset_left to select the desired region of the image to use.
def update_profile_banner(banner, options = {})
post('/1.1/account/update_profile_banner.json', options.merge(:banner => banner))[:body]
post('/1.1/account/update_profile_banner.json', options.merge(:banner => banner)).body
end

# Returns the available size variations of the specified user's profile banner.
Expand Down
15 changes: 2 additions & 13 deletions lib/twitter/search_results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@ class SearchResults
deprecate_alias :to_hash, :to_h
deprecate_alias :to_hsh, :to_h

class << self
# Construct a new SearchResults object from a response hash
#
# @param response [Hash]
# @param request [Twitter::Request]
# @return [Twitter::SearchResults]
def from_response(response, request)
new(response[:body], request)
end
end

# Initializes a new SearchResults object
#
# @param attrs [Hash]
Expand Down Expand Up @@ -61,8 +50,8 @@ def next_page

# @return [Hash]
def fetch_next_page
response = @client.send(@request_method, @path, next_page)
self.attrs = response[:body]
response = @client.send(@request_method, @path, next_page).body
self.attrs = response
end

# @param attrs [Hash]
Expand Down
10 changes: 0 additions & 10 deletions lib/twitter/trend_results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ class TrendResults
deprecate_alias :to_hash, :to_h
deprecate_alias :to_hsh, :to_h

class << self
# Construct a new TrendResults object from a response hash
#
# @param response [Hash]
# @return [Twitter::Base]
def from_response(response = {})
new(response[:body].first)
end
end

# Initializes a new TrendResults object
#
# @param attrs [Hash]
Expand Down
2 changes: 1 addition & 1 deletion spec/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

SimpleCov.start do
add_filter '/spec/'
minimum_coverage(99.42)
minimum_coverage(99.41)
end

require 'twitter'
Expand Down

0 comments on commit 6f9a352

Please sign in to comment.