From cd16ee9dd50001512d54db6b6309421ae57f380f Mon Sep 17 00:00:00 2001 From: Jazmin Schroeder Date: Wed, 4 Dec 2013 23:50:50 -0600 Subject: [PATCH] Add SmartPost options --- lib/fedex/label.rb | 4 ++-- lib/fedex/request/base.rb | 7 ++++++- lib/fedex/request/shipment.rb | 13 +++++++++++-- spec/lib/fedex/label_spec.rb | 29 +++++++++++++++++++++++++++-- spec/lib/fedex/shipment_spec.rb | 21 ++++++++++++++++++++- spec/support/vcr.rb | 1 + 6 files changed, 67 insertions(+), 8 deletions(-) diff --git a/lib/fedex/label.rb b/lib/fedex/label.rb index 978dbc6c..808e8165 100644 --- a/lib/fedex/label.rb +++ b/lib/fedex/label.rb @@ -12,7 +12,7 @@ def initialize(label_details = {}) package_details = label_details[:process_shipment_reply][:completed_shipment_detail][:completed_package_details] @options = package_details[:label] @options[:format] = label_details[:format] - @options[:tracking_number] = package_details[:tracking_ids][:tracking_number] + @options[:tracking_number] = [package_details[:tracking_ids]].flatten.first[:tracking_number] @options[:file_name] = label_details[:file_name] @image = Base64.decode64(options[:parts][:image]) if has_image? @@ -53,4 +53,4 @@ def save(path, append_name = true) end end end -end \ No newline at end of file +end diff --git a/lib/fedex/request/base.rb b/lib/fedex/request/base.rb index eba3d6b5..89583eba 100644 --- a/lib/fedex/request/base.rb +++ b/lib/fedex/request/base.rb @@ -44,7 +44,12 @@ class Base def initialize(credentials, options={}) requires!(options, :shipper, :recipient, :packages) @credentials = credentials - @shipper, @recipient, @packages, @service_type, @customs_clearance, @debug = options[:shipper], options[:recipient], options[:packages], options[:service_type], options[:customs_clearance], options[:debug] + @shipper = options[:shipper] + @recipient = options[:recipient] + @packages = options[:packages] + @service_type = options[:service_type] + @customs_clearance = options[:customs_clearance] + @smartpost_details = options[:smartpost_details] @debug = ENV['DEBUG'] == 'true' @shipping_options = options[:shipping_options] ||={} # Expects hash with addr and port diff --git a/lib/fedex/request/shipment.rb b/lib/fedex/request/shipment.rb index 69a24637..67af6925 100644 --- a/lib/fedex/request/shipment.rb +++ b/lib/fedex/request/shipment.rb @@ -46,6 +46,7 @@ def add_requested_shipment(xml) add_shipping_charges_payment(xml) add_special_services(xml) if @shipping_options[:return_reason] add_customs_clearance(xml) if @customs_clearance + add_smartpost_details(xml) if @smartpost_details add_custom_components(xml) xml.RateRequestTypes "ACCOUNT" add_packages(xml) @@ -78,6 +79,14 @@ def add_special_services(xml) } end + def add_smartpost_details(xml) + xml.SmartPostDetail { + xml.Indicia @smartpost_details[:indicia] + xml.AncillaryEndorsement @smartpost_details[:ancillary_endorsement] + xml.HubId @smartpost_details[:hub_id] + } + end + # Callback used after a failed shipment response. def failure_response(api_response, response) error_message = if response[:process_shipment_reply] @@ -96,7 +105,7 @@ def success_response(api_response, response) # Build xml Fedex Web Service request def build_xml builder = Nokogiri::XML::Builder.new do |xml| - xml.ProcessShipmentRequest(:xmlns => "http://fedex.com/ws/ship/v12"){ + xml.ProcessShipmentRequest(:xmlns => "http://fedex.com/ws/ship/v#{service[:version]}"){ add_web_authentication_detail(xml) add_client_detail(xml) add_version(xml) @@ -107,7 +116,7 @@ def build_xml end def service - { :id => 'ship', :version => 12 } + { :id => 'ship', :version => 13 } end # Successful request diff --git a/spec/lib/fedex/label_spec.rb b/spec/lib/fedex/label_spec.rb index b2293b4e..7dfeab57 100644 --- a/spec/lib/fedex/label_spec.rb +++ b/spec/lib/fedex/label_spec.rb @@ -24,7 +24,7 @@ module Fedex let(:label_specification) do { :label_format_type => 'COMMON2D', - :image_type => 'PNG', + :image_type => 'PDF', } end @@ -68,6 +68,31 @@ module Fedex @label.should respond_to('file_name') end end + + describe "smartpost", :vcr do + let(:smartpost_details) do + { :indicia => "PARCEL_SELECT", + :ancillary_endorsement => "RETURN_SERVICE", + :hub_id => "5531"} + end + + let(:options) do + { :shipper => shipper, + :recipient => recipient, + :packages => packages, + :service_type => "SMART_POST", + :label_specification => label_specification, + :smartpost_details => smartpost_details, + :filename => filename + } + end + + it 'creates label' do + label = fedex.label(options) + File.should exist(filename) + end + end + end end -end \ No newline at end of file +end diff --git a/spec/lib/fedex/shipment_spec.rb b/spec/lib/fedex/shipment_spec.rb index c850a9e6..98152e16 100644 --- a/spec/lib/fedex/shipment_spec.rb +++ b/spec/lib/fedex/shipment_spec.rb @@ -22,9 +22,10 @@ { :packaging_type => "YOUR_PACKAGING", :drop_off_type => "REGULAR_PICKUP" } end + let(:filename) { require 'tmpdir' - File.join(Dir.tmpdir, "label#{rand(15000)}.pdf") + p File.join(Dir.tmpdir, "label#{rand(15000)}.pdf") } context "domestic shipment", :vcr do @@ -54,5 +55,23 @@ end + context 'smartpost', :vcr do + let(:smartpost_details) do + { :indicia => "PARCEL_SELECT", + :ancillary_endorsement => "RETURN_SERVICE", + :hub_id => "5531"} + end + + let(:options) do + {:shipper => shipper, :recipient => recipient, :packages => packages, :filename => filename, :service_type => "SMART_POST", :smartpost_details => smartpost_details, debug: true} + end + + it "returns valid shipment" do + shipment = fedex.ship(options) + shipment.class.should_not == Fedex::RateError + end + + end + end end diff --git a/spec/support/vcr.rb b/spec/support/vcr.rb index a7ed69cf..cc60c1b7 100644 --- a/spec/support/vcr.rb +++ b/spec/support/vcr.rb @@ -3,6 +3,7 @@ VCR.configure do |c| c.cassette_library_dir = File.expand_path('../../vcr', __FILE__) c.hook_into :webmock + c.allow_http_connections_when_no_cassette = true end RSpec.configure do |c|