lib/twitter/client.rb in twitter-4.3.0 vs lib/twitter/client.rb in twitter-4.4.0

- old
+ new

@@ -25,11 +25,10 @@ # Wrapper for the Twitter REST API # # @note All methods have been separated into modules and follow the same grouping used in {http://dev.twitter.com/doc the Twitter API Documentation}. # @see http://dev.twitter.com/pages/every_developer class Client - include Twitter::API include Twitter::API::DirectMessages include Twitter::API::Favorites include Twitter::API::FriendsAndFollowers include Twitter::API::Help include Twitter::API::Lists @@ -65,60 +64,40 @@ request(:get, path, params) end # Perform an HTTP POST request def post(path, params={}) - request(:post, path, params) + signature_params = params.values.any?{|value| value.respond_to?(:to_io)} ? {} : params + request(:post, path, params, signature_params) end - # Perform an HTTP UPDATE request + # Perform an HTTP PUT request def put(path, params={}) request(:put, path, params) end private - # Returns a Faraday::Connection object - # - # @return [Faraday::Connection] - def connection - @connection ||= Faraday.new(@endpoint, @connection_options.merge(:builder => @middleware)) - end - - # Perform an HTTP request - # - # @raise [Twitter::Error::ClientError, Twitter::Error::DecodeError] - def request(method, path, params={}) - uri = URI(@endpoint) unless uri.respond_to?(:host) - uri += path - request_headers = {} - if credentials? - authorization = auth_header(method, uri, params) - request_headers[:authorization] = authorization.to_s - end - connection.url_prefix = @endpoint - response = connection.run_request(method.to_sym, path, nil, request_headers) do |request| - unless params.empty? - case request.method - when :post, :put - request.body = params - else - request.params.update(params) - end - end - yield request if block_given? + def request(method, path, params={}, signature_params=params) + connection.send(method.to_sym, path, params) do |request| + request.headers[:authorization] = auth_header(method.to_sym, path, signature_params).to_s end.env - response rescue Faraday::Error::ClientError raise Twitter::Error::ClientError rescue MultiJson::DecodeError raise Twitter::Error::DecodeError end - def auth_header(method, uri, params={}) - # When posting a file, don't sign any params - signature_params = [:post, :put].include?(method.to_sym) && params.values.any?{|value| value.respond_to?(:to_io)} ? {} : params - SimpleOAuth::Header.new(method, uri, signature_params, credentials) + # Returns a Faraday::Connection object + # + # @return [Faraday::Connection] + def connection + @connection ||= Faraday.new(@endpoint, @connection_options.merge(:builder => @middleware)) + end + + def auth_header(method, path, params={}) + uri = URI(@endpoint + path) + SimpleOAuth::Header.new(method, uri, params, credentials) end end end