Class: Ramazon::Request

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/ramazon/request.rb

Overview

the object used to prepare and submit requests to amazon

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Request

Returns a new instance of Request.



7
8
9
# File 'lib/ramazon/request.rb', line 7

def initialize(options = {})
  self.options = options.merge(default_options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/ramazon/request.rb', line 5

def options
  @options
end

Instance Method Details

#errorsObject

errors returned from amazon



27
28
29
# File 'lib/ramazon/request.rb', line 27

def errors
  @errors ||= Ramazon::Error.parse(@response.body) || []
end

#paramsObject



64
65
66
67
68
69
70
71
# File 'lib/ramazon/request.rb', line 64

def params
  formatted_params = {}
  self.options.each do |key, value|
    formatted_params[key.to_s.classify] = value.is_a?(Array) ? value.join(",") : value
  end

  formatted_params
end

#signed_urlObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ramazon/request.rb', line 42

def signed_url
  self.options[:timestamp] = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S")
  encoded_param_strings = []
  sorted_params.each do |p|
    encoded_param_strings << "#{p[0]}=#{CGI::escape(p[1].to_s)}"
  end
  string_to_sign = 
"GET
#{Ramazon::Configuration.base_uri.gsub("http://", "")}
#{Ramazon::Configuration.path}
#{encoded_param_strings.join("&")}"

  signature = Ramazon::Signatory.sign(string_to_sign)
  encoded_param_strings << "Signature=" + signature
  
  "#{Ramazon::Configuration.uri}?#{encoded_param_strings.join("&")}"
end

#sorted_paramsObject



60
61
62
# File 'lib/ramazon/request.rb', line 60

def sorted_params
  params.sort {|a, b| a <=> b}
end

#submitObject



11
12
13
14
15
16
17
18
# File 'lib/ramazon/request.rb', line 11

def submit
  @response = self.class.get(signed_url)
  if valid?
    @response.body
  else
    raise self.errors[0], "The following response errors were returned: #{self.errors.collect{|e| e.to_s}}" 
  end
end

#unsigned_pathObject

use this if any type of caching is desired (TBD)



32
33
34
35
36
37
38
39
40
# File 'lib/ramazon/request.rb', line 32

def unsigned_path
  qs = ""

  sorted_params.each do |key, value|
    qs << "&#{key}=#{URI.encode(value.to_s)}"
  end

  qs.size > 0 ? qs[1..qs.size] : qs
end

#valid?Boolean

Error checking for responses (will return true if errors are present)

Returns:

  • (Boolean)


22
23
24
# File 'lib/ramazon/request.rb', line 22

def valid?
  @response && errors.empty?
end