Class: SimpleQS::Request::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_qs/request/base.rb

Direct Known Subclasses

Get, Post

Constant Summary collapse

RESERVED_CHARACTERS =
/[^a-zA-Z0-9\-\.\_\~]/
SIGNATURE_VERSION =
2
SIGNATURE_METHOD =
'HmacSHA1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



14
15
16
# File 'lib/simple_qs/request/base.rb', line 14

def initialize(params = {})
  self.query_params = params
end

Instance Attribute Details

#query_paramsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/simple_qs/request/base.rb', line 33

def query_params
  @query_params ||= {}
  @query_params = {
    'SignatureVersion'    => SIGNATURE_VERSION,
    'SignatureMethod'     => SIGNATURE_METHOD,
    'Version'             => SimpleQS::API_VERSION,
    'Timestamp'           => timestamp,
    'AWSAccessKeyId'      => SimpleQS.access_key_id
  }.merge(@query_params)
  @query_params.delete('Timestamp') if @query_params['Expires']
  
  @query_params
end

Class Method Details

.http_method(value = nil) ⇒ Object



91
92
93
94
# File 'lib/simple_qs/request/base.rb', line 91

def http_method(value = nil)
  @http_method = value if value
  @http_method
end

Instance Method Details

#==(other) ⇒ Object



18
19
20
21
22
# File 'lib/simple_qs/request/base.rb', line 18

def ==(other)
  self.class.http_method == other.class.http_method\
    && query_params == other.query_params\
    && query_string == other.query_string
end

#canonical_query_stringObject

Canonicalizes query string



62
63
64
# File 'lib/simple_qs/request/base.rb', line 62

def canonical_query_string
  params_to_query(query_params.sort)
end

#params_to_query(params) ⇒ Object



86
87
88
# File 'lib/simple_qs/request/base.rb', line 86

def params_to_query(params)
  params.map {|pair| pair.map {|value| URI.escape(value.to_s, RESERVED_CHARACTERS)}.join('=')}.join('&')
end

#query_stringObject



52
53
54
# File 'lib/simple_qs/request/base.rb', line 52

def query_string
  @query_string ||= "/"
end

#query_string=(value) ⇒ Object



56
57
58
59
# File 'lib/simple_qs/request/base.rb', line 56

def query_string=(value)
  value = value.join('/') if value.kind_of?(Array)
  @query_string = (value =~ /^\// ? value : "/#{value}")
end

#sign!Object



79
80
81
82
83
84
# File 'lib/simple_qs/request/base.rb', line 79

def sign!
  update_query_params({
    'Signature'   => Base64.encode64(HMAC::SHA1.digest(SimpleQS.secret_access_key, signature_base_string)).chomp
  })
  self
end

#signature_base_stringObject



66
67
68
69
70
71
72
73
# File 'lib/simple_qs/request/base.rb', line 66

def signature_base_string
  [
    self.class.http_method.to_s.upcase,
    SimpleQS.host.downcase,
    query_string,
    canonical_query_string
  ].join("\n")
end

#timestampObject



24
25
26
# File 'lib/simple_qs/request/base.rb', line 24

def timestamp
  @timestamp ||= _timestamp
end

#timestamp=(time) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
# File 'lib/simple_qs/request/base.rb', line 28

def timestamp=(time)
  raise ArgumentError, "expected Time object, bug got #{time.class.to_s} instead." unless time.kind_of?(Time)
  @timestamp = time.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
end

#update_query_params(params) ⇒ Object



48
49
50
# File 'lib/simple_qs/request/base.rb', line 48

def update_query_params(params)
  @query_params = query_params.merge params
end

#uri(with_query_params = false) ⇒ Object



75
76
77
# File 'lib/simple_qs/request/base.rb', line 75

def uri(with_query_params = false)
  "http://#{SimpleQS.host}#{query_string}" << (with_query_params ? "?#{params_to_query(query_params)}" : '')
end