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.



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

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.



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

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

Class Method Details

.decode(value) ⇒ Object



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

def self.decode(value)
  CGI.unescape(value.to_s)
end

.default_optionsObject



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

def self.default_options
  {
    nonce: OpenSSL::Random.random_bytes(16).unpack1('H*'),
    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



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

def self.encode(value)
  CGI.escape(value.to_s).gsub('+', '%20').gsub('%7E', '~')
end

Instance Method Details

#signed_attributesObject



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

def signed_attributes
  attributes.merge(oauth_signature: signature)
end

#to_sObject



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

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

#urlObject



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

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

#valid?(secrets = {}) ⇒ Boolean

Returns:

  • (Boolean)


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

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