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.



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

def initialize( args )
  missing_parameters = REQUIRED_PARAMETERS - args.keys
  raise "Missing paramters: #{missing_parameters.join(',')}" unless missing_parameters.empty?
  @uri = URI.parse( args[:Endpoint] )
  @httpMethod = resolveHTTPMethod( args[:RestStyle] )
  @version = args[:Version]
  @ssl = (@uri.scheme == 'https') || (@uri.port == 443) || args[:UseSSL]
  @skip_ssl_verify = args[:SkipSSLCheck]

  agent = ::MTurk::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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/amazon/webservices/util/rest_transport.rb', line 51

def method_missing( method, *args )
  params = { :Operation => method, :Version => @version }
  params.merge!( args[0].delete( :Request )[0] )
  params.merge!( args[0] )

  http = Net::HTTP.new( @uri.host, @uri.port )
  if @ssl
    http.use_ssl = true
    if @skip_ssl_verify
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    else
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    end
  end

  req = nil
  if @httpMethod == :GET
    req = Net::HTTP::Get.new( @uri.request_uri + toQueryString(params), @headers )
  else
    req = Net::HTTP::Post.new( @uri.request_uri, @headers )
    req.form_data = toPostParams( params )
    req['Content-Type'] = @headers['Content-Type'] # necessary because req.form_data resets Content-Type header
  end

  res = http.start { |conn|
    conn.request(req)
  }.body

  xml = Nokogiri::XML( res )
  XMLSimplifier.simplify xml
end

Class Method Details

.canPost?Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#resolveHTTPMethod(method) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/amazon/webservices/util/rest_transport.rb', line 39

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