Class: OAuth::Client::Helper

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/oauth/client/helper.rb

Instance Method Summary collapse

Methods included from Helper

#escape, #generate_key

Constructor Details

#initialize(request, options = {}) ⇒ Helper

Returns a new instance of Helper.



11
12
13
14
15
# File 'lib/oauth/client/helper.rb', line 11

def initialize(request, options = {})
  @request = request
  @options = options
  @options[:signature_method] ||= 'HMAC-SHA1'
end

Instance Method Details

#generate_timestampObject



29
30
31
# File 'lib/oauth/client/helper.rb', line 29

def generate_timestamp
  Time.now.to_i.to_s
end

#headerObject



57
58
59
60
61
62
63
64
65
# File 'lib/oauth/client/helper.rb', line 57

def header
  parameters = oauth_parameters
  parameters.merge!( { 'oauth_signature' => signature( options.merge({ :parameters => parameters }) ) } )

  header_params_str = parameters.map { |k,v| "#{k}=\"#{escape(v)}\"" }.join(', ')

  realm = "realm=\"#{options[:realm]}\", " if options[:realm]
  "OAuth #{realm}#{header_params_str}"
end

#nonceObject



21
22
23
# File 'lib/oauth/client/helper.rb', line 21

def nonce
  options[:nonce] ||= generate_key
end

#oauth_parametersObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/oauth/client/helper.rb', line 33

def oauth_parameters
  {
    'oauth_consumer_key'     => options[:consumer].key,
    'oauth_token'            => options[:token] ? options[:token].token : '',
    'oauth_signature_method' => options[:signature_method],
    'oauth_timestamp'        => timestamp,
    'oauth_nonce'            => nonce,
    'oauth_version'          => '1.0'
  }.reject { |k,v| v == "" }
end

#optionsObject



17
18
19
# File 'lib/oauth/client/helper.rb', line 17

def options
  @options
end

#parametersObject



67
68
69
# File 'lib/oauth/client/helper.rb', line 67

def parameters
  OAuth::RequestProxy.proxy(@request).parameters
end

#parameters_with_oauthObject



71
72
73
# File 'lib/oauth/client/helper.rb', line 71

def parameters_with_oauth
  oauth_parameters.merge( parameters )
end

#signature(extra_options = {}) ⇒ Object



44
45
46
47
48
# File 'lib/oauth/client/helper.rb', line 44

def signature(extra_options = {})
  OAuth::Signature.sign(@request, { :uri      => options[:request_uri],
                                                :consumer => options[:consumer],
                                                :token    => options[:token] }.merge(extra_options) )
end

#signature_base_string(extra_options = {}) ⇒ Object



50
51
52
53
54
55
# File 'lib/oauth/client/helper.rb', line 50

def signature_base_string(extra_options = {})
  OAuth::Signature.signature_base_string(@request, { :uri      => options[:request_uri],
                                                :consumer => options[:consumer],
                                                :token    => options[:token],
                                                :parameters => oauth_parameters}.merge(extra_options) )
end

#timestampObject



25
26
27
# File 'lib/oauth/client/helper.rb', line 25

def timestamp
  options[:timestamp] ||= generate_timestamp
end