Class: Amazon::MWS::Connection::RequestBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon/mws/connection/request_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb, path, body = nil) ⇒ RequestBuilder

Returns a new instance of RequestBuilder.



5
6
7
8
9
# File 'lib/amazon/mws/connection/request_builder.rb', line 5

def initialize(verb, path, body = nil)
  # Create the request object
  @request = request_method(verb).new(path)
  process_body(body)
end

Instance Attribute Details

#requestObject

Returns the value of attribute request.



3
4
5
# File 'lib/amazon/mws/connection/request_builder.rb', line 3

def request
  @request
end

Instance Method Details

#add_content_md5(body = "") ⇒ Object



48
49
50
51
# File 'lib/amazon/mws/connection/request_builder.rb', line 48

def add_content_md5(body = "")
  @request['Content-MD5'] = Base64.encode64(create_md5(body)).chomp
  return self # chainable
end

#add_content_typeObject



43
44
45
46
# File 'lib/amazon/mws/connection/request_builder.rb', line 43

def add_content_type
  @request.content_type = "text/html; charset=iso-8859-1"
  return self
end

#add_user_agentObject

For the SubmitFeed (p. 41) function, we require that you pass the Content-MD5 HTTP header, which contains the MD5 hash of the HTTP entity body (see Section 14.15 of RFC 2616, the HTTP/1.1 specification), so we can check if the feed we stored for processing is bit for bit identical with what you sent, protecting you from corrupted descriptive or pricing product data appearing on Amazon.com.

def add_host

@request['Host'] = Amazon::MWS::DEFAULT_HOST
return self

end



38
39
40
41
# File 'lib/amazon/mws/connection/request_builder.rb', line 38

def add_user_agent
  @request['User-Agent'] = "Amazon::MWS (Language=Ruby)"
  return self
end

#create_md5(body) ⇒ Object

think about chaining this with process_body



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/amazon/mws/connection/request_builder.rb', line 54

def create_md5(body)
  md5 = Digest::MD5.new

  # stream from file or in memory?
  if body.respond_to?(:read)
    digest = body.each { |line| md5.update(line) }
  else
    digest = md5.update(body)
  end

  return digest.digest
end

#process_body(body) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/amazon/mws/connection/request_builder.rb', line 15

def process_body(body)
  @request.content_length = 0 and return self if body.nil?

  if body.respond_to?(:read)
    @request.body_stream = body
  else
    @request.body = body
  end

  @request.content_length = body.respond_to?(:lstat) ? body.stat.size : body.size
  return self
end

#request_method(verb) ⇒ Object



11
12
13
# File 'lib/amazon/mws/connection/request_builder.rb', line 11

def request_method(verb)
  Net::HTTP.const_get(verb.to_s.capitalize)
end