diff --git a/README.md b/README.md index 094c2c370..f8e889477 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ but object equivalence seems like a step in the right direction. * All deprecated methods have been removed. * `Twitter::Client#totals` has been removed. Use `Twitter::Client#user` instead. +* `Twitter.faraday_options` has been renamed to `Twitter.connection_options`. * `Twitter::Client#friendships` now takes up to 3 arguments instead of 1. * Support for the XML response format has been removed. This decision was guided largely by Twitter, who has started removing XML responses available diff --git a/lib/twitter/config.rb b/lib/twitter/config.rb index fa3a997ae..75de57d1b 100644 --- a/lib/twitter/config.rb +++ b/lib/twitter/config.rb @@ -3,28 +3,17 @@ module Twitter # Defines constants and methods related to configuration module Config - # An array of valid keys in the options hash when configuring a {Twitter::API} - VALID_OPTIONS_KEYS = [ - :adapter, - :consumer_key, - :consumer_secret, - :endpoint, - :gateway, - :oauth_token, - :oauth_token_secret, - :proxy, - :search_endpoint, - :user_agent, - :media_endpoint, - :faraday_options].freeze - # The adapter that will be used to connect if none is set + # The HTTP connection adapter that will be used to connect if none is set DEFAULT_ADAPTER = :net_http - # By default, don't set an application key + # The Faraday connection options if none is set + DEFAULT_CONNECTION_OPTIONS = {} + + # The consumer key if none is set DEFAULT_CONSUMER_KEY = nil - # By default, don't set an application secret + # The consumer secret if none is set DEFAULT_CONSUMER_SECRET = nil # The endpoint that will be used to connect if none is set @@ -34,32 +23,47 @@ module Config # @see http://en.blog.wordpress.com/2009/12/12/twitter-api/ # @see http://staff.tumblr.com/post/287703110/api # @see http://developer.typepad.com/typepad-twitter-api/twitter-api.html - DEFAULT_ENDPOINT = 'https://api.twitter.com'.freeze + DEFAULT_ENDPOINT = 'https://api.twitter.com' + + # The gateway server if none is set + DEFAULT_GATEWAY = nil - # By default, don't set a user oauth token + # This endpoint will be used by default when updating statuses with media + DEFAULT_MEDIA_ENDPOINT = 'https://upload.twitter.com' + + # The oauth token if none is set DEFAULT_OAUTH_TOKEN = nil - # By default, don't set a user oauth secret + # The oauth token secret if none is set DEFAULT_OAUTH_TOKEN_SECRET = nil - # By default, don't use a proxy server + # The proxy server if none is set DEFAULT_PROXY = nil # The search endpoint that will be used to connect if none is set # # @note This is configurable in case you want to use HTTP instead of HTTPS or use a Twitter-compatible endpoint. # @see http://status.net/wiki/Twitter-compatible_API - DEFAULT_SEARCH_ENDPOINT = 'https://search.twitter.com'.freeze + DEFAULT_SEARCH_ENDPOINT = 'https://search.twitter.com' # The value sent in the 'User-Agent' header if none is set - DEFAULT_USER_AGENT = "Twitter Ruby Gem #{Twitter::Version}".freeze - - DEFAULT_GATEWAY = nil + DEFAULT_USER_AGENT = "Twitter Ruby Gem #{Twitter::Version}" - # This endpoint will be used by default when updating statuses with media - DEFAULT_MEDIA_ENDPOINT = 'https://upload.twitter.com'.freeze - - DEFAULT_FARADAY_OPTIONS = {}.freeze + # An array of valid keys in the options hash when configuring a {Twitter::API} + VALID_OPTIONS_KEYS = [ + :adapter, + :connection_options, + :consumer_key, + :consumer_secret, + :endpoint, + :gateway, + :oauth_token, + :oauth_token_secret, + :proxy, + :search_endpoint, + :user_agent, + :media_endpoint + ] attr_accessor *VALID_OPTIONS_KEYS @@ -84,17 +88,17 @@ def options # Reset all configuration options to defaults def reset self.adapter = DEFAULT_ADAPTER + self.connection_options = DEFAULT_CONNECTION_OPTIONS self.consumer_key = DEFAULT_CONSUMER_KEY self.consumer_secret = DEFAULT_CONSUMER_SECRET self.endpoint = DEFAULT_ENDPOINT + self.gateway = DEFAULT_GATEWAY + self.media_endpoint = DEFAULT_MEDIA_ENDPOINT self.oauth_token = DEFAULT_OAUTH_TOKEN self.oauth_token_secret = DEFAULT_OAUTH_TOKEN_SECRET self.proxy = DEFAULT_PROXY self.search_endpoint = DEFAULT_SEARCH_ENDPOINT self.user_agent = DEFAULT_USER_AGENT - self.gateway = DEFAULT_GATEWAY - self.media_endpoint = DEFAULT_MEDIA_ENDPOINT - self.faraday_options = DEFAULT_FARADAY_OPTIONS self end diff --git a/lib/twitter/connection.rb b/lib/twitter/connection.rb index a4526b8aa..9dcb59b04 100644 --- a/lib/twitter/connection.rb +++ b/lib/twitter/connection.rb @@ -1,3 +1,4 @@ +require 'faraday' require 'twitter/request/gateway' require 'twitter/request/multipart_with_file' require 'twitter/request/phoenix' @@ -10,8 +11,11 @@ module Twitter module Connection private + # Returns a Faraday::Connection object + # + # @return [Faraday::Connection] def connection(options={}) - merged_options = faraday_options.merge({ + merged_options = connection_options.merge({ :headers => { :accept => 'application/json', :user_agent => user_agent, @@ -23,7 +27,7 @@ def connection(options={}) Faraday.new(merged_options) do |builder| builder.use Twitter::Request::Phoenix if options[:phoenix] builder.use Twitter::Request::MultipartWithFile - builder.use Twitter::Request::TwitterOAuth, credentials if authenticated? + builder.use Twitter::Request::TwitterOAuth, credentials if credentials? builder.use Faraday::Request::Multipart builder.use Faraday::Request::UrlEncoded builder.use Twitter::Request::Gateway, gateway if gateway diff --git a/spec/twitter/api_spec.rb b/spec/twitter/api_spec.rb index e4587ad6a..e140af3ce 100644 --- a/spec/twitter/api_spec.rb +++ b/spec/twitter/api_spec.rb @@ -30,6 +30,7 @@ before do @configuration = { + :connection_options => {:timeout => 10}, :consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', @@ -41,7 +42,6 @@ :search_endpoint => 'http://google.com/', :media_endpoint => 'https://upload.twitter.com/', :user_agent => 'Custom User Agent', - :faraday_options => {:timeout => 10} } end diff --git a/spec/twitter/search_spec.rb b/spec/twitter/search_spec.rb index 4710229af..5966cdc4a 100644 --- a/spec/twitter/search_spec.rb +++ b/spec/twitter/search_spec.rb @@ -37,6 +37,7 @@ before do @configuration = { + :connection_options => {:timeout => 10}, :consumer_key => 'CK', :consumer_secret => 'CS', :oauth_token => 'OT', @@ -48,7 +49,6 @@ :search_endpoint => 'http://google.com/', :media_endpoint => 'https://upload.twitter.com/', :user_agent => 'Custom User Agent', - :faraday_options => {:timeout => 10} } end