Skip to content

Commit

Permalink
Add proxy support
Browse files Browse the repository at this point in the history
Closes #27.
  • Loading branch information
sferik committed Oct 28, 2010
1 parent 8246a23 commit 1df33b7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
4 changes: 3 additions & 1 deletion lib/twitter/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 11 additions & 10 deletions lib/twitter/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion spec/twitter/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion spec/twitter/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1df33b7

Please sign in to comment.