Class: Rubizon::AbstractSig2Product

Inherits:
Object
  • Object
show all
Defined in:
lib/rubizon/abstract_sig2_product.rb

Overview

An abstract representation of an AWS product whose REST API uses signature version 2. This class provides a foundation for classes that represent specific AWS products.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specs = {}) ⇒ AbstractSig2Product

Initialization

specs - A Hash containing specifications for the product.

:scheme - (optional) Default scheme is https.  
          May be set to "http".
:host   - (conditionally required) If an ARN is not
          specified or if the host cannot be properly
          deduced from the ARN, :host must be specified.
          If specified, this will override any host name
          that might be deduced from the ARN.
:ARN    - (optional) An ARN may be specified instead of a
          host.  The host can be deduced from the ARN.
:path   - (optional) Default path is '/'.  May be set to
          a path that applies to all requests for the 
          product.  Additional path elements may be appended
          if needed by an operation or subject of that
          operation
:URL    - (optional) A URL may be specified instead of the
          individual scheme, host and path elements.  If
          a URL is specified, it will override the individual
          elements.
:_omit  - (optional) An array containing a list of elements
          that are not to be included in the query string.
          This, for example, can be used in the Product
          Advertising API to suppress the SignatureMethod
          and SignatureVersion parameter/value pairs which
          result from the signing process but are not
          supported in that API
(other) - (optional) Other key/value pairs may be specified.
          These will be included in any query string 
          generated for this product


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubizon/abstract_sig2_product.rb', line 39

def initialize(specs={})
  @scheme= (specs.delete(:scheme) || specs.delete('scheme') || 'https').to_s
  @arn= specs.delete(:ARN) || specs.delete('ARN') || specs.delete(:arn) || specs.delete('arn')
  @host= specs.delete(:host) || specs.delete('host')
  @url= specs.delete(:URL) || specs.delete('URL') || specs.delete(:url) || specs.delete('url')
  @path= specs.delete(:path) || specs.delete('path') || '/'
  if (@url)
    require 'uri'
    url = URI.parse(@url)
    @scheme= url.scheme
    @host= url.host
    @path= url.path
  end
  if @arn && !@host
    @host= self.class.host_from_ARN(@arn)
  end
  if !@host
    raise InvalidParameterError, 'No host was specified and one could not be deduced from arn specifications'
  end
  @query_elements= specs
  @query_elements['SignatureMethod']= 'HmacSHA256'
  @query_elements['SignatureVersion']= 2
  @query_elements['arn']= @arn if @arn
end

Instance Attribute Details

#arnObject (readonly)

Returns the ARN (Amazon Resource Name) served by this object. Returns nil if an ARN is not defined.



82
83
84
# File 'lib/rubizon/abstract_sig2_product.rb', line 82

def arn
  @arn
end

#hostObject (readonly)

Returns the host (domain and subdomains) of the product served by this object.



86
87
88
# File 'lib/rubizon/abstract_sig2_product.rb', line 86

def host
  @host
end

#pathObject (readonly)

Returns the path part of the URL of the product served by this object, typically ‘/’.



90
91
92
# File 'lib/rubizon/abstract_sig2_product.rb', line 90

def path
  @path
end

#query_elementsObject (readonly)

Returns a Hash containing elements to be included in any query string generated for this product. To this will be added elements identifying the specific action, the subject of the action, the access key, and elements related to signing the request.



96
97
98
# File 'lib/rubizon/abstract_sig2_product.rb', line 96

def query_elements
  @query_elements
end

#schemeObject (readonly)

Returns the URL scheme, such as http or https



78
79
80
# File 'lib/rubizon/abstract_sig2_product.rb', line 78

def scheme
  @scheme
end

Class Method Details

.host_from_ARN(arn) ⇒ Object

Default method for calculating the name of the host associated with a given Amazon Resource Name (ARN). This may vary from product to product, so any product with a different mapping from ARN to hostname should override this method.

arn - A String containing an Amazon Resource Name (ARN).

Returns the name of the host hosting the named resource.



72
73
74
75
# File 'lib/rubizon/abstract_sig2_product.rb', line 72

def self.host_from_ARN(arn)
  elems= arn.split(':',5)
  "#{elems[2]}.#{elems[3]}.amazonaws.com"
end

Instance Method Details

#create_request(credentials) ⇒ Object

Create a Request object that can be used to formulate a single request for this product.

Returns an instance of Request



110
111
112
# File 'lib/rubizon/abstract_sig2_product.rb', line 110

def create_request(credentials)
  Request.new(credentials,@scheme,@host,@path,@query_elements)
end

#endpointObject

Returns the product’s endpoint. The endpoint is that part of a URL that includes the scheme, host, and path, but not the query string. An action may extend the product’s path, but otherwise would retain the rest of the endpoint.



102
103
104
# File 'lib/rubizon/abstract_sig2_product.rb', line 102

def endpoint
  "#{@scheme}://#{@host}#{@path}"
end