Skip to content

Commit

Permalink
Enable SSL by default; add option to disable SSL
Browse files Browse the repository at this point in the history
Closes #66
Closes #73
  • Loading branch information
sferik committed Oct 9, 2010
1 parent 2feaf88 commit c4f8907
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 33 deletions.
8 changes: 8 additions & 0 deletions lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ def self.format=(value)
@format = value
end

def self.scheme
@scheme ||= 'https'
end

def self.scheme=(value)
@scheme = value
end

def self.api_endpoint
api_endpoint = "api.twitter.com/#{Twitter.api_version}"
api_endpoint = Addressable::URI.heuristic_parse(api_endpoint).to_s
Expand Down
4 changes: 3 additions & 1 deletion lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,12 @@ def connection_with_builders(builders)
headers = {
:user_agent => Twitter.user_agent
}
@connection ||= Faraday::Connection.new(:url => Twitter.api_endpoint, :headers => headers) do |builder|
@connection = Faraday::Connection.new(:url => Twitter.api_endpoint, :headers => headers) do |builder|
builder.adapter(@adapter || Faraday.default_adapter)
builders.each do |b| builder.use b end
end
@connection.scheme = Twitter.scheme
@connection
end

def oauth_header(path, options)
Expand Down
4 changes: 3 additions & 1 deletion lib/twitter/geo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def connection
headers = {
:user_agent => Twitter.user_agent
}
@connection ||= Faraday::Connection.new(:url => @api_endpoint, :headers => headers) do |builder|
@connection = Faraday::Connection.new(:url => @api_endpoint, :headers => headers) do |builder|
builder.adapter(@adapter || Faraday.default_adapter)
builder.use Faraday::Response::RaiseErrors
case Twitter.format.to_s
Expand All @@ -48,6 +48,8 @@ def connection
end
builder.use Faraday::Response::Mashify
end
@connection.scheme = Twitter.scheme
@connection
end

end
Expand Down
4 changes: 3 additions & 1 deletion lib/twitter/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def connection
headers = {
:user_agent => user_agent
}
@connection ||= Faraday::Connection.new(:url => @api_endpoint.omit(:path), :headers => headers) do |builder|
@connection = Faraday::Connection.new(:url => @api_endpoint.omit(:path), :headers => headers) do |builder|
builder.adapter(@adapter || Faraday.default_adapter)
builder.use Faraday::Response::RaiseErrors
case Twitter.format.to_s
Expand All @@ -187,6 +187,8 @@ def connection
end
builder.use Faraday::Response::Mashify
end
@connection.scheme = Twitter.scheme
@connection
end

protected
Expand Down
4 changes: 3 additions & 1 deletion lib/twitter/trends.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def connection
headers = {
:user_agent => Twitter.user_agent
}
@connection ||= Faraday::Connection.new(:url => @api_endpoint, :headers => headers) do |builder|
@connection = Faraday::Connection.new(:url => @api_endpoint, :headers => headers) do |builder|
builder.adapter(@adapter || Faraday.default_adapter)
builder.use Faraday::Response::RaiseErrors
case Twitter.format.to_s
Expand All @@ -65,6 +65,8 @@ def connection
end
builder.use Faraday::Response::Mashify
end
@connection.scheme = Twitter.scheme
@connection
end

end
Expand Down
4 changes: 3 additions & 1 deletion lib/twitter/unauthenticated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ def connection_with_builders(builders)
headers = {
:user_agent => Twitter.user_agent
}
Faraday::Connection.new(:url => @api_endpoint, :headers => headers) do |builder|
@connection = Faraday::Connection.new(:url => @api_endpoint, :headers => headers) do |builder|
builder.adapter(@adapter || Faraday.default_adapter)
builders.each do |b| builder.use b end
end
@connection.scheme = Twitter.scheme
@connection
end

def merge_user_into_options!(user_id_or_screen_name, options={})
Expand Down
4 changes: 2 additions & 2 deletions test/faraday/raise_errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class RaiseErrorsTest < Test::Unit::TestCase
end

should "raise NotAcceptable when an invalid format is specified" do
stub_get('http://search.twitter.com/search.json?q=from%3Asferik', 'not_acceptable.json', 406)
stub_get('https://search.twitter.com/search.json?q=from%3Asferik', 'not_acceptable.json', 406)
assert_raise Twitter::NotAcceptable do
@search.from('sferik')
@search.fetch
end
end

should "raise EnhanceYourCalm when search is rate limited" do
stub_get('http://search.twitter.com/search.json?q=from%3Asferik', 'enhance_your_calm.json', 420)
stub_get('https://search.twitter.com/search.json?q=from%3Asferik', 'enhance_your_calm.json', 420)
assert_raise Twitter::EnhanceYourCalm do
@search.from('sferik')
@search.fetch
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def fixture_file(filename)
end

def twitter_url(url)
url =~ /^http/ ? url : "http://api.twitter.com#{url}"
url =~ /^http/ ? url : "https://api.twitter.com#{url}"
end

def stub_get(url, filename, status=nil, location=nil)
Expand Down
2 changes: 1 addition & 1 deletion test/twitter/geo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class GeoTest < Test::Unit::TestCase

should "limit the number of results returned" do
stub_get('/1/geo/reverse_geocode.json?lat=37.783935&max_results=2&long=-122.39361', 'hash.json')
assert Twitter::Geo.reverse_geocode(:lat => 37.783935, :long => -122.39361, :max_results => 2)
assert Twitter::Geo.reverse_geocode(:lat => 37.783935, :long => -122.39361, :max_results => 2)
end

should "lookup with granularity" do
Expand Down
28 changes: 14 additions & 14 deletions test/twitter/search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,67 +82,67 @@ class SearchTest < Test::Unit::TestCase
end

should "specify the language" do
stub_get('http://search.twitter.com/search.json?q=&lang=en', 'hash.json')
stub_get('https://search.twitter.com/search.json?q=&lang=en', 'hash.json')
assert @search.lang('en')
assert @search.fetch
end

should "specify the locale" do
stub_get('http://search.twitter.com/search.json?q=&locale=ja', 'hash.json')
stub_get('https://search.twitter.com/search.json?q=&locale=ja', 'hash.json')
assert @search.locale('ja')
assert @search.fetch
end

should "specify the number of results per page" do
stub_get('http://search.twitter.com/search.json?q=&rpp=25', 'hash.json')
stub_get('https://search.twitter.com/search.json?q=&rpp=25', 'hash.json')
assert @search.per_page(25)
assert @search.fetch
end

should "specify the page number" do
stub_get('http://search.twitter.com/search.json?q=&page=20', 'hash.json')
stub_get('https://search.twitter.com/search.json?q=&page=20', 'hash.json')
assert @search.page(20)
assert @search.fetch
end

should "specify only returning results greater than an id" do
stub_get('http://search.twitter.com/search.json?q=&since_id=1234', 'hash.json')
stub_get('https://search.twitter.com/search.json?q=&since_id=1234', 'hash.json')
assert @search.since(1234)
assert @search.fetch
end

should "specify since a date" do
stub_get('http://search.twitter.com/search.json?q=&since=2009-04-14', 'hash.json')
stub_get('https://search.twitter.com/search.json?q=&since=2009-04-14', 'hash.json')
assert @search.since_date('2009-04-14')
assert @search.fetch
end

should "specify until a date" do
stub_get('http://search.twitter.com/search.json?q=&until=2009-04-14', 'hash.json')
stub_get('https://search.twitter.com/search.json?q=&until=2009-04-14', 'hash.json')
assert @search.until_date('2009-04-14')
assert @search.fetch
end

should "specify geo coordinates" do
stub_get('http://search.twitter.com/search.json?q=&geocode=40.757929%2C-73.985506%2C25mi', 'hash.json')
stub_get('https://search.twitter.com/search.json?q=&geocode=40.757929%2C-73.985506%2C25mi', 'hash.json')
assert @search.geocode('40.757929', '-73.985506', '25mi')
assert @search.fetch
end

should "specify max id" do
stub_get('http://search.twitter.com/search.json?q=&max_id=1234', 'hash.json')
stub_get('https://search.twitter.com/search.json?q=&max_id=1234', 'hash.json')
assert @search.max(1234)
assert @search.fetch
end

should "set the phrase" do
stub_get('http://search.twitter.com/search.json?q=&phrase=Who%20Dat', 'hash.json')
stub_get('https://search.twitter.com/search.json?q=&phrase=Who%20Dat', 'hash.json')
assert @search.phrase('Who Dat')
assert @search.fetch
end

should "set the result type" do
stub_get('http://search.twitter.com/search.json?q=&result_type=popular', 'hash.json')
stub_get('https://search.twitter.com/search.json?q=&result_type=popular', 'hash.json')
assert @search.result_type('popular')
assert @search.fetch
end
Expand All @@ -162,7 +162,7 @@ class SearchTest < Test::Unit::TestCase
end

should "not replace the current query when fetching" do
stub_get('http://search.twitter.com/search.json?q=milk%20cheeze', 'hash.json')
stub_get('https://search.twitter.com/search.json?q=milk%20cheeze', 'hash.json')
assert @search.containing('milk').containing('cheeze')
assert_equal ['milk', 'cheeze'], @search.query[:q]
assert @search.fetch
Expand All @@ -171,7 +171,7 @@ class SearchTest < Test::Unit::TestCase

context "fetching" do
setup do
stub_get('http://search.twitter.com/search.json?q=%40sferik', 'hash.json')
stub_get('https://search.twitter.com/search.json?q=%40sferik', 'hash.json')
@search = Twitter::Search.new('@sferik')
@response = @search.fetch
end
Expand All @@ -187,7 +187,7 @@ class SearchTest < Test::Unit::TestCase

context "iterating over results" do
setup do
stub_get('http://search.twitter.com/search.json?q=from%3Asferik', 'hash.json')
stub_get('https://search.twitter.com/search.json?q=from%3Asferik', 'hash.json')
@search.from('sferik')
end

Expand Down
33 changes: 23 additions & 10 deletions test/twitter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class TwitterTest < Test::Unit::TestCase
assert_equal Faraday.default_adapter, Twitter.adapter
end

context "when overriding the adapter" do
should "be able to specify the adapter" do
context "when overriding adapter" do
should "be able to specify adapter" do
Twitter.adapter = :typhoeus
assert_equal :typhoeus, Twitter.adapter
# Reset
Expand All @@ -35,8 +35,8 @@ class TwitterTest < Test::Unit::TestCase
assert_equal 'Ruby Twitter Gem', Twitter.user_agent
end

context "when overriding the user_agent" do
should "be able to specify the user_agent" do
context "when overriding user_agent" do
should "be able to specify user_agent" do
Twitter.user_agent = 'My Twitter Gem'
assert_equal 'My Twitter Gem', Twitter.user_agent
# Reset
Expand All @@ -48,8 +48,8 @@ class TwitterTest < Test::Unit::TestCase
assert_equal "http://api.twitter.com/#{Twitter.api_version}", Twitter.api_endpoint
end

context "when overriding the api_endpoint" do
should "be able to specify the api_endpoint" do
context "when overriding api_endpoint" do
should "be able to specify api_endpoint" do
Twitter.api_endpoint = 'tumblr.com'
assert_equal 'http://tumblr.com', Twitter.api_endpoint
# Reset
Expand All @@ -61,8 +61,8 @@ class TwitterTest < Test::Unit::TestCase
assert_equal 1, Twitter.api_version
end

context "when overriding the api_version" do
should "be able to specify the api_version" do
context "when overriding api_version" do
should "be able to specify api_version" do
Twitter.api_version = 2
assert_equal 2, Twitter.api_version
# Reset
Expand All @@ -74,13 +74,26 @@ class TwitterTest < Test::Unit::TestCase
assert_equal "json", Twitter.format
end

context "when overriding the format" do
should "be able to specify the format" do
context "when overriding format" do
should "be able to specify format" do
Twitter.format = "xml"
assert_equal "xml", Twitter.format
# Reset
Twitter.format = "json"
end
end

should "default scheme to https" do
assert_equal "https", Twitter.scheme
end

context "when overriding scheme" do
should "be able to specify scheme" do
Twitter.scheme = "http"
assert_equal "http", Twitter.scheme
# Reset
Twitter.scheme = "https"
end
end

end

1 comment on commit c4f8907

@sferik
Copy link
Owner Author

@sferik sferik commented on c4f8907 Oct 12, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this turns out to be significantly faster in practice: https://gist.github.com/8ea6f99884291fbd1987

Please sign in to comment.