Skip to content

Commit

Permalink
Move add_package and packaging_type to each builder to resolve slight…
Browse files Browse the repository at this point in the history
… differences
  • Loading branch information
chapmankyle committed Apr 26, 2024
1 parent 27cb818 commit 13d60ae
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 30 deletions.
35 changes: 6 additions & 29 deletions lib/ups/builders/builder_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,28 +103,8 @@ def add_ship_from(opts = {})
shipment_root.merge!(OrganisationBuilder.new('ShipFrom', opts).as_json)
end

# Adds a Package section to the JSON body being built
# Adds a specific packaging type to the shipment
#
# @param [Hash] opts A Hash of data to build the requested section
# @return [void]
def add_package(opts = {})
if shipment_root['Package'].nil?
shipment_root['Package'] = []
end

item = {}
item.merge!(packaging_type(opts[:packaging_type] || customer_supplied_packaging))
item.merge!(element_with_value('Description',
opts[:description] || 'Rate'))
item.merge!(package_weight(opts[:weight], opts[:unit]))

if opts[:dimensions]
item.merge!(package_dimensions(opts[:dimensions]))
end

shipment_root['Package'] << item
end

def customer_supplied_packaging
{ code: '02', description: 'Customer Supplied Package' }
end
Expand Down Expand Up @@ -163,19 +143,16 @@ def as_json
all = element_with_value(name, {})
all[name].merge!(root)

shipment_root.merge!({ 'ShipmentServiceOptions' => shipment_service_options })
all[name]['Shipment'] = shipment_root
if shipment_service_options.size > 0
shipment_root.merge!({ 'ShipmentServiceOptions' => shipment_service_options })
end

all[name]['Shipment'] = shipment_root
all
end

private

def packaging_type(packaging_options_hash)
code_description 'Packaging', packaging_options_hash[:code],
packaging_options_hash[:description]
end

def package_weight(weight, unit)
pkg_weight = element_with_value('PackageWeight', {})
pkg_weight['PackageWeight'].merge!(unit_of_measurement(unit),
Expand All @@ -199,7 +176,7 @@ def package_dimensions(dimensions)
end

def unit_of_measurement(unit)
multi_valued('UnitOfMeasurement', 'Code' => unit.to_s)
multi_valued('UnitOfMeasurement', 'Code' => unit.to_s, 'Description' => unit.to_s)
end

def element_with_value(name, value)
Expand Down
27 changes: 27 additions & 0 deletions lib/ups/builders/rate_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ def initialize
add_request 'Shop'
end

# Adds a Package section to the JSON body being built
#
# @param [Hash] opts A Hash of data to build the requested section
# @return [void]
def add_package(opts = {})
if shipment_root['Package'].nil?
shipment_root['Package'] = []
end

item = {}
item.merge!(packaging_type(opts[:packaging_type] || customer_supplied_packaging))
item.merge!(package_weight(opts[:weight], opts[:unit]))

if opts[:dimensions]
item.merge!(package_dimensions(opts[:dimensions]))
end

shipment_root['Package'] << item
end

# Adds ReturnService to JSON body being built
#
# @param [String] service_code The code for Return Service type
Expand All @@ -24,6 +44,13 @@ def add_return_service(service_code, service_description = '')
service_code,
service_description))
end

private

def packaging_type(packaging_options_hash)
code_description 'PackagingType', packaging_options_hash[:code],
packaging_options_hash[:description]
end
end
end
end
26 changes: 26 additions & 0 deletions lib/ups/builders/ship_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ def initialize
add_request 'validate'
end

# Adds a Package section to the JSON body being built
#
# @param [Hash] opts A Hash of data to build the requested section
# @return [void]
def add_package(opts = {})
if shipment_root['Package'].nil?
shipment_root['Package'] = []
end

item = {}
item.merge!(packaging_type(opts[:packaging_type] || customer_supplied_packaging))
item.merge!(element_with_value('Description', opts[:description] || ''))
item.merge!(package_weight(opts[:weight], opts[:unit]))

if opts[:dimensions]
item.merge!(package_dimensions(opts[:dimensions]))
end

shipment_root['Package'] << item
end

# Adds a LabelSpecification section to the JSON body being built
#
# @return [void]
Expand Down Expand Up @@ -112,6 +133,11 @@ def version_string
"RubyUPS/#{UPS::Version::STRING}"
end

def packaging_type(packaging_options_hash)
code_description 'Packaging', packaging_options_hash[:code],
packaging_options_hash[:description]
end

def label_image_format(format)
code_description 'LabelImageFormat', format.to_s, format.to_s
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ups/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Connection
TRACK_VERSION = 'v1'
LABEL_VERSION = 'v1'

RATE_PATH = "/api/rating/#{RATE_VERSION}/Rate"
RATE_PATH = "/api/rating/#{RATE_VERSION}/Shop"
SHIP_PATH = "/api/shipments/#{SHIP_VERSION}/ship"
TRACK_PATH = "/api/track/#{TRACK_VERSION}/details"
LABEL_PATH = "/api/labels/#{LABEL_VERSION}/recovery"
Expand Down
4 changes: 4 additions & 0 deletions spec/ups/builders/rate_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def test_has_correct_package_weight
assert_equal package[:weight], @rate_builder.as_json['RateRequest']['Shipment']['Package'][0]['PackageWeight']['Weight']
end

def test_has_correct_packaging_type
assert_equal '02', @rate_builder.as_json['RateRequest']['Shipment']['Package'][0]['PackagingType']['Code']
end

def test_has_correct_return_service
assert_equal '10', @rate_builder.as_json['RateRequest']['Shipment']['ShipmentServiceOptions']['ReturnService']['Code']
end
Expand Down
4 changes: 4 additions & 0 deletions spec/ups/builders/ship_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def test_has_correct_package_weight
assert_equal package[:weight], @ship_builder.as_json['ShipmentRequest']['Shipment']['Package'][0]['PackageWeight']['Weight']
end

def test_has_correct_packaging_type
assert_equal '02', @ship_builder.as_json['ShipmentRequest']['Shipment']['Package'][0]['Packaging']['Code']
end

def test_has_correct_label_specification
assert_equal 'GIF', @ship_builder.as_json['ShipmentRequest']['LabelSpecification']['LabelImageFormat']['Code']
assert_equal '100', @ship_builder.as_json['ShipmentRequest']['LabelSpecification']['LabelStockSize']['Height']
Expand Down

0 comments on commit 13d60ae

Please sign in to comment.