diff --git a/lib/mws.rb b/lib/mws.rb index 06e9ade..135b2ce 100644 --- a/lib/mws.rb +++ b/lib/mws.rb @@ -24,30 +24,17 @@ require_relative 'mws/api/merchant_fulfillment' module MWS - @@aws_access_key_id = nil - @@aws_secret_access_key = nil - - def self.aws_access_key_id=(key_id) - @@aws_access_key_id = key_id - end - - def self.aws_access_key_id - @@aws_access_key_id - end - - def self.aws_secret_access_key=(secret_key) - @@aws_secret_access_key = secret_key - end - - def self.aws_secret_access_key - @@aws_secret_access_key - end - - def self.new(options = {}) - @connection = MWS::Connection.new(options) - end - - def self.config - yield self + class << self + attr_accessor :aws_access_key_id, + :aws_secret_access_key, + :user_agent + + def config + yield self + end + + def new(options = {}) + MWS::Connection.new(options) + end end end diff --git a/lib/mws/api/base.rb b/lib/mws/api/base.rb index aa2343c..a5dccf2 100644 --- a/lib/mws/api/base.rb +++ b/lib/mws/api/base.rb @@ -6,6 +6,7 @@ class Base # TODO: Temporary solution, move to configuration DEFAULT_TIMEOUT = 2000 + USER_AGENT = 'User-Agent'.freeze def initialize(connection) @verb ||= :get @@ -20,7 +21,10 @@ def call(action, params = {}) when 'GET' HTTParty.get(query.request_uri, http_request_options) when 'POST' - HTTParty.post(query.request_uri, (params[:request_params] || {}).merge(http_request_options)) + HTTParty.post( + query.request_uri, + params.fetch(:request_params, {}).deep_merge(http_request_options) + ) end end @@ -46,9 +50,11 @@ def method_missing(name, *args) end def http_request_options - { - timeout: DEFAULT_TIMEOUT - } + @http_request_options ||= begin + options = { timeout: DEFAULT_TIMEOUT } + options.merge!(headers: { USER_AGENT => MWS.user_agent }) if MWS.user_agent.present? + options + end end end end diff --git a/spec/mws-rb/api/base_spec.rb b/spec/mws-rb/api/base_spec.rb index 3c1150c..f1846ca 100644 --- a/spec/mws-rb/api/base_spec.rb +++ b/spec/mws-rb/api/base_spec.rb @@ -1,6 +1,16 @@ require 'spec_helper' describe MWS::API::Base do + class TestApi < MWS::API::Base + ACTIONS = [:test_action].freeze + + def initialize(connection) + @uri = '/Products/2011-10-01' + @version = '2011-10-01' + super(connection) + end + end + let(:connection) do MWS::Connection.new(aws_access_key_id: 'access key', aws_secret_access_key: 'secret key', @@ -8,6 +18,7 @@ mws_auth_token: 'auth token') end + let(:test_api) { TestApi.new(connection) } let(:base) { MWS::API::Base.new(connection) } it 'should receive a connection object' do @@ -29,20 +40,61 @@ end describe 'method_missing to call actions' do - class TestApi < MWS::API::Base - ACTIONS = [:test_action].freeze - def initialize(connection) - @uri = '/Products/2011-10-01' - @version = '2011-10-01' - super(connection) - end - end - - let(:test_api) { TestApi.new(connection) } before(:each) { allow(HTTParty).to receive(:get).and_return({}) } it 'should raise exception if Actions do not contain the action name' do expect { test_api.action_not_found }.to raise_error(NoMethodError) end end + + context 'user agent' do + subject { test_api.test_action(params) } + + before do |example| + @original_user_agent = MWS.user_agent + MWS.config { |config| config.user_agent = user_agent } + end + + after do + subject + MWS.config { |config| config.user_agent = @original_user_agent } + end + + let(:params) { {} } + + context 'presents in configuration' do + let(:user_agent) { 'My Seller Tool/2.0 (Language=Java/1.6.0.11; Platform=Windows/XP' } + + it 'User-Agent is passed' do + expect(HTTParty).to receive(:get) do |_uri, params| + expect(params[:headers]['User-Agent']).to eq user_agent + end + end + + context 'other header params presents as well' do + let(:params) { { request_params: { headers: { 'Content-Type' => 'text/xml' } } } } + + before { test_api.instance_variable_set(:@verb, :post) } + + it 'merges all header params' do + expect(HTTParty).to receive(:post) do |_uri, params| + params.fetch(:headers).tap do |headers| + expect(headers.fetch('User-Agent')).to eq user_agent + expect(headers.fetch('Content-Type')).to eq 'text/xml' + end + end + end + end + end + + context 'is not present in configuration' do + let(:user_agent) { nil } + + it 'headers are not passed' do + expect(HTTParty).to receive(:get) do |_uri, params| + expect(params.key?(:headers)).to be false + end + end + end + end end