Class: Nervion::OAuthHeader

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

Constant Summary collapse

PARAMETERS_INCLUDED =
%w{ oauth_consumer_key
oauth_nonce
oauth_signature
oauth_signature_method
oauth_timestamp
oauth_token
oauth_version }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_method, base_url, params, oauth_params) ⇒ OAuthHeader

Returns a new instance of OAuthHeader.



13
14
15
16
# File 'lib/nervion/oauth_header.rb', line 13

def initialize(http_method, base_url, params, oauth_params)
  @http_method, @base_url = http_method, base_url
  @params, @oauth_params = params, oauth_params
end

Class Method Details

.for(request) ⇒ Object



9
10
11
# File 'lib/nervion/oauth_header.rb', line 9

def self.for(request)
  new(request.http_method, request.uri, request.params, request.oauth_params).to_s
end

Instance Method Details

#consumer_keyObject



33
34
35
# File 'lib/nervion/oauth_header.rb', line 33

def consumer_key
  @oauth_params[:consumer_key]
end

#consumer_secretObject



37
38
39
# File 'lib/nervion/oauth_header.rb', line 37

def consumer_secret
  @oauth_params[:consumer_secret]
end

#nonceObject



49
50
51
# File 'lib/nervion/oauth_header.rb', line 49

def nonce
  Digest::MD5.hexdigest "#{consumer_key}#{token}#{timestamp}"
end

#oauth_infoObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/nervion/oauth_header.rb', line 69

def oauth_info
  {
    oauth_consumer_key: consumer_key,
    oauth_nonce: nonce,
    oauth_signature_method: signature_method,
    oauth_timestamp: timestamp,
    oauth_token: token,
    oauth_version: version
  }
end

#secretsObject



80
81
82
83
84
85
# File 'lib/nervion/oauth_header.rb', line 80

def secrets
  {
    consumer_secret: consumer_secret,
    access_token_secret: token_secret
  }
end

#signatureObject



65
66
67
# File 'lib/nervion/oauth_header.rb', line 65

def signature
  OAuthSignature.for @http_method, @base_url, @params, oauth_info, secrets
end

#signature_methodObject



57
58
59
# File 'lib/nervion/oauth_header.rb', line 57

def signature_method
  'HMAC-SHA1'
end

#timestampObject



53
54
55
# File 'lib/nervion/oauth_header.rb', line 53

def timestamp
  Time.now.to_i.to_s
end

#to_sObject



26
27
28
29
30
31
# File 'lib/nervion/oauth_header.rb', line 26

def to_s
  'OAuth ' << PARAMETERS_INCLUDED.map do |param|
    method_name = param.gsub /^oauth_/, ''
    "#{param}=\"#{PercentEncoder.encode(send(method_name))}\""
  end.join(', ')
end

#tokenObject



41
42
43
# File 'lib/nervion/oauth_header.rb', line 41

def token
  @oauth_params[:access_token]
end

#token_secretObject



45
46
47
# File 'lib/nervion/oauth_header.rb', line 45

def token_secret
  @oauth_params[:access_token_secret]
end

#versionObject



61
62
63
# File 'lib/nervion/oauth_header.rb', line 61

def version
  '1.0'
end