Permalink
Browse files

Remove explicit proxy and user_agent configuration

  • Loading branch information...
1 parent bb8a15d commit f6e647f73eaa0f39b4306256789ded414ea9a8c2 @sferik committed Jun 22, 2012
Showing with 22 additions and 74 deletions.
  1. +11 −5 README.md
  2. +9 −11 lib/twitter/config.rb
  3. +0 −16 lib/twitter/core_ext/hash.rb
  4. +1 −15 lib/twitter/requestable.rb
  5. +1 −14 spec/twitter/client_spec.rb
  6. +0 −13 spec/twitter_spec.rb
View
@@ -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:sekret@proxy.example.com:8080'
+ Twitter.connection_options[:headers][:user_agent] = 'Custom User Agent'
+
### Authentication
This library now attempts to pull credentials from `ENV` if they are not
View
@@ -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']
@@ -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,
@@ -67,8 +69,6 @@ module Config
:middleware,
:oauth_token,
:oauth_token_secret,
- :proxy,
- :user_agent,
]
attr_accessor *VALID_OPTIONS_KEYS
@@ -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
@@ -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.
View
@@ -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
@@ -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:sekret@proxy.example.com:8080',
- :user_agent => 'Custom User Agent',
- :connection_options => {:timeout => 10},
}
end
@@ -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
View
@@ -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)

0 comments on commit f6e647f

Please sign in to comment.