Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jnunemaker/twitter
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Oct 27, 2010
2 parents f6420b5 + d96dea9 commit e40624c
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 252 deletions.
1 change: 1 addition & 0 deletions lib/twitter.rb
@@ -1,5 +1,6 @@
require File.expand_path('../twitter/error', __FILE__)
require File.expand_path('../twitter/configuration', __FILE__)
require File.expand_path('../twitter/api', __FILE__)
require File.expand_path('../twitter/client', __FILE__)
require File.expand_path('../twitter/search', __FILE__)
require File.expand_path('../twitter/base', __FILE__)
Expand Down
20 changes: 20 additions & 0 deletions lib/twitter/api.rb
@@ -0,0 +1,20 @@
require File.expand_path('../connection', __FILE__)
require File.expand_path('../request', __FILE__)
require File.expand_path('../authentication', __FILE__)

module Twitter
class API
attr_accessor *Configuration::VALID_OPTIONS_KEYS

def initialize(options={})
options = Twitter.options.merge(options)
Configuration::VALID_OPTIONS_KEYS.each do |key|
send("#{key}=", options[key])
end
end

include Connection
include Request
include Authentication
end
end
18 changes: 18 additions & 0 deletions lib/twitter/authentication.rb
@@ -0,0 +1,18 @@
module Twitter
module Authentication
private

def authentication
{
:consumer_key => consumer_key,
:consumer_secret => consumer_secret,
:token => oauth_token,
:token_secret => oauth_token_secret
}
end

def authenticated?
authentication.values.all?
end
end
end
19 changes: 6 additions & 13 deletions lib/twitter/client.rb
@@ -1,19 +1,12 @@
Dir[File.expand_path('../client/*.rb', __FILE__)].each{|f| require f}

module Twitter
class Client
attr_accessor *Configuration::VALID_OPTIONS_KEYS
class Client < API
# Require client method modules after initializing the Client class in
# order to avoid a superclass mismatch error, allowing those modules to be
# Client-namespaced.
Dir[File.expand_path('../client/*.rb', __FILE__)].each{|f| require f}

def initialize(options={})
options = Twitter.options.merge(options)
Configuration::VALID_OPTIONS_KEYS.each do |key|
send("#{key}=", options[key])
end
end
alias :api_endpoint :endpoint

include Connection
include Request
include Authentication
include Utils

include Timeline
Expand Down
19 changes: 0 additions & 19 deletions lib/twitter/client/authentication.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/twitter/client/connection.rb

This file was deleted.

40 changes: 0 additions & 40 deletions lib/twitter/client/request.rb

This file was deleted.

23 changes: 13 additions & 10 deletions lib/twitter/configuration.rb
Expand Up @@ -3,12 +3,14 @@

module Twitter
module Configuration
VALID_OPTIONS_KEYS = [:consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret, :adapter, :endpoint, :format, :user_agent].freeze
VALID_FORMATS = [:json, :xml].freeze
DEFAULT_ADAPTER = Faraday.default_adapter.freeze
DEFAULT_ENDPOINT = 'https://api.twitter.com/1/'.freeze
DEFAULT_FORMAT = :json.freeze
DEFAULT_USER_AGENT = "Twitter Ruby Gem #{Twitter::VERSION}".freeze
VALID_OPTIONS_KEYS = [:consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret, :adapter, :endpoint, :search_endpoint, :format, :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_USER_AGENT = "Twitter Ruby Gem #{Twitter::VERSION}".freeze

attr_accessor *VALID_OPTIONS_KEYS

Expand All @@ -25,10 +27,11 @@ def options
end

def reset
self.adapter = DEFAULT_ADAPTER
self.endpoint = DEFAULT_ENDPOINT
self.format = DEFAULT_FORMAT
self.user_agent = DEFAULT_USER_AGENT
self.adapter = DEFAULT_ADAPTER
self.endpoint = DEFAULT_ENDPOINT
self.search_endpoint = DEFAULT_SEARCH_ENDPOINT
self.format = DEFAULT_FORMAT
self.user_agent = DEFAULT_USER_AGENT
end
end
end
26 changes: 26 additions & 0 deletions lib/twitter/connection.rb
@@ -0,0 +1,26 @@
require 'faraday_middleware'
Dir[File.expand_path('../../faraday/*.rb', __FILE__)].each{|f| require f}

module Twitter
module Connection
private

def connection(raw=false)
options = {
:headers => {:user_agent => user_agent},
:ssl => {:verify => false},
: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
end
end
end
end
38 changes: 38 additions & 0 deletions lib/twitter/request.rb
@@ -0,0 +1,38 @@
module Twitter
module Request
def get(path, options={}, raw=false)
request(:get, path, options, raw)
end

def post(path, options={}, raw=false)
request(:post, path, options, raw)
end

def put(path, options={}, raw=false)
request(:put, path, options, raw)
end

def delete(path, options={}, raw=false)
request(:delete, path, options, raw)
end

private

def request(method, path, options, raw)
response = connection(raw).send(method) do |request|
case method
when :get, :delete
request.url(formatted_path(path), options)
when :post, :put
request.path = formatted_path(path)
request.body = options
end
end
raw ? response : response.body
end

def formatted_path(path)
[path, format].compact.join('.')
end
end
end
16 changes: 4 additions & 12 deletions lib/twitter/search.rb
@@ -1,30 +1,22 @@
require 'cgi'
require File.expand_path('../search/request', __FILE__)
Dir[File.expand_path('../search/*.rb', __FILE__)].each{|f| require f}

module Twitter

# Handles the Twitter Search API
#
# @see http://dev.twitter.com/doc/get/search Twitter Search API docs
class Search
attr_accessor *Configuration::VALID_OPTIONS_KEYS
class Search < API
attr_reader :fetch, :query

# Creates a new instance of a search
#
# @param [String] query the optional keyword to search
def initialize(options={})
options = Twitter.options.merge(options)
def initialize(*)
clear
Configuration::VALID_OPTIONS_KEYS.each do |key|
send("#{key}=", options[key])
end
super
end

include Connection
include Request
include Authentication
alias :api_endpoint :search_endpoint

# Clears all the query filters to make a new search
def clear
Expand Down
19 changes: 0 additions & 19 deletions lib/twitter/search/authentication.rb

This file was deleted.

27 changes: 0 additions & 27 deletions lib/twitter/search/connection.rb

This file was deleted.

22 changes: 0 additions & 22 deletions lib/twitter/search/request.rb

This file was deleted.

0 comments on commit e40624c

Please sign in to comment.