diff --git a/lib/ups/parsers/base_parser.rb b/lib/ups/parsers/base_parser.rb index e314d6e..128f275 100644 --- a/lib/ups/parsers/base_parser.rb +++ b/lib/ups/parsers/base_parser.rb @@ -32,6 +32,10 @@ def parsed_response private + def normalize_response_into_array(response_node) + [response_node].flatten + end + def build_error_description(errors_node) return errors_node.last[:ErrorDescription] if errors_node.is_a?(Array) diff --git a/lib/ups/parsers/rates_parser.rb b/lib/ups/parsers/rates_parser.rb index 343cc9b..f532609 100644 --- a/lib/ups/parsers/rates_parser.rb +++ b/lib/ups/parsers/rates_parser.rb @@ -11,7 +11,7 @@ def rated_shipments private def rates - root_response[:RatedShipment] + normalize_response_into_array(root_response[:RatedShipment]) end def root_response diff --git a/spec/stubs/rates_success_single_rate.xml b/spec/stubs/rates_success_single_rate.xml new file mode 100644 index 0000000..952e0fe --- /dev/null +++ b/spec/stubs/rates_success_single_rate.xml @@ -0,0 +1,55 @@ + + + + 1 + Success + + + + 11 + + Your invoice may vary from the displayed reference rates + Ship To Address Classification is changed from Commercial to Residential + + + KGS + + 0.5 + + + GBP + 25.03 + + + GBP + 0.00 + + + GBP + 25.03 + + + + + + + + + + + + + + + + + 0.5 + + + + + + + + + diff --git a/spec/ups/connection/rates_standard_spec.rb b/spec/ups/connection/rates_standard_spec.rb index 29944cc..8df8714 100644 --- a/spec/ups/connection/rates_standard_spec.rb +++ b/spec/ups/connection/rates_standard_spec.rb @@ -68,6 +68,32 @@ } ] end + + describe 'when API responds with a single rate' do + before do + Excon.stub(method: :post) do |params| + case params[:path] + when UPS::Connection::RATE_PATH + { body: File.read("#{stub_path}/rates_success_single_rate.xml"), status: 200 } + end + end + end + + it 'returns rates' do + expect(subject.rated_shipments).wont_be_empty + expect(subject.rated_shipments).must_equal [ + { + :service_code=>"11", + :service_name=>"UPS Standard", + :warnings=>[ + "Your invoice may vary from the displayed reference rates", + "Ship To Address Classification is changed from Commercial to Residential" + ], + :total=>"25.03" + } + ] + end + end end describe 'error rates response' do