Skip to content

Commit

Permalink
Remove explicit proxy and user_agent configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jun 22, 2012
1 parent bb8a15d commit f6e647f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 74 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,36 @@ This functionality may be replicated by inserting custom Faraday middleware.

### Configuration

The Faraday middleware stack is now fully configurable and is exposed as a
The Faraday middleware stack is now fully configurable and is exposed as a
`Faraday::Builder` which can be manipulated in place:

Twitter.middleware.insert_after Twitter::Response::RaiseClientError, CustomMiddleware

Likewise, the middleware stack can be replaced in it's entirety:

Twitter.middleware = Faraday::Builder.new(&Proc.new { |builder|
Twitter.middleware = Faraday::Builder.new(&Proc.new{|builder|
# Specify a middleware stack here
})

As part of these configuration changes we've removed `adapter` configuration.
As part of these configuration changes we've removed `adapter` configuration.
If you would like to change the adapter used, you can do so by setting Faraday's
`default_adapter`:

Faraday.default_adapter = :some_other_adapter

The adapter can also be configured as part of the middleware stack:

Twitter.middleware = Faraday::Builder.new(&Proc.new { |builder|
# Specify a middleware stack here
builder.adapter :some_other_adapter
})

The `proxy` and `user_agent` configuration have also been removed. These can be
set via the `connection_options` configuration.

Twitter.connection_options[:proxy] = 'http://erik:[email protected]:8080'
Twitter.connection_options[:headers][:user_agent] = 'Custom User Agent'

### Authentication

This library now attempts to pull credentials from `ENV` if they are not
Expand Down
20 changes: 9 additions & 11 deletions lib/twitter/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ module Twitter
module Config

# The Faraday connection options if none is set
DEFAULT_CONNECTION_OPTIONS = {}
DEFAULT_CONNECTION_OPTIONS = {
:headers => {
:accept => 'application/json',
:user_agent => "Twitter Ruby Gem #{Twitter::Version}"
},
:open_timeout => 5,
:ssl => {:verify => false},
:timeout => 10,
}

# The consumer key if none is set
DEFAULT_CONSUMER_KEY = ENV['TWITTER_CONSUMER_KEY']
Expand Down Expand Up @@ -51,12 +59,6 @@ module Config
# The oauth token secret if none is set
DEFAULT_OAUTH_TOKEN_SECRET = ENV['TWITTER_OAUTH_TOKEN_SECRET']

# The proxy server if none is set
DEFAULT_PROXY = nil

# The value sent in the 'User-Agent' header if none is set
DEFAULT_USER_AGENT = "Twitter Ruby Gem #{Twitter::Version}"

# An array of valid keys in the options hash when configuring a {Twitter::Client}
VALID_OPTIONS_KEYS = [
:connection_options,
Expand All @@ -67,8 +69,6 @@ module Config
:middleware,
:oauth_token,
:oauth_token_secret,
:proxy,
:user_agent,
]

attr_accessor *VALID_OPTIONS_KEYS
Expand Down Expand Up @@ -101,8 +101,6 @@ def reset
self.middleware = DEFAULT_MIDDLEWARE
self.oauth_token = DEFAULT_OAUTH_TOKEN
self.oauth_token_secret = DEFAULT_OAUTH_TOKEN_SECRET
self.proxy = DEFAULT_PROXY
self.user_agent = DEFAULT_USER_AGENT
self
end

Expand Down
16 changes: 0 additions & 16 deletions lib/twitter/core_ext/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@ def except!(*keys)
self
end

# Merges self with another hash, recursively
#
# @param hash [Hash] The hash to merge
# @return [Hash]
def deep_merge(hash)
target = self.dup
hash.keys.each do |key|
if hash[key].is_a?(Hash) && self[key].is_a?(Hash)
target[key] = target[key].deep_merge(hash[key])
next
end
target[key] = hash[key]
end
target
end

# Take a list and merge it into the hash with the correct key
#
# @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object.
Expand Down
16 changes: 1 addition & 15 deletions lib/twitter/requestable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,7 @@ def post(path, params={}, options={})
# @return [Faraday::Connection]
def connection
return @connection if defined? @connection

default_options = {
:headers => {
:accept => 'application/json',
:user_agent => user_agent,
},
:open_timeout => 5,
:proxy => proxy,
:ssl => {:verify => false},
:timeout => 10,
}

options = default_options.deep_merge(connection_options)

@connection = Faraday.new(endpoint, options.merge(:builder => middleware))
@connection = Faraday.new(endpoint, connection_options.merge(:builder => middleware))
end

# Perform an HTTP request
Expand Down
15 changes: 1 addition & 14 deletions spec/twitter/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@

before do
@configuration = {
:connection_options => {:timeout => 10},
:consumer_key => 'CK',
:consumer_secret => 'CS',
:endpoint => 'http://tumblr.com/',
:media_endpoint => 'https://upload.twitter.com/',
:middleware => Proc.new{},
:oauth_token => 'OT',
:oauth_token_secret => 'OS',
:proxy => 'http://erik:[email protected]:8080',
:user_agent => 'Custom User Agent',
:connection_options => {:timeout => 10},
}
end

Expand Down Expand Up @@ -78,15 +76,4 @@
client2.current_user.screen_name.should eq 'pengwynn'
end

it "recursively merges connection options" do
stub_get("/1/statuses/user_timeline.json").
with(:query => {:screen_name => "sferik"}, :headers => {"Accept" => "application/json", "User-Agent" => "Custom User Agent"}).
to_return(:body => fixture("statuses.json"), :headers => {:content_type => "application/json; charset=utf-8"})
client = Twitter::Client.new(:connection_options => {:headers => {:user_agent => 'Custom User Agent'}})
client.user_timeline("sferik")
a_get("/1/statuses/user_timeline.json").
with(:query => {:screen_name => "sferik"}, :headers => {"User-Agent" => "Custom User Agent"}).
should have_been_made
end

end
13 changes: 0 additions & 13 deletions spec/twitter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,6 @@
end
end

describe ".user_agent" do
it "returns the default user agent" do
Twitter.user_agent.should eq Twitter::Config::DEFAULT_USER_AGENT
end
end

describe ".user_agent=" do
it "sets the user_agent" do
Twitter.user_agent = 'Custom User Agent'
Twitter.user_agent.should eq 'Custom User Agent'
end
end

describe '.middleware' do
it "returns a Faraday::Builder" do
Twitter.middleware.should be_kind_of(Faraday::Builder)
Expand Down

0 comments on commit f6e647f

Please sign in to comment.