/
roaund.rb
68 lines (58 loc) · 1.66 KB
/
roaund.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'cgi'
require 'rest'
class Roaund
autoload :Token, 'roaund/token'
class << self
attr_accessor :temporary_credential_request_url,
:resource_owner_authorization_url,
:token_request_url
end
attr_accessor :consumer_key,
:consumer_token,
:temporary_token
def initialize(config)
@consumer_key, @consumer_token = config[:consumer_key], config[:consumer_token]
end
def initiate
response = REST.post(self.class.temporary_credential_request_url, nil, {
'Authorization' => authorization
})
if response.ok?
@temporary_token = Roaund::Token.load(response.body)
@temporary_token.client = self
end
end
def signature_base
[
request.method,
request.host,
request.path_and_query,
authorization_as_hash.except('oauth_signature'),
request.body_params
].map { |part| CGI.escape(part) }.join('&')
end
def authorization_as_hash
authorization_as_hash = {
'oauth_consumer_key' => @consumer_key,
'oauth_signature_method' => 'HMAC-SHA1',
}
if temporary_token
authorization_as_hash['oauth_token'] = temporary_token.token
end
authorization_as_hash
end
def authorization
"Oauth #{_pairs_to_header_options(authorization_as_hash.merge('Realm' => @realm))}"
end
def token(params_as_string)
token = Roaund::Token.new
token.client = self
token.parse(params_as_string)
token
end
def _pairs_to_header_options(pairs)
encoded = []; for (key, value) in pairs
encoded << "#{CGI.escape(key)}=\"#{CGI.escape(value||'')}\""
end; encoded.join(',')
end
end