Permalink
Browse files

Enable SSL by default; add option to disable SSL

Closes #66
Closes #73
  • Loading branch information...
1 parent 2feaf88 commit c4f8907d6595f93d63bc84d6575920a14774e656 @sferik committed Oct 9, 2010
View
8 lib/twitter.rb
@@ -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
View
4 lib/twitter/base.rb
@@ -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)
View
4 lib/twitter/geo.rb
@@ -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
@@ -48,6 +48,8 @@ def connection
end
builder.use Faraday::Response::Mashify
end
+ @connection.scheme = Twitter.scheme
+ @connection
end
end
View
4 lib/twitter/search.rb
@@ -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
@@ -187,6 +187,8 @@ def connection
end
builder.use Faraday::Response::Mashify
end
+ @connection.scheme = Twitter.scheme
+ @connection
end
protected
View
4 lib/twitter/trends.rb
@@ -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
@@ -65,6 +65,8 @@ def connection
end
builder.use Faraday::Response::Mashify
end
+ @connection.scheme = Twitter.scheme
+ @connection
end
end
View
4 lib/twitter/unauthenticated.rb
@@ -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={})
View
4 test/faraday/raise_errors_test.rb
@@ -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
View
2 test/test_helper.rb
@@ -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)
View
2 test/twitter/geo_test.rb
@@ -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
View
28 test/twitter/search_test.rb
@@ -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
@@ -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
@@ -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
@@ -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
View
33 test/twitter_test.rb
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
Owner

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

Please sign in to comment.