ups 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 35bf9beb313035a44c0368e0e6e56a4b0092b8db9b7c174352a753afada6e86b
4
- data.tar.gz: 9bc83318963bdb23e80ff441ded0b570b86e1ffe842e0d0d301e13aceaef842d
3
+ metadata.gz: e626b90734e734625637fba39fbab3bdea5049cce4ff12018d58674432eda7c4
4
+ data.tar.gz: b79d317b84991d35e1f2f585f9185e47bcb1c99c871ba7d6a565e785960f8e68
5
5
  SHA512:
6
- metadata.gz: b7b24ffd26fc3c9a04d86dc20c82c6000d114459917c8ceaf6cd30283fd6c1b07402ab6e6421648077365f4154127ffc64419dd8c7b7d006125a2e6d848a2101
7
- data.tar.gz: 70fe24d8d6b75f2eaca7cc73a2c5d29d438a69c531adf5f3b68dbfda1ace11af8f9ef060bc502e31e172edc416b301bb13bba1852b1dad90c087129e4db4fcfd
6
+ metadata.gz: de2ec5f3cfc70541b6576f688e970de88707b2039bc174d9e8f38fc40100172557d4d1e84ea5ec30ab0b6f707de38e6e3949de859d0a90f8549ed8dafd86ddcd
7
+ data.tar.gz: 10d7b84f11f208787a1ae941ea4fbee0ec2e3447af4ad4901036da009e8b07ada9da3fcfdf272f37349bff43b323e9a15400bca231003a63e2e9162ba410def8
@@ -1,9 +1,10 @@
1
1
  module Ups
2
2
  class Configuration
3
- attr_accessor :client_id, :client_secret, :account_number, :sandbox
3
+ attr_accessor :client_id, :client_secret, :account_number, :negotiated_rates, :sandbox
4
4
 
5
5
  def initialize(params = {})
6
6
  @sandbox = true
7
+ @negotiated_rates = false
7
8
  params.map{|k, v| send("#{k}=", v) if respond_to?("#{k}=") }
8
9
  end
9
10
 
data/lib/ups/rating.rb CHANGED
@@ -1,15 +1,17 @@
1
1
  module Ups
2
2
  class Rating
3
- attr_reader :client, :account_number
3
+ attr_reader :client, :account_number, :negotiated_rates
4
4
 
5
5
  def initialize(client, account_number: nil)
6
6
  @client = client
7
7
  @account_number = account_number || client.config.account_number
8
+ @negotiated_rates = client.config&.negotiated_rates || false
8
9
  end
9
10
 
10
11
  def get_rates(rate_request)
11
12
  endpoint = "/api/rating/v2409/Shop"
12
13
  payload = build_rate_payload(rate_request)
14
+ payload.deep_merge!(negotiated_rates_json) if negotiated_rates
13
15
  response = client.post(endpoint, body: payload)
14
16
  parse_rate_response(response)
15
17
  end
@@ -93,11 +95,11 @@ module Ups
93
95
  }
94
96
  }
95
97
  }
96
- base.deep_merge!(signature_required(package.delivery_confirmation)) if [2,3].include?(package&.delivery_confirmation.to_i)
98
+ base.deep_merge!(signature_required_json(package.delivery_confirmation)) if [2,3].include?(package&.delivery_confirmation.to_i)
97
99
  base
98
100
  end
99
101
 
100
- def signature_required(type)
102
+ def signature_required_json(type)
101
103
  # https://github.com/UPS-API/api-documentation/blob/main/Rating.yaml
102
104
  # DCISType fails if the value is nil, 0, or 1...
103
105
  # but 2 and 3 work as expected. So we need to omit
@@ -116,19 +118,31 @@ module Ups
116
118
  }
117
119
  end
118
120
 
121
+ def negotiated_rates_json
122
+ {
123
+ RateRequest: {
124
+ Shipment: {
125
+ ShipmentRatingOptions: {
126
+ NegotiatedRatesIndicator: "1",
127
+ TPFCNegotiatedRatesIndicator: "1"
128
+ }
129
+ }
130
+ }
131
+ }
132
+ end
133
+
119
134
  def parse_rate_response(response)
120
135
  rates = []
121
136
 
122
137
  if response['RateResponse'] && response['RateResponse']['RatedShipment']
123
138
  rated_shipments = response['RateResponse']['RatedShipment']
124
139
  rated_shipments = [rated_shipments] unless rated_shipments.is_a?(Array)
125
-
126
140
  rated_shipments.each do |shipment|
127
141
  rates << {
128
142
  service_code: shipment['Service']['Code'],
129
143
  service_name: get_service_name(shipment['Service']['Code']),
130
- total: shipment['TotalCharges']['MonetaryValue'].to_f,
131
- currency: shipment['TotalCharges']['CurrencyCode'],
144
+ total: get_rate_value(shipment),
145
+ currency: shipment.dig('TotalCharges', 'CurrencyCode'),
132
146
  transit_time: shipment['GuaranteedDelivery'] ? shipment['GuaranteedDelivery']['BusinessDaysInTransit'] : nil
133
147
  }
134
148
  end
@@ -137,6 +151,12 @@ module Ups
137
151
  rates
138
152
  end
139
153
 
154
+ def get_rate_value(data)
155
+ return data.dig('NegotiatedRateCharges', 'TotalCharge', 'MonetaryValue').to_f if negotiated_rates
156
+
157
+ data.dig('TotalCharges', 'MonetaryValue').to_f
158
+ end
159
+
140
160
  def get_service_name(code)
141
161
  service_names[code] || "Unknown Service (#{code})"
142
162
  end
data/lib/ups/shipping.rb CHANGED
@@ -1,15 +1,17 @@
1
1
  module Ups
2
2
  class Shipping
3
- attr_reader :client, :account_number
3
+ attr_reader :client, :account_number, :negotiated_rates
4
4
 
5
5
  def initialize(client, account_number: nil)
6
6
  @client = client
7
7
  @account_number = account_number || client.config.account_number
8
+ @negotiated_rates = client.config&.negotiated_rates || false
8
9
  end
9
10
 
10
11
  def create_shipment(ship_request)
11
12
  endpoint = "/api/shipments/v2409/ship"
12
13
  payload = build_ship_payload(ship_request)
14
+ payload.deep_merge!(negotiated_rates_json) if negotiated_rates
13
15
  response = client.post(endpoint, body: payload)
14
16
  parse_ship_response(response)
15
17
  end
@@ -78,6 +80,18 @@ module Ups
78
80
  }
79
81
  end
80
82
 
83
+ def negotiated_rates_json
84
+ {
85
+ ShipmentRequest: {
86
+ Shipment: {
87
+ ShipmentRatingOptions: {
88
+ NegotiatedRatesIndicator: "X"
89
+ }
90
+ }
91
+ }
92
+ }
93
+ end
94
+
81
95
  def address_hash(address)
82
96
  {
83
97
  AddressLine: [address.address_line_1, address.address_line_2].compact,
@@ -140,7 +154,7 @@ module Ups
140
154
  tracking_number: results['ShipmentIdentificationNumber'],
141
155
  label: results['PackageResults'][0]['ShippingLabel']['GraphicImage'],
142
156
  extension: results['PackageResults'][0]['ShippingLabel']['ImageFormat']['Code'],
143
- total: results['ShipmentCharges']['TotalCharges']['MonetaryValue'].to_f,
157
+ total: get_rate_value(results['ShipmentCharges']),
144
158
  currency: results['ShipmentCharges']['TotalCharges']['CurrencyCode'],
145
159
  data: response.as_json(except: ["GraphicImage", "HTMLImage"]),
146
160
  alerts: response['ShipmentResponse']['Response']['Alert']&.map{|s| s['Description'] }
@@ -149,5 +163,12 @@ module Ups
149
163
  raise APIError, "Invalid shipment response: #{response}"
150
164
  end
151
165
  end
166
+
167
+ def get_rate_value(data)
168
+ return data.dig('NegotiatedRateCharges', 'TotalCharge', 'MonetaryValue').to_f if negotiated_rates
169
+
170
+ data.dig('TotalCharges', 'MonetaryValue').to_f
171
+ end
172
+
152
173
  end
153
174
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ups
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - JD Warren
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-06-16 00:00:00.000000000 Z
11
+ date: 2025-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty