Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SmartPost options #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/fedex/label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -53,4 +53,4 @@ def save(path, append_name = true)
end
end
end
end
end
7 changes: 6 additions & 1 deletion lib/fedex/request/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions lib/fedex/request/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand All @@ -107,7 +116,7 @@ def build_xml
end

def service
{ :id => 'ship', :version => 12 }
{ :id => 'ship', :version => 13 }
end

# Successful request
Expand Down
29 changes: 27 additions & 2 deletions spec/lib/fedex/label_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Fedex

let(:label_specification) do
{ :label_format_type => 'COMMON2D',
:image_type => 'PNG',
:image_type => 'PDF',
}
end

Expand Down Expand Up @@ -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
end
21 changes: 20 additions & 1 deletion spec/lib/fedex/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions spec/support/vcr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down