Class: Elasticity::AwsRequest
- Inherits:
-
Object
- Object
- Elasticity::AwsRequest
- Defined in:
- lib/elasticity/aws_request.rb
Instance Attribute Summary collapse
-
#access_key ⇒ Object
readonly
Returns the value of attribute access_key.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
-
#secret_key ⇒ Object
readonly
Returns the value of attribute secret_key.
Class Method Summary collapse
-
.aws_escape(param) ⇒ Object
(Used from RightScale’s right_aws gem) Escape a string according to Amazon’s rules.
-
.camelize(word) ⇒ Object
(Used from Rails’ ActiveSupport).
-
.convert_ruby_to_aws(params) ⇒ Object
Since we use the same structure as AWS, we can generate AWS param names from the Ruby versions of those names (and the param nesting).
-
.parse_error_response(error_xml) ⇒ Object
AWS error responses all follow the same form.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(access = nil, secret = nil, options = {}) ⇒ AwsRequest
constructor
Supported values for options: :region - AWS region (e.g. us-west-1) :secure - true or false, default true.
- #submit(ruby_params) ⇒ Object
Constructor Details
#initialize(access = nil, secret = nil, options = {}) ⇒ AwsRequest
Supported values for options:
:region - AWS region (e.g. us-west-1)
:secure - true or false, default true.
16 17 18 19 20 21 |
# File 'lib/elasticity/aws_request.rb', line 16 def initialize(access=nil, secret=nil, ={}) @access_key = get_access_key(access) @secret_key = get_secret_key(secret) @host = "elasticmapreduce.#{{:region => 'us-east-1'}.merge()[:region]}.amazonaws.com" @protocol = {:secure => true}.merge()[:secure] ? 'https' : 'http' end |
Instance Attribute Details
#access_key ⇒ Object (readonly)
Returns the value of attribute access_key.
8 9 10 |
# File 'lib/elasticity/aws_request.rb', line 8 def access_key @access_key end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
10 11 12 |
# File 'lib/elasticity/aws_request.rb', line 10 def host @host end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
11 12 13 |
# File 'lib/elasticity/aws_request.rb', line 11 def protocol @protocol end |
#secret_key ⇒ Object (readonly)
Returns the value of attribute secret_key.
9 10 11 |
# File 'lib/elasticity/aws_request.rb', line 9 def secret_key @secret_key end |
Class Method Details
.aws_escape(param) ⇒ Object
(Used from RightScale’s right_aws gem) Escape a string according to Amazon’s rules. See: docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/index.html?REST_RESTAuth.html
78 79 80 81 82 |
# File 'lib/elasticity/aws_request.rb', line 78 def self.aws_escape(param) param.to_s.gsub(/([^a-zA-Z0-9._~-]+)/n) do '%' + $1.unpack('H2' * $1.size).join('%').upcase end end |
.camelize(word) ⇒ Object
(Used from Rails’ ActiveSupport)
114 115 116 |
# File 'lib/elasticity/aws_request.rb', line 114 def self.camelize(word) word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } end |
.convert_ruby_to_aws(params) ⇒ Object
Since we use the same structure as AWS, we can generate AWS param names from the Ruby versions of those names (and the param nesting).
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/elasticity/aws_request.rb', line 86 def self.convert_ruby_to_aws(params) result = {} params.each do |key, value| case value when Array prefix = "#{camelize(key.to_s)}.member" value.each_with_index do |item, index| if item.is_a?(String) result["#{prefix}.#{index+1}"] = item else convert_ruby_to_aws(item).each do |nested_key, nested_value| result["#{prefix}.#{index+1}.#{nested_key}"] = nested_value end end end when Hash prefix = "#{camelize(key.to_s)}" convert_ruby_to_aws(value).each do |nested_key, nested_value| result["#{prefix}.#{nested_key}"] = nested_value end else result[camelize(key.to_s)] = value end end result end |
.parse_error_response(error_xml) ⇒ Object
AWS error responses all follow the same form. Extract the message from the error document.
120 121 122 123 124 |
# File 'lib/elasticity/aws_request.rb', line 120 def self.parse_error_response(error_xml) xml_doc = Nokogiri::XML(error_xml) xml_doc.remove_namespaces! xml_doc.xpath("/ErrorResponse/Error/Message").text end |
Instance Method Details
#==(other) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/elasticity/aws_request.rb', line 33 def ==(other) return false unless other.is_a? AwsRequest return false unless @access_key == other.access_key return false unless @secret_key == other.secret_key return false unless @host == other.host return false unless @protocol == other.protocol true end |
#submit(ruby_params) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/elasticity/aws_request.rb', line 23 def submit(ruby_params) aws_params = AwsRequest.convert_ruby_to_aws(ruby_params) signed_params = sign_params(aws_params) begin RestClient.post("#@protocol://#@host", signed_params, :content_type => 'application/x-www-form-urlencoded; charset=utf-8') rescue RestClient::BadRequest => e raise ArgumentError, AwsRequest.parse_error_response(e.http_body) end end |