Skip to content

Commit

Permalink
Rename faraday_options to connection_options
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Oct 20, 2011
1 parent cacb6ca commit 221cb65
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 36 additions & 32 deletions lib/twitter/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down
8 changes: 6 additions & 2 deletions lib/twitter/connection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'faraday'
require 'twitter/request/gateway'
require 'twitter/request/multipart_with_file'
require 'twitter/request/phoenix'
Expand All @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/twitter/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

before do
@configuration = {
:connection_options => {:timeout => 10},
:consumer_key => 'CK',
:consumer_secret => 'CS',
:oauth_token => 'OT',
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/twitter/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

before do
@configuration = {
:connection_options => {:timeout => 10},
:consumer_key => 'CK',
:consumer_secret => 'CS',
:oauth_token => 'OT',
Expand All @@ -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

Expand Down

0 comments on commit 221cb65

Please sign in to comment.