Class: Amazon::WebServices::Util::RESTTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon/webservices/util/rest_transport.rb

Constant Summary collapse

REQUIRED_PARAMETERS =
[:Endpoint]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ RESTTransport

Returns a new instance of RESTTransport.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/amazon/webservices/util/rest_transport.rb', line 21

def initialize( args )
  missing_parameters = REQUIRED_PARAMETERS - args.keys
  raise "Missing paramters: #{missing_parameters.join(',')}" unless missing_parameters.empty?
  @url = args[:Endpoint]
  @httpMethod = resolveHTTPMethod( args[:RestStyle] )
  @version = args[:Version]

  agent = RubyAWS::agent( args[:SoftwareName] )
  @headers = {
    'User-Agent' => agent,
    'X-Amazon-Software' => agent,
    'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/amazon/webservices/util/rest_transport.rb', line 48

def method_missing( method, *args )
  params = { :Operation => method, :Version => @version }
  params.merge!( args[0].delete( :Request )[0] )
  params.merge!( args[0] )
  res = nil
  if @httpMethod == :GET
    url = URI.parse( @url + toQueryString(params) )
    res = Net::HTTP.new( url.host, url.port ).start { |http|
      http.get( url.request_uri, @headers )
    }.body
  else
    url = URI.parse( @url )
    res = Net::HTTP.new( url.host, url.port ).start { |http|
      req = Net::HTTP::Post.new( url.path, @headers )
      req.form_data = toPostParams( params )
      req['Content-Type'] = @headers['Content-Type'] # necessary because req.form_data resets Content-Type header
      http.request( req )
    }.body
  end
  xml = REXML::Document.new( res )
  XMLSimplifier.simplify xml
end

Class Method Details

.canPost?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/amazon/webservices/util/rest_transport.rb', line 17

def self.canPost?
  Net::HTTP.respond_to? :post_form
end

Instance Method Details

#resolveHTTPMethod(method) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/amazon/webservices/util/rest_transport.rb', line 36

def resolveHTTPMethod( method )
  case method.to_s.upcase
  when "GET"
    return :GET
  when "POST"
    raise "Your version of Ruby does not support HTTP Post" unless RESTTransport.canPost?
    return :POST
  else
    return ( RESTTransport.canPost? ? :POST : :GET )
  end
end