Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added exceptions and connection class, an igore as well

  • Loading branch information...
commit dc2060843c8f53ba6939015faaddb8f1b0fac25c 1 parent 4273f08
David Michael authored
View
1  .gitignore
@@ -0,0 +1 @@
+mws.yml
View
2  lib/amazon/mws.rb
@@ -14,6 +14,8 @@
require 'mws/base'
require 'mws/version'
+require 'mws/exceptions'
+require 'mws/connection'
require 'mws/authentication'
Amazon::MWS::Base.class_eval do
View
56 lib/amazon/mws/authentication.rb
@@ -4,6 +4,8 @@ class Authentication
VERSION = 2
class QueryString < String #:nodoc:
+ attr_accessor :request, :access_key_id, :secret_access_key, :merchant_id, :marketplace_id
+
def initialize(request, access_key_id, secret_access_key, merchant_id, marketplace_id, options = {})
super()
@request = request
@@ -11,26 +13,13 @@ def initialize(request, access_key_id, secret_access_key, merchant_id, marketpla
@secret_access_key = secret_access_key
@merchant_id = merchant_id
@marketplace_id = marketplace_id
-
+
self << build
end
- private
+ # private
def build
- required_params.to_query_string
- end
-
- def required_params
- {
- 'AWSAccessKeyId' => @access_key_id,
- 'Marketplace' => @marketplace_id,
- 'Merchant' => @merchant_id,
- 'Signature' => calculate_signature,
- 'SignatureMethod' => Signature::METHOD,
- 'SignatureVersion' => Signature::VERSION,
- 'Timestamp' => date,
- 'Version' => Authentication::VERSION
- }
+ Params.new(self).to_query_string
end
def calculate_signature
@@ -40,8 +29,25 @@ def calculate_signature
def date
@request['date'].to_s.strip.empty? ? Time.now : Time.parse(@request['date'])
end
+
+ class Params < Hash
+ def initialize(query)
+ super()
+ replace(
+ 'AWSAccessKeyId' => query.access_key_id,
+ 'Marketplace' => query.marketplace_id,
+ 'Merchant' => query.merchant_id,
+ 'Signature' => query.calculate_signature,
+ 'SignatureMethod' => Signature::METHOD,
+ 'SignatureVersion' => Signature::VERSION,
+ 'Timestamp' => query.date,
+ 'Version' => Authentication::VERSION
+ )
+ end
+ end
+ # Params
end
- # class QueryString
+ # QueryString
class Signature < String #:nodoc:
VERSION = '2009-01-01'
@@ -49,8 +55,11 @@ class Signature < String #:nodoc:
def initialize(request, secret_access_key)
super()
- @request = request
- @secret_access_key = secret_access_key
+ @request = request
+ @request['Host'] = Amazon::MWS::Base::DEFAULT_HOST
+ @secret_access_key = secret_access_key
+ @uri = URI.parse(@request.path)
+
self << build
end
@@ -75,8 +84,7 @@ def http_verb
end
def host_header
- return "" if @request['Host'].nil?
- @request['Host'].downcase
+ @request['Host']
end
def path
@@ -103,10 +111,8 @@ def path
#
# d. Separate the name-value pairs with an ampersand ( & ) (ASCII code 38).
def canonicalized_query_string
- uri = URI.parse(@request.path)
- query = uri.query
- return "" if query.nil?
- Hash.from_query_string(uri.query).sort.to_query_string
+ return if @uri.query.nil?
+ Hash.from_query_string(@uri.query).sort.to_query_string
end
end
# class Signature
View
44 lib/amazon/mws/connection.rb
@@ -0,0 +1,44 @@
+module Amazon
+ module MWS
+
+ class Connection
+ def initialize(options = {})
+ @options = Options.new(options)
+ end
+
+ def request_method(verb)
+ Net::HTTP.const_get(verb.to_s.capitalize)
+ end
+
+ def query_string_authentication(request, options = {})
+ Authentication::QueryString.new(request, access_key_id, secret_access_key, merchant_id, marketplace_id, options)
+ end
+
+ def url_for(path, options = {})
+ request = request_method(:get).new(path, {})
+ query_string = query_string_authentication(request, options)
+
+ return "#{protocol(options)}#{http.address}#{port_string}#{path}?#{query_string}"
+ end
+
+ class Options < Hash #:nodoc:
+ VALID_OPTIONS = [:access_key_id, :secret_access_key, :merchant_id, :marketplace_id, :server, :use_ssl].freeze
+
+ def initialize(options = {})
+ super()
+ self.validate(options)
+ self.replace(:server => DEFAULT_HOST, :port => (options[:use_ssl] ? 443 : 80))
+ self.merge!(options)
+ end
+
+ def validate(options)
+ invalid_options = options.keys - VALID_OPTIONS
+ raise InvalidConnectionOption.new(invalid_options) unless invalid_options.empty?
+ end
+ end
+ # Options
+ end
+ # Connection
+ end
+end
+
View
22 lib/amazon/mws/exceptions.rb
@@ -0,0 +1,22 @@
+module Amazon
+ module MWS
+
+ # Abstract super class of all Amazon::MWS exceptions
+ class MWSException < StandardError
+ end
+
+ # Abstract super class for all invalid options.
+ class InvalidOption < MWSException
+ end
+
+ # Raised if an unrecognized option is passed when establishing a connection.
+ class InvalidConnectionOption < InvalidOption
+ def initialize(invalid_options)
+ message = "The following connection options are invalid: #{invalid_options.join(', ')}. " +
+ "The valid connection options are: #{Connection::Options::VALID_OPTIONS.join(', ')}."
+ super(message)
+ end
+ end
+
+ end
+end
View
7 lib/amazon/mws/extensions.rb
@@ -18,6 +18,13 @@ def to_query_string
end
end
+class Object
+ def returning(value)
+ yield(value)
+ value
+ end
+end
+
module Kernel
def __method__(depth = 0)
caller[depth][/`([^']+)'/, 1]
Please sign in to comment.
Something went wrong with that request. Please try again.