Skip to content

Commit

Permalink
Make all global settings overrideable at the class level
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Oct 18, 2010
1 parent 18594cf commit 66f3ac2
Show file tree
Hide file tree
Showing 13 changed files with 408 additions and 419 deletions.
114 changes: 77 additions & 37 deletions lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,14 @@
require 'faraday/raise_http_5xx'
require 'forwardable'
require 'simple_oauth'
require 'twitter/authenticated'
require 'twitter/geo'
require 'twitter/search'
require 'twitter/trends'
require 'twitter/unauthenticated'

module Twitter

extend SingleForwardable

def_delegators :client, :firehose, :user, :suggestions, :retweeted_to_user, :retweeted_by_user, :status, :friend_ids, :follower_ids, :timeline, :lists_subscribed, :list_timeline, :profile_image

class << self
attr_accessor :consumer_key
attr_accessor :consumer_secret
attr_accessor :access_key
attr_accessor :access_secret
attr_accessor :consumer_key, :consumer_secret, :access_key, :access_secret
def client; Twitter::Unauthenticated.new end

# config/initializers/twitter.rb (for instance)
#
Expand All @@ -31,14 +22,13 @@ class << self
# config.access_key = 'atoken'
# config.access_secret = 'asecret'
# end

def configure
yield self
true
end
end

def client; Twitter::Unauthenticated.new end

module ConfigHelper
def adapter
@adapter ||= Faraday.default_adapter
end
Expand All @@ -47,12 +37,20 @@ def adapter=(value)
@adapter = value
end

def user_agent
@user_agent ||= 'Ruby Twitter Gem'
def api_endpoint
@api_endpoint ||= Addressable::URI.heuristic_parse("api.twitter.com/#{api_version}").to_s
end

def user_agent=(value)
@user_agent = value
def api_endpoint=(value)
@api_endpoint = Addressable::URI.heuristic_parse(value).to_s
end

def api_version
@api_version ||= 1
end

def api_version=(value)
@api_version = value
end

def format
Expand All @@ -71,22 +69,18 @@ def protocol=(value)
@protocol = value
end

def api_endpoint
@api_endpoint ||= Addressable::URI.heuristic_parse("api.twitter.com/#{api_version}").to_s
end

def api_endpoint=(value)
@api_endpoint = Addressable::URI.heuristic_parse(value).to_s
def user_agent
@user_agent ||= 'Ruby Twitter Gem'
end

def api_version
@api_version ||= 1
def user_agent=(value)
@user_agent = value
end
end

def api_version=(value)
@api_version = value
end
extend ConfigHelper

module ConnectionHelper
def connection
builders = []
builders << Faraday::Response::RaiseHttp5xx
Expand All @@ -109,14 +103,56 @@ def connection_with_unparsed_response
end

def connection_with_builders(builders)
headers = {:user_agent => Twitter.user_agent}
headers = {:user_agent => user_agent}
ssl = {:verify => false}
@connection = Faraday::Connection.new(:url => Twitter.api_endpoint, :headers => headers, :ssl => ssl) do |builder|
builder.adapter(@adapter || Faraday.default_adapter)
builders.each do |b| builder.use b end
connection = Faraday::Connection.new(:url => api_endpoint, :headers => headers, :ssl => ssl) do |builder|
builder.adapter(adapter)
builders.each{|b| builder.use b}
end
@connection.scheme = Twitter.protocol
@connection
connection.scheme = protocol
connection
end
end

module RequestHelper
def oauth_header(path, options, method)
oauth_params = {
:consumer_key => @consumer_key,
:consumer_secret => @consumer_secret,
:token => @access_key,
:token_secret => @access_secret
}
SimpleOAuth::Header.new(method, self.class.connection.build_url(path), options, oauth_params).to_s
end

def perform_get(path, options={})
results = self.class.connection.get do |request|
request.url(path, options)
request['Authorization'] = oauth_header(path, options, :get)
end.body
end

def perform_post(path, options={})
results = self.class.connection.post do |request|
request.path = path
request.body = options
request['Authorization'] = oauth_header(path, options, :post)
end.body
end

def perform_put(path, options={})
results = self.class.connection.put do |request|
request.path = path
request.body = options
request['Authorization'] = oauth_header(path, options, :put)
end.body
end

def perform_delete(path, options={})
results = self.class.connection.delete do |request|
request.url(path, options)
request['Authorization'] = oauth_header(path, options, :delete)
end.body
end
end

Expand All @@ -130,7 +166,6 @@ class BadGateway < StandardError; end
class ServiceUnavailable < StandardError; end

class EnhanceYourCalm < StandardError

def initialize(message, http_headers)
@http_headers = Hash[http_headers]
super message
Expand All @@ -141,7 +176,12 @@ def waiting_time
header_value = @http_headers["retry-after"] || @http_headers["Retry-After"]
header_value.to_i
end

end

end

require 'twitter/authenticated'
require 'twitter/geo'
require 'twitter/search'
require 'twitter/trends'
require 'twitter/unauthenticated'
Loading

0 comments on commit 66f3ac2

Please sign in to comment.