Class: Ramazon::Request
- Inherits:
-
Object
- Object
- Ramazon::Request
- Includes:
- HTTParty
- Defined in:
- lib/ramazon/request.rb
Overview
the object used to prepare and submit requests to amazon
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#errors ⇒ Object
errors returned from amazon.
-
#initialize(options = {}) ⇒ Request
constructor
A new instance of Request.
- #params ⇒ Object
- #signed_url ⇒ Object
- #sorted_params ⇒ Object
- #submit ⇒ Object
-
#unsigned_path ⇒ Object
use this if any type of caching is desired (TBD).
-
#valid? ⇒ Boolean
Error checking for responses (will return true if errors are present).
Constructor Details
#initialize(options = {}) ⇒ Request
Returns a new instance of Request.
7 8 9 |
# File 'lib/ramazon/request.rb', line 7 def initialize( = {}) self. = .merge() end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/ramazon/request.rb', line 5 def @options end |
Instance Method Details
#errors ⇒ Object
errors returned from amazon
27 28 29 |
# File 'lib/ramazon/request.rb', line 27 def errors @errors ||= Ramazon::Error.parse(@response.body) || [] end |
#params ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/ramazon/request.rb', line 64 def params formatted_params = {} self..each do |key, value| formatted_params[key.to_s.classify] = value.is_a?(Array) ? value.join(",") : value end formatted_params end |
#signed_url ⇒ Object
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.[: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_params ⇒ Object
60 61 62 |
# File 'lib/ramazon/request.rb', line 60 def sorted_params params.sort {|a, b| a <=> b} end |
#submit ⇒ Object
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_path ⇒ Object
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)
22 23 24 |
# File 'lib/ramazon/request.rb', line 22 def valid? @response && errors.empty? end |