From 1df33b7495093bc1f136d61b8aac9c9038414bc5 Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Thu, 28 Oct 2010 15:23:15 -0700 Subject: [PATCH] Add proxy support Closes #27. --- lib/twitter/configuration.rb | 4 +++- lib/twitter/connection.rb | 21 +++++++++++---------- spec/twitter/api_spec.rb | 3 ++- spec/twitter/search_spec.rb | 3 ++- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/twitter/configuration.rb b/lib/twitter/configuration.rb index 9a0e27696..edf2a073e 100644 --- a/lib/twitter/configuration.rb +++ b/lib/twitter/configuration.rb @@ -3,13 +3,14 @@ module Twitter module Configuration - VALID_OPTIONS_KEYS = [:consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret, :adapter, :endpoint, :search_endpoint, :format, :user_agent].freeze + VALID_OPTIONS_KEYS = [:consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret, :adapter, :endpoint, :format, :proxy, :search_endpoint, :user_agent].freeze VALID_FORMATS = [:json, :xml].freeze DEFAULT_ADAPTER = Faraday.default_adapter.freeze DEFAULT_ENDPOINT = 'https://api.twitter.com/1/'.freeze DEFAULT_SEARCH_ENDPOINT = 'https://search.twitter.com/'.freeze DEFAULT_FORMAT = :json.freeze + DEFAULT_PROXY = nil.freeze DEFAULT_USER_AGENT = "Twitter Ruby Gem #{Twitter::VERSION}".freeze attr_accessor *VALID_OPTIONS_KEYS @@ -31,6 +32,7 @@ def reset self.endpoint = DEFAULT_ENDPOINT self.search_endpoint = DEFAULT_SEARCH_ENDPOINT self.format = DEFAULT_FORMAT + self.proxy = DEFAULT_PROXY self.user_agent = DEFAULT_USER_AGENT end end diff --git a/lib/twitter/connection.rb b/lib/twitter/connection.rb index 309b4e94b..fc8bf4c13 100644 --- a/lib/twitter/connection.rb +++ b/lib/twitter/connection.rb @@ -7,19 +7,20 @@ module Connection def connection(raw=false) options = { - :headers => {:user_agent => user_agent}, + :headers => {'Accept' => "application/#{format}", 'User-Agent' => user_agent}, + :proxy => proxy, :ssl => {:verify => false}, - :url => api_endpoint + :url => api_endpoint, } - Faraday::Connection.new(options) do |builder| - builder.use Faraday::Request::Multipart - builder.use Faraday::Request::OAuth, authentication if authenticated? - builder.adapter(adapter) - builder.use Faraday::Response::RaiseHttp5xx - builder.use Faraday::Response::Parse unless raw - builder.use Faraday::Response::RaiseHttp4xx - builder.use Faraday::Response::Mashify unless raw + Faraday::Connection.new(options) do |connection| + connection.use Faraday::Request::Multipart + connection.use Faraday::Request::OAuth, authentication if authenticated? + connection.adapter(adapter) + connection.use Faraday::Response::RaiseHttp5xx + connection.use Faraday::Response::Parse unless raw + connection.use Faraday::Response::RaiseHttp4xx + connection.use Faraday::Response::Mashify unless raw end end end diff --git a/spec/twitter/api_spec.rb b/spec/twitter/api_spec.rb index d70852808..0fa652fff 100644 --- a/spec/twitter/api_spec.rb +++ b/spec/twitter/api_spec.rb @@ -36,8 +36,9 @@ :oauth_token_secret => 'OS', :adapter => :typhoeus, :endpoint => 'http://tumblr.com/', - :search_endpoint => 'http://google.com/', :format => :xml, + :proxy => 'http://erik:sekret@proxy.example.com:8080', + :search_endpoint => 'http://google.com/', :user_agent => 'Custom User Agent', } end diff --git a/spec/twitter/search_spec.rb b/spec/twitter/search_spec.rb index f2860bf7c..66a8a2de1 100644 --- a/spec/twitter/search_spec.rb +++ b/spec/twitter/search_spec.rb @@ -41,8 +41,9 @@ :oauth_token_secret => 'OS', :adapter => :typhoeus, :endpoint => 'http://tumblr.com/', - :search_endpoint => 'http://google.com/', :format => :xml, + :proxy => 'http://erik:sekret@proxy.example.com:8080', + :search_endpoint => 'http://google.com/', :user_agent => 'Custom User Agent', } end