Class: TwitterDispatch::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_dispatch/dispatcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strategy = :none, *args) ⇒ Dispatcher

Initialize a dispatcher using the specified strategy.

Strategies

  • :oauth - You will need to provide four arguments before any options: the consumer key, the consumer secret, the access key, and the access secret.

  • :basic - You will need to provide two arguments before any options: the screen name# and the password of the authenticating user.

  • :none - You will not need to provide any additional arguments but will only be able to access resources that do not require authentication (such as statuses/public_timeline).

Options

  • :base_url - The base URL used to make calls. This way the dispatcher can be used on Twitter-compatible APIs, not just the Twitter API.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/twitter_dispatch/dispatcher.rb', line 27

def initialize(strategy = :none, *args)
  args << {} unless args.last.is_a?(Hash)
  @strategy = strategy
  case @strategy
  when :oauth
    raise ArgumentError, "The :oauth strategy requires four arguments - consumer_key, consumer_secret, access_key and access_secret." unless args.size == 5
    @consumer_key, @consumer_secret, @access_key, @access_secret, @options = *args
  when :basic
    raise ArgumentError, "The :basic strategy requires two arguments - screen_name and password." unless args.size == 3
    @screen_name, @password, @options = *args
  when :none
    raise ArgumentError, "The :none strategy does not take any mandatory arguments, only options." unless args.size == 1
    @options = *args
  else
    raise ArgumentError, "Valid strategies are :oauth, :basic and :none."
  end

  @options = TwitterDispatch::Dispatcher.default_options.merge(@options)
end

Instance Attribute Details

#access_secretObject (readonly)

Returns the value of attribute access_secret.



66
67
68
# File 'lib/twitter_dispatch/dispatcher.rb', line 66

def access_secret
  @access_secret
end

#access_tokenObject (readonly)

Returns the value of attribute access_token.



66
67
68
# File 'lib/twitter_dispatch/dispatcher.rb', line 66

def access_token
  @access_token
end

#passwordObject (readonly)

Returns the value of attribute password.



66
67
68
# File 'lib/twitter_dispatch/dispatcher.rb', line 66

def password
  @password
end

#screen_nameObject (readonly)

Returns the value of attribute screen_name.



66
67
68
# File 'lib/twitter_dispatch/dispatcher.rb', line 66

def screen_name
  @screen_name
end

#strategyObject (readonly)

Returns the value of attribute strategy.



66
67
68
# File 'lib/twitter_dispatch/dispatcher.rb', line 66

def strategy
  @strategy
end

Class Method Details

.default_optionsObject

Override this method to provide new defaults. The defaults are as follows:



8
9
10
11
12
13
# File 'lib/twitter_dispatch/dispatcher.rb', line 8

def self.default_options
  {
    :base_url => 'http://twitter.com',
    :api_timeout => 10
  }
end

Instance Method Details

#base_urlObject



68
69
70
# File 'lib/twitter_dispatch/dispatcher.rb', line 68

def base_url
  @options[:base_url]
end

#basic?Boolean

True if the current strategy is HTTP Basic.

Returns:

  • (Boolean)


83
84
85
# File 'lib/twitter_dispatch/dispatcher.rb', line 83

def basic?
  strategy == :basic
end

#consumerObject

Generates an OAuth consumer for this dispatcher given the base_url and provided access key and token.



49
50
51
# File 'lib/twitter_dispatch/dispatcher.rb', line 49

def consumer
  OAuth::Consumer.new(@consumer_key, @consumer_secret, :site => @options[:base_url])
end

#delete(*args) ⇒ Object

Perform a DELETE request through the dispatcher.



121
122
123
# File 'lib/twitter_dispatch/dispatcher.rb', line 121

def delete(*args)
  proxy.delete(*args)
end

#get(*args) ⇒ Object

Perform a GET request through the dispatcher.



106
107
108
# File 'lib/twitter_dispatch/dispatcher.rb', line 106

def get(*args)
  proxy.get(*args)
end

#netObject



58
59
60
61
62
63
64
# File 'lib/twitter_dispatch/dispatcher.rb', line 58

def net
  uri = URI.parse(base_url)
  net = Net::HTTP.new(uri.host, uri.port)
  net.use_ssl = uri.scheme == 'https'
  net.read_timeout = @options[:api_timeout]
  net
end

#oauth?Boolean

True if the current strategy is OAuth.

Returns:

  • (Boolean)


78
79
80
# File 'lib/twitter_dispatch/dispatcher.rb', line 78

def oauth?
  strategy == :oauth
end

#path_prefixObject

:nodoc:



54
55
56
# File 'lib/twitter_dispatch/dispatcher.rb', line 54

def path_prefix
  URI.parse(base_url).path
end

#post(*args) ⇒ Object

Perform a POST request through the dispatcher.



111
112
113
# File 'lib/twitter_dispatch/dispatcher.rb', line 111

def post(*args)
  proxy.post(*args)
end

#proxyObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/twitter_dispatch/dispatcher.rb', line 87

def proxy
  case strategy
  when :oauth
    @proxy ||= TwitterDispatch::Proxy::OAuth.new(self)
  when :basic
    @proxy ||= TwitterDispatch::Proxy::Basic.new(self)
  when :none
    @proxy ||= TwitterDispatch::Proxy::None.new(self)
  end     
end

#put(*args) ⇒ Object

Perform a PUT request through the dispatcher.



116
117
118
# File 'lib/twitter_dispatch/dispatcher.rb', line 116

def put(*args)
  proxy.put(*args)
end

#request(http_method, path, *args) ⇒ Object

Make a request through the dispatcher. Method can be any of the standard HTTP verbs, path is the API path such as /account/verify_credentials.json



101
102
103
# File 'lib/twitter_dispatch/dispatcher.rb', line 101

def request(http_method, path, *args)
  proxy.request(http_method, path, *args)
end

#strategy?Boolean

Returns true if a strategy is set.

Returns:

  • (Boolean)


73
74
75
# File 'lib/twitter_dispatch/dispatcher.rb', line 73

def strategy?
  strategy != :none
end