From d86c47367b6c03e5896fa79bee81328f4927d951 Mon Sep 17 00:00:00 2001 From: Aaron Davis Date: Sun, 14 Jul 2013 11:57:28 -0700 Subject: [PATCH 1/4] remove Enumerations --- lib/shippinglogic/ups/rate.rb | 53 ++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/lib/shippinglogic/ups/rate.rb b/lib/shippinglogic/ups/rate.rb index 6db97a7..dbf72f2 100644 --- a/lib/shippinglogic/ups/rate.rb +++ b/lib/shippinglogic/ups/rate.rb @@ -76,7 +76,7 @@ class UPS # rates.first # #, @type="Ground", @name="Ground"> - # + # # # to show accessor methods # rates.first.name # # => "Ground" @@ -84,10 +84,10 @@ class Rate < Service def self.path "/Rate" end - + # Each rate result is an object of this class class Service; attr_accessor :name, :type, :speed, :rate, :currency; end - + # shipper options attribute :shipper_name, :string attribute :shipper_streets, :string @@ -95,7 +95,7 @@ class Service; attr_accessor :name, :type, :speed, :rate, :currency; end attribute :shipper_state, :string attribute :shipper_postal_code, :string attribute :shipper_country, :string, :modifier => :country_code - + # recipient options attribute :recipient_name, :string attribute :recipient_streets, :string @@ -104,7 +104,7 @@ class Service; attr_accessor :name, :type, :speed, :rate, :currency; end attribute :recipient_postal_code, :string attribute :recipient_country, :string, :modifier => :country_code attribute :recipient_residential, :boolean, :default => false - + # packaging options attribute :packaging_type, :string, :default => "00" attribute :package_count, :integer, :default => 1 @@ -114,84 +114,84 @@ class Service; attr_accessor :name, :type, :speed, :rate, :currency; end attribute :package_width, :float attribute :package_height, :float attribute :package_dimension_units, :string, :default => "IN" - + # monetary options attribute :currency_type, :string attribute :insured_value, :decimal attribute :payor_account_number, :string, :default => lambda { |shipment| shipment.base.account } - + # delivery options attribute :service_type, :string attribute :dropoff_type, :string, :default => "01" attribute :saturday, :boolean, :default => false - + # misc options attribute :documents_only, :boolean, :default => false - + private def target @target ||= parse_response(request(build_request)) end - + def build_request b = builder build_authentication(b) b.instruct! - + b.RatingServiceSelectionRequest do b.Request do b.RequestAction "Rate" b.RequestOption service_type ? "Rate" : "Shop" end - + b.PickupType do b.Code dropoff_type end - + b.Shipment do b.Shipper do b.Name shipper_name b.ShipperNumber payor_account_number build_address(b, :shipper) end - + b.ShipTo do b.CompanyName recipient_name build_address(b, :recipient) end - + if service_type b.Service do b.Code service_type end end - + b.DocumentsOnly if documents_only - + package_count.times do |i| b.Package do b.PackagingType do b.Code packaging_type end - + b.Dimensions do b.UnitOfMeasurement do b.Code package_dimension_units end - + b.Length "%.2f" % package_length b.Width "%.2f" % package_width b.Height "%.2f" % package_height end - + b.PackageWeight do b.UnitOfMeasurement do b.Code package_weight_units end - + b.Weight "%.1f" % package_weight end - + b.PackageServiceOptions do if insured_value b.InsuredValue do @@ -202,20 +202,21 @@ def build_request end end end - + b.ShipmentServiceOptions do b.SaturdayDelivery if saturday end end end end - + def parse_response(response) return [] if !response[:rated_shipment] - + response[:rated_shipment].collect do |details| service = Service.new - service.name = Enumerations::SERVICE_TYPES[details[:service][:code]] + # service.name = Enumerations::SERVICE_TYPES[details[:service][:code]] + service.name = details[:service][:code] service.type = service.name service.speed = (days = details[:guaranteed_days_to_delivery]) && (days.to_i * 86400) service.rate = BigDecimal.new(details[:total_charges][:monetary_value]) From 42932572f4ce98f20381dd3c357f15c0ee0c2762 Mon Sep 17 00:00:00 2001 From: Aaron Davis Date: Sun, 14 Jul 2013 14:23:39 -0700 Subject: [PATCH 2/4] update ups.rb to include enumerations --- lib/shippinglogic/ups.rb | 1 + lib/shippinglogic/ups/enumerations.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/shippinglogic/ups.rb b/lib/shippinglogic/ups.rb index 4936789..1bbe6ee 100644 --- a/lib/shippinglogic/ups.rb +++ b/lib/shippinglogic/ups.rb @@ -1,3 +1,4 @@ +require "shippinglogic/ups/enumerations" require "shippinglogic/ups/service" require "shippinglogic/ups/cancel" require "shippinglogic/ups/rate" diff --git a/lib/shippinglogic/ups/enumerations.rb b/lib/shippinglogic/ups/enumerations.rb index fda9a9d..f47e23b 100644 --- a/lib/shippinglogic/ups/enumerations.rb +++ b/lib/shippinglogic/ups/enumerations.rb @@ -21,7 +21,7 @@ module Enumerations "2b" => "Medium Express Box", "2c" => "Large Express Box" } - + # delivery options DROPOFF_TYPES = { "01" => "Daily Pickup", From 230d00662c6e601e8f4344c3ce1d2d948e4566f5 Mon Sep 17 00:00:00 2001 From: Aaron Davis Date: Sun, 14 Jul 2013 14:30:12 -0700 Subject: [PATCH 3/4] re-enable service.name enumerations --- lib/shippinglogic/ups/rate.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/shippinglogic/ups/rate.rb b/lib/shippinglogic/ups/rate.rb index dbf72f2..80034aa 100644 --- a/lib/shippinglogic/ups/rate.rb +++ b/lib/shippinglogic/ups/rate.rb @@ -215,8 +215,7 @@ def parse_response(response) response[:rated_shipment].collect do |details| service = Service.new - # service.name = Enumerations::SERVICE_TYPES[details[:service][:code]] - service.name = details[:service][:code] + service.name = Enumerations::SERVICE_TYPES[details[:service][:code]] service.type = service.name service.speed = (days = details[:guaranteed_days_to_delivery]) && (days.to_i * 86400) service.rate = BigDecimal.new(details[:total_charges][:monetary_value]) From 0955da7333f7b760bb3a69b3b1cfb254e869c24a Mon Sep 17 00:00:00 2001 From: Aaron Davis Date: Sun, 14 Jul 2013 15:45:20 -0700 Subject: [PATCH 4/4] correct spelling for occurred --- README.rdoc | 4 ++-- lib/shippinglogic/fedex/track.rb | 6 ++--- lib/shippinglogic/ups/track.rb | 39 ++++++++++++++++---------------- spec/fedex/track_spec.rb | 2 +- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/README.rdoc b/README.rdoc index 851fc22..a651459 100644 --- a/README.rdoc +++ b/README.rdoc @@ -51,7 +51,7 @@ What I think is unique about this library is it's usage / syntax: tracking.events.first # => # + # @city="Sacramento", @type="DL", @country="US", @occurred_at=Mon Dec 08 10:43:37 -0500 2008> tracking.events.first.name # => "Delivered" @@ -139,7 +139,7 @@ Then your results: - @tracking_details.events.each do |event| .event .name= event.name - .occured_at= event.occured_at.to_s(:long) + .occurred_at= event.occurred_at.to_s(:long) .location== #{event.city}, #{event.state} #{event.postal_code}, #{event.country} .residential= event.residential ? "Yes" : "No" diff --git a/lib/shippinglogic/fedex/track.rb b/lib/shippinglogic/fedex/track.rb index c003259..b4aeabf 100644 --- a/lib/shippinglogic/fedex/track.rb +++ b/lib/shippinglogic/fedex/track.rb @@ -22,7 +22,7 @@ class FedEx # # tracking.events.first # # => # + # # @city="Sacramento", @type="DL", @country="US", @occurred_at=Mon Dec 08 10:43:37 -0500 2008> # # tracking.events.first.name # # => "Delivered" @@ -36,7 +36,7 @@ class FedEx class Track < Service class Details # Each tracking result is an object of this class - class Event; attr_accessor :name, :type, :occured_at, :city, :state, :postal_code, :country, :residential; end + class Event; attr_accessor :name, :type, :occurred_at, :city, :state, :postal_code, :country, :residential; end attr_accessor :origin_city, :origin_state, @@ -82,7 +82,7 @@ def initialize(response) event = Event.new event.name = details[:event_description] event.type = details[:event_type] - event.occured_at = Time.parse(details[:timestamp]) + event.occurred_at = Time.parse(details[:timestamp]) event.city = details[:address][:city] event.state = details[:address][:state_or_province_code] event.postal_code = details[:address][:postal_code] diff --git a/lib/shippinglogic/ups/track.rb b/lib/shippinglogic/ups/track.rb index 0fe5b23..d928918 100644 --- a/lib/shippinglogic/ups/track.rb +++ b/lib/shippinglogic/ups/track.rb @@ -16,14 +16,14 @@ class UPS # # tracking_details.status # # => "Delivered" - # + # # tracking_details.signature_name # # => "KKING" - # + # # tracking_details.events.first # # => # - # + # # @city="Sacramento", @type="Delivered", @country="US", @occurred_at=Mon Dec 08 10:43:37 -0500 2008> + # # tracking_details.events.first.name # # => "Delivered" # @@ -37,48 +37,47 @@ class Track < Service def self.path "/Track" end - + class Details # Each tracking result is an object of this class - class Event; attr_accessor :name, :type, :occured_at, :city, :state, :postal_code, :country; end - + class Event; attr_accessor :name, :type, :occurred_at, :city, :state, :postal_code, :country; end + attr_accessor :origin_city, :origin_state, :origin_country, :destination_city, :destination_state, :destination_country, :signature_name, :service_type, :status, :delivery_at, :events - + def initialize(response) details = response[:shipment] - + if origin = details.fetch(:shipper, {})[:address] self.origin_city = origin[:city] self.origin_state = origin[:state_province_code] self.origin_country = origin[:country_code] end - + if destination = details.fetch(:ship_to, {})[:address] self.destination_city = destination[:city] self.destination_state = destination[:state_province_code] self.destination_country = destination[:country_code] end - + package = details[:package] events = package[:activity].is_a?(Array) ? package[:activity] : [package[:activitiy]].compact last_event = events.first delivery = events.detect{|e| e[:status][:status_type][:code] == "D" } - + self.signature_name = last_event && last_event[:signed_for_by_name] self.service_type = details[:service][:description] self.status = last_event && last_event[:status][:status_type][:description] self.delivery_at = delivery && Time.parse(delivery[:date] + delivery[:time]) - + self.events = events.collect do |details| event = Event.new status = details[:status][:status_type] event.name = status[:description] event.type = status[:code] - #FIXME The proper spelling is "occurred", not "occured." - event.occured_at = Time.parse(details[:date] + details[:time]) + event.occurred_at = Time.parse(details[:date] + details[:time]) location = details[:activity_location][:address] event.city = location[:city] event.state = location[:state_province_code] @@ -88,28 +87,28 @@ def initialize(response) end end end - + attribute :tracking_number, :string - + private # The parent class Service requires that we define this method. This is our kicker. This method is only # called when we need to deal with information from UPS. Notice the caching into the @target variable. def target @target ||= Details.new(request(build_request)) end - + # Just building some XML to send off to UPS. UPS requires this particualar format. def build_request b = builder build_authentication(b) b.instruct! - + b.TrackRequest do b.Request do b.RequestAction "Track" b.RequestOption "activity" end - + b.TrackingNumber tracking_number end end diff --git a/spec/fedex/track_spec.rb b/spec/fedex/track_spec.rb index bd78e2d..4d92507 100644 --- a/spec/fedex/track_spec.rb +++ b/spec/fedex/track_spec.rb @@ -27,7 +27,7 @@ event = track_details.events.first event.name.should == "Delivered" event.type.should == "DL" - event.occured_at.should == Time.parse("Mon Dec 08 10:43:37 -0500 2008") + event.occurred_at.should == Time.parse("Mon Dec 08 10:43:37 -0500 2008") event.city.should == "Sacramento" event.state.should == "CA" event.postal_code.should == "95817"