Class: TwitterRequestHeaders

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

Constant Summary collapse

OAUTH_VERSION =
'1.0'
OAUTH_CIPHER =
'HMAC-SHA1'
TWITTER_API =
'https://api.twitter.com/1.1'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oauth_token, oauth_secret, request_verb, request_path, request_params = nil) ⇒ TwitterRequestHeaders

Creates new session.

Parameters:

  • oauth_token (String)

    Twitter OAuth Key

  • oauth_secret (String)

    Twitter OAuth Secret

  • request_verb (String)

    HTTP verb: GET, POST, e.g.

  • request_path (String)

    HTTP request path. Like /users/blah/blah

  • request_params (Hash, nil) (defaults to: nil)

    HTTP request query. Like 111222333



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/twitter_request_headers.rb', line 42

def initialize(oauth_token, oauth_secret, request_verb, request_path, request_params = nil)
  unless @@configured
    fail StandardError, "Call #{self.class}.configure first!"
  end

  @oauth_token = oauth_token
  @oauth_secret = oauth_secret

  @request_verb = request_verb
  @request_path = request_path
  @request_params = request_params || {}

  @nonce = nonce
  @epochtime = epochtime
end

Class Attribute Details

.consumer_keyObject (readonly)

Returns the value of attribute consumer_key.



10
11
12
# File 'lib/twitter_request_headers.rb', line 10

def consumer_key
  @consumer_key
end

.consumer_secretObject (readonly)

Returns the value of attribute consumer_secret.



10
11
12
# File 'lib/twitter_request_headers.rb', line 10

def consumer_secret
  @consumer_secret
end

.oauth_cipherObject (readonly)

Returns the value of attribute oauth_cipher.



10
11
12
# File 'lib/twitter_request_headers.rb', line 10

def oauth_cipher
  @oauth_cipher
end

.oauth_versionObject (readonly)

Returns the value of attribute oauth_version.



10
11
12
# File 'lib/twitter_request_headers.rb', line 10

def oauth_version
  @oauth_version
end

.twitter_apiObject (readonly)

Returns the value of attribute twitter_api.



10
11
12
# File 'lib/twitter_request_headers.rb', line 10

def twitter_api
  @twitter_api
end

Class Method Details

.configure(consumer_key, consumer_secret, oauth_version = nil, oauth_cipher = nil, twitter_api = nil) ⇒ true

Define Twitter application credentials and options to be accessible from rest of all library.

Parameters:

  • consumer_key (String)

    Application ID/Key

  • consumer_secret (String)

    Application Secret

  • oauth_version (String, nil) (defaults to: nil)

    OAuth version used by Twitter. See dashboard.

  • oauth_cipher (String, nil) (defaults to: nil)

    OAuth chipher used by Twitter. See API docs.

  • twitter_api (String, nil) (defaults to: nil)

    Twitter API address. See dashboard.

Returns:

  • (true)


22
23
24
25
26
27
28
29
30
31
# File 'lib/twitter_request_headers.rb', line 22

def configure(consumer_key, consumer_secret, oauth_version = nil, oauth_cipher = nil, twitter_api = nil)
  @consumer_key = consumer_key
  @consumer_secret = consumer_secret

  @oauth_version = oauth_version || OAUTH_VERSION
  @oauth_cipher = oauth_cipher || OAUTH_CIPHER
  @twitter_api = twitter_api || TWITTER_API

  @@configured = true
end

Instance Method Details

#headerHash

HTTP header of format: => ‘OAuth …’

Returns:

  • (Hash)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/twitter_request_headers.rb', line 63

def header
  signature = Signature.new(
    @oauth_token,
    @oauth_secret,
    @request_verb,
    @request_path,
    @request_params,
    @nonce,
    @epochtime
  ).digest

  {
    Header.key => Header.new(@oauth_token, signature, @nonce, @epochtime).value
  }
end