Class: Hungrytable::RequestHeader

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

Constant Summary collapse

ATTRIBUTE_KEYS =
%w(consumer_key nonce signature_method timestamp token version).map(&:to_sym)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, url, params, oauth = {}) ⇒ RequestHeader

Returns a new instance of RequestHeader.



29
30
31
32
33
34
35
36
37
# File 'lib/hungrytable/request_header.rb', line 29

def initialize(method, url, params, oauth = {})
  @method = method.to_s.upcase
  @uri = URI.parse(url.to_s)
  @uri.scheme = @uri.scheme.downcase
  @uri.normalize!
  @uri.fragment = nil
  @params = params
  @options =  self.class.default_options.merge(oauth) 
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



27
28
29
# File 'lib/hungrytable/request_header.rb', line 27

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



27
28
29
# File 'lib/hungrytable/request_header.rb', line 27

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



27
28
29
# File 'lib/hungrytable/request_header.rb', line 27

def params
  @params
end

Class Method Details

.decode(value) ⇒ Object



23
24
25
# File 'lib/hungrytable/request_header.rb', line 23

def self.decode(value)
  URI.decode(value.to_s)
end

.default_optionsObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hungrytable/request_header.rb', line 7

def self.default_options
  {
    :nonce => OpenSSL::Random.random_bytes(16).unpack('H*')[0],
    :signature_method => 'HMAC-SHA1',
    :timestamp => Time.now.to_i.to_s,
    :version => '1.0',
    :consumer_key => Hungrytable::Config.oauth_key,
    :consumer_secret => Hungrytable::Config.oauth_secret,
    :token => ''
  }
end

.encode(value) ⇒ Object



19
20
21
# File 'lib/hungrytable/request_header.rb', line 19

def self.encode(value)
  URI.encode(value.to_s, /[^a-z0-9\-\.\_\~]/i)
end

Instance Method Details

#signed_attributesObject



57
58
59
# File 'lib/hungrytable/request_header.rb', line 57

def signed_attributes
  attributes.merge(:oauth_signature => signature)
end

#to_sObject



45
46
47
# File 'lib/hungrytable/request_header.rb', line 45

def to_s
  %Q(OAuth realm="http://www.opentable.com/", #{normalized_attributes})
end

#urlObject



39
40
41
42
43
# File 'lib/hungrytable/request_header.rb', line 39

def url
  uri = @uri.dup
  uri.query = nil
  uri.to_s
end

#valid?(secrets = {}) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/hungrytable/request_header.rb', line 49

def valid?(secrets = {})
  original_options = options.dup
  options.merge!(secrets)
  valid = options[:signature] == signature
  options.replace(original_options)
  valid
end