freight_kit 0.1.21 → 0.1.23

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.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -3
  3. data/Rakefile +1 -3
  4. data/VERSION +1 -1
  5. data/freight_kit-0.1.21.gem +0 -0
  6. data/freight_kit-0.1.22.gem +0 -0
  7. data/freight_kit.gemspec +8 -19
  8. data/lib/freight_kit/address_normalizer.rb +1 -3
  9. data/lib/freight_kit/api_clients/soap_client.rb +30 -5
  10. data/lib/freight_kit/carrier.rb +36 -7
  11. data/lib/freight_kit/carriers/a1_express.rb +11 -0
  12. data/lib/freight_kit/carriers/abf_freight_system.rb +9 -26
  13. data/lib/freight_kit/carriers/aloha_freight_forwarders.rb +1 -5
  14. data/lib/freight_kit/carriers/amado_trucking.rb +1 -5
  15. data/lib/freight_kit/carriers/clear_lane_freight_systems.rb +6 -15
  16. data/lib/freight_kit/carriers/cpl_express.rb +1 -5
  17. data/lib/freight_kit/carriers/cross_country_freight_solutions.rb +6 -17
  18. data/lib/freight_kit/carriers/dependable_highway_express.rb +6 -18
  19. data/lib/freight_kit/carriers/forward_air.rb +4 -12
  20. data/lib/freight_kit/carriers/gsp_express.rb +13 -0
  21. data/lib/freight_kit/carriers/on_trac.rb +7 -25
  22. data/lib/freight_kit/carriers/peninsula_truck_lines.rb +4 -20
  23. data/lib/freight_kit/carriers/roadrunner_transportation_services.rb +2 -8
  24. data/lib/freight_kit/carriers/saia.rb +4 -19
  25. data/lib/freight_kit/carriers/southeastern_freight_lines.rb +4 -13
  26. data/lib/freight_kit/carriers/t_force_worldwide.rb +7 -19
  27. data/lib/freight_kit/carriers/top_ten_express.rb +13 -0
  28. data/lib/freight_kit/carriers/total_quality_logistics.rb +2 -1
  29. data/lib/freight_kit/carriers/western_regional_delivery_service.rb +26 -11
  30. data/lib/freight_kit/carriers/xpo_logistics.rb +5 -17
  31. data/lib/freight_kit/errors/rate_limit_error.rb +12 -0
  32. data/lib/freight_kit/errors/unserviceable_accessorials_error.rb +1 -3
  33. data/lib/freight_kit/helpers/pickupable.rb +1 -10
  34. data/lib/freight_kit/http_client/httpi_adapter.rb +37 -0
  35. data/lib/freight_kit/http_client/safe_retries.rb +17 -1
  36. data/lib/freight_kit/http_client.rb +13 -9
  37. data/lib/freight_kit/models/credential.rb +2 -15
  38. data/lib/freight_kit/models/lane.rb +20 -0
  39. data/lib/freight_kit/models/shipment.rb +1 -8
  40. data/lib/freight_kit/models/territory.rb +37 -0
  41. data/lib/freight_kit/package.rb +3 -13
  42. data/lib/freight_kit/packaging.rb +1 -16
  43. data/lib/freight_kit/platforms/carrier_logistics.rb +5 -10
  44. data/lib/freight_kit/platforms/dsdc.rb +82 -0
  45. data/lib/freight_kit/platforms/next_tms/graphql_client/mutation/quote_create.rb +32 -0
  46. data/lib/freight_kit/platforms/next_tms/graphql_client/mutation/shipment_create.rb +29 -0
  47. data/lib/freight_kit/platforms/next_tms/graphql_client/query/broker_carrier.rb +37 -0
  48. data/lib/freight_kit/platforms/next_tms/graphql_client/query/broker_carriers.rb +45 -0
  49. data/lib/freight_kit/platforms/next_tms/graphql_client/query/broker_customer.rb +54 -0
  50. data/lib/freight_kit/platforms/next_tms/graphql_client/query/broker_customers.rb +62 -0
  51. data/lib/freight_kit/platforms/next_tms/graphql_client/query/carrier.rb +22 -0
  52. data/lib/freight_kit/platforms/next_tms/graphql_client/query/carriers.rb +30 -0
  53. data/lib/freight_kit/platforms/next_tms/graphql_client/query/customer.rb +20 -0
  54. data/lib/freight_kit/platforms/next_tms/graphql_client/query/customers.rb +28 -0
  55. data/lib/freight_kit/platforms/next_tms/graphql_client/query/invoice.rb +31 -0
  56. data/lib/freight_kit/platforms/next_tms/graphql_client/query/invoices.rb +39 -0
  57. data/lib/freight_kit/platforms/next_tms/graphql_client/query/shipment.rb +102 -0
  58. data/lib/freight_kit/platforms/next_tms/graphql_client/query/shipment_documents.rb +29 -0
  59. data/lib/freight_kit/platforms/next_tms/graphql_client/query/shipments.rb +110 -0
  60. data/lib/freight_kit/platforms/next_tms/graphql_client.rb +22 -0
  61. data/lib/freight_kit/platforms/next_tms/orm/address.rb +22 -0
  62. data/lib/freight_kit/platforms/next_tms/orm/base.rb +120 -0
  63. data/lib/freight_kit/platforms/next_tms/orm/broker_carrier.rb +18 -0
  64. data/lib/freight_kit/platforms/next_tms/orm/broker_customer.rb +22 -0
  65. data/lib/freight_kit/platforms/next_tms/orm/carrier.rb +14 -0
  66. data/lib/freight_kit/platforms/next_tms/orm/collection.rb +335 -0
  67. data/lib/freight_kit/platforms/next_tms/orm/customer.rb +12 -0
  68. data/lib/freight_kit/platforms/next_tms/orm/invoice.rb +21 -0
  69. data/lib/freight_kit/platforms/next_tms/orm/record_not_found_error.rb +9 -0
  70. data/lib/freight_kit/platforms/next_tms/orm/shipment.rb +50 -0
  71. data/lib/freight_kit/platforms/next_tms/orm.rb +38 -0
  72. data/lib/freight_kit/platforms/next_tms.rb +414 -55
  73. data/lib/freight_kit/platforms/the_great_information_factory.rb +4 -12
  74. data/lib/freight_kit.rb +19 -0
  75. data/schemas/next_tms.json +110710 -0
  76. metadata +86 -5
@@ -0,0 +1,335 @@
1
+ module FreightKit
2
+ module Platforms
3
+ class NextTMS
4
+ module ORM
5
+ # A lazy, chainable query of a collection field, e.g. `shipments(filter:, page:, pageSize:, sort:)`.
6
+ #
7
+ # Conditions become ransack predicates (`where(pro: '123')` => `proEq`), so only the filter fields the tenant
8
+ # generates from the model's ransackable attributes work. Without {#limit}, {#to_a} fetches every page.
9
+ class Collection
10
+ include Enumerable
11
+
12
+ # Pairs of opposite predicates, e.g. `delivered_at_gteq` is the opposite of `delivered_at_lt`
13
+ NEGATIONS = {
14
+ 'cont' => 'not_cont', 'end' => 'not_end', 'eq' => 'not_eq', 'gt' => 'lteq', 'gteq' => 'lt', 'in' => 'not_in',
15
+ 'start' => 'not_start'
16
+ }.freeze
17
+ PAGE_SIZE = 100 # the API's maximum
18
+ PREDICATE = /_(cont|end|eq|gt|gteq|in|lt|lteq|not_cont|not_end|not_eq|not_in|not_start|null|start)\z/
19
+
20
+ attr_reader :filter, :orm_class, :page, :page_size, :sort, :updated_at, :version
21
+
22
+ attr_writer :orm_objects
23
+
24
+ delegate :[], :each, :each_with_index, :map, to: :to_a
25
+
26
+ def initialize(orm_class, **options)
27
+ raise ArgumentError, 'orm_class must be descended from FreightKit::Platforms::NextTMS::ORM::Base' unless orm_class < Base
28
+
29
+ @filter = options[:filter] || {}
30
+ @none = options[:none] || false
31
+ @offset = options[:offset]
32
+ @orm_class = orm_class
33
+ @page = options[:page]
34
+ @page_size = options[:page_size]
35
+ @sort = Array(options[:sort]).compact.presence
36
+ @total_items = options[:total_items]
37
+ @updated_at = options[:updated_at]
38
+ end
39
+
40
+ # Arguments and blocks need the records, the bare forms only the total
41
+ def any?(*args, &) = args.empty? && !block_given? ? total_items.positive? : super
42
+ def blank? = !any?
43
+ def count(*args, &) = args.empty? && !block_given? ? total_items : super
44
+ def empty? = total_items.zero?
45
+
46
+ # The singular query cannot apply the collection's filter, so a filtered collection filters by ID too, and a
47
+ # limited one searches its page
48
+ def find(id)
49
+ record = if @none then nil
50
+ elsif page_size.present? then to_a.find { |item| integer_id(item.id).to_s == integer_id(id).to_s }
51
+ elsif filter.blank? then orm_class.new(id).reload
52
+ else where(id:).limit(1).first
53
+ end
54
+
55
+ record || raise(RecordNotFoundError, "Couldn't find #{orm_class} with id #{id}")
56
+ end
57
+
58
+ def find_by(**conditions)
59
+ find_by!(**conditions)
60
+ rescue RecordNotFoundError
61
+ nil
62
+ end
63
+
64
+ def find_by!(**conditions)
65
+ return find(conditions[:id]) if conditions.keys == [:id]
66
+
67
+ where(**conditions).limit(1).first || raise(RecordNotFoundError, "Couldn't find #{orm_class} with #{conditions}")
68
+ end
69
+
70
+ def first = page_size.present? ? orm_objects.first : limit(1).orm_objects.first
71
+ def first! = first || raise(RecordNotFoundError, "Couldn't find #{orm_class}")
72
+
73
+ def ids = pluck(:id)
74
+ def inspect = "#<#{self.class.name} orm_class=#{orm_class}>"
75
+
76
+ def last
77
+ return orm_objects.last if page_size.present?
78
+
79
+ offset(total_items - 1).limit(1).orm_objects.first unless empty?
80
+ end
81
+
82
+ def limit(page_size)
83
+ raise ArgumentError, 'page_size must be 1..100' unless page_size.is_a?(Integer) && page_size.between?(1, PAGE_SIZE)
84
+
85
+ new_instance(page: @offset ? offset_page(@offset, page_size) : page || 1, page_size:)
86
+ end
87
+
88
+ def new_instance(**options)
89
+ self.class.new(
90
+ orm_class,
91
+ filter: options.fetch(:filter, filter),
92
+ none: options.fetch(:none, @none),
93
+ offset: options.fetch(:offset, @offset),
94
+ page: options.fetch(:page, page),
95
+ page_size: options.fetch(:page_size, page_size),
96
+ sort: options.fetch(:sort, sort),
97
+ total_items: options[:total_items],
98
+ updated_at: options.fetch(:updated_at, updated_at)
99
+ )
100
+ end
101
+
102
+ def none = new_instance(none: true, total_items: 0, page: 1, page_size: 1).tap { |collection| collection.orm_objects = [] }
103
+ def not(**conditions) = merge(conditions.map { |key, value| build_negated_filter(key => value) })
104
+
105
+ def offset(value)
106
+ raise ArgumentError, 'Offset must be a non-negative integer' unless value.is_a?(Integer) && value >= 0
107
+
108
+ page_size.present? ? new_instance(offset: value, page: offset_page(value, page_size)) : new_instance(offset: value)
109
+ end
110
+
111
+ def order(*orders, **order_hash)
112
+ sort_strings = if order_hash.any?
113
+ order_hash.map { |attribute, direction| "#{attribute}:#{direction || :asc}" }
114
+ else
115
+ orders.map { |(attribute, direction)| "#{attribute}:#{direction || :asc}" }
116
+ end
117
+
118
+ new_instance(sort: sort_strings)
119
+ end
120
+
121
+ def orm_objects = @orm_objects ||= fetch_orm_objects
122
+ def pluck(attribute) = map(&attribute)
123
+
124
+ def present? = any?
125
+
126
+ def sample
127
+ collection = page_size.present? ? self : limit(10)
128
+ collection.to_a.sample
129
+ end
130
+ alias take sample
131
+
132
+ def to_a
133
+ @to_a ||= if @orm_objects || page_size.present?
134
+ orm_objects
135
+ else
136
+ raise ArgumentError, 'offset needs a limit, since the API pages' if @offset
137
+
138
+ first = new_instance(page: page || 1, page_size: PAGE_SIZE).tap(&:orm_objects)
139
+ pages = ((first.page + 1)..first.total_pages).map { |page| new_instance(page:, page_size: PAGE_SIZE).orm_objects }
140
+ @total_items = first.total_items
141
+ @updated_at = first.updated_at
142
+ @version = first.version
143
+
144
+ [first.orm_objects, *pages].flatten
145
+ end
146
+ end
147
+
148
+ def total_items
149
+ return 0 if @none
150
+ return @total_items if @total_items.present?
151
+
152
+ @total_items = (page_size.present? ? self : new_instance(page: 1, page_size: 1)).tap(&:orm_objects).total_items
153
+ end
154
+ alias length total_items
155
+ alias size total_items
156
+
157
+ def total_pages
158
+ return if total_items.blank? || page_size.blank?
159
+
160
+ (total_items.to_f / page_size).ceil
161
+ end
162
+
163
+ def where(**conditions)
164
+ conditions.blank? ? self : merge(conditions.map { |key, value| build_filter(key => value) })
165
+ end
166
+
167
+ protected
168
+
169
+ def fetch_orm_objects
170
+ return @orm_objects = [] if @none
171
+
172
+ operation = orm_class.collection_operation
173
+ variables = { filter: serialize(filter).presence, page:, page_size:, sort: }
174
+ data = orm_class.platform.query(operation, cache: true, **variables)[operation]
175
+
176
+ raise ResponseError, 'data[:page] is blank' if data&.dig(:page).blank?
177
+ raise ResponseError, 'data[:page_size] is blank' if data[:page_size].blank?
178
+ raise ResponseError, 'data[:total_items] is blank' if data[:total_items].blank?
179
+
180
+ @page = data[:page]
181
+ @page_size = data[:page_size]
182
+ @total_items = data[:total_items]
183
+ @updated_at = data[:updated_at]
184
+ @version = data[:version]
185
+
186
+ data[:items].map { |item| orm_class.new(item[:id], loaded: true, **item.except(:id)) }
187
+ end
188
+
189
+ private
190
+
191
+ # Contradictory alternatives match nothing, so they drop out
192
+ def alternatives(conditions)
193
+ conditions.filter_map { |condition| combine({}, condition.map { |key, value| build_filter(key => value) }) }
194
+ end
195
+
196
+ # Whether the filter knows the key only by its foreign key, e.g. `broker_carrier_id_null` but no `broker_carrier_null`
197
+ def association?(key)
198
+ arguments = GraphQLClient::Schema.types["#{orm_class.graphql_name}Filter"].arguments
199
+
200
+ arguments.exclude?("#{key.to_s.camelize(:lower)}Null") && arguments.key?("#{key.to_s.camelize(:lower)}IdNull")
201
+ end
202
+
203
+ def build_filter(**conditions)
204
+ conditions.each_with_object({}) do |(key, value), filter|
205
+ next filter[:or] = alternatives(value) if key == :or
206
+
207
+ value = integer_ids(value) if id_attribute?(key)
208
+ next filter[key] = value if key.to_s.match?(PREDICATE)
209
+
210
+ case value
211
+ when Array
212
+ next filter[:"#{key}_id_in"] = value.map { |record| integer_id(record.id) } if value.any? && value.all?(Base)
213
+
214
+ filter[:"#{key}_in"] = value
215
+ when Base
216
+ filter[:"#{key}_id_eq"] = integer_id(value.id)
217
+ when Collection
218
+ filter[:"#{key}_id_in"] = value.ids.map { |id| integer_id(id) }
219
+ when nil
220
+ filter[:"#{association?(key) ? "#{key}_id" : key}_null"] = true
221
+ when Range
222
+ filter[:"#{key}_gteq"] = value.begin unless value.begin.nil?
223
+ filter[:"#{key}_#{value.exclude_end? ? 'lt' : 'lteq'}"] = value.end unless value.end.nil?
224
+ else
225
+ filter[:"#{key}_eq"] = value
226
+ end
227
+ end
228
+ end
229
+
230
+ # Filter fields are ANDed, so only a condition that becomes one predicate can be negated
231
+ def build_negated_filter(**conditions)
232
+ conditions.each_with_object({}) do |(key, value), filter|
233
+ predicates = build_filter(key => value)
234
+ raise ArgumentError, "Can't negate #{key}: #{value.inspect}, negate each condition instead" if key == :or || predicates.many?
235
+
236
+ predicate, value = predicates.first
237
+ match = predicate.to_s.match(PREDICATE)
238
+ next filter[predicate] = !value if match[1] == 'null'
239
+
240
+ filter[:"#{match.pre_match}_#{NEGATIONS[match[1]] || NEGATIONS.key(match[1])}"] = value
241
+ end
242
+ end
243
+
244
+ # ANDs each condition's predicates with `base`, since a filter has one value per predicate, and conditions such as
245
+ # `id:` and `id_eq:` share one.
246
+ #
247
+ # @return [Hash, nil] Nil when the predicates contradict each other.
248
+ def combine(base, predicates_by_condition)
249
+ predicates_by_condition.flat_map(&:to_a).each_with_object(base.dup) do |(key, value), result|
250
+ old = result[key]
251
+ next result[key] = value if old.nil? || comparable(old) == comparable(value)
252
+ raise ArgumentError, 'A query takes one or:, combine the alternatives instead' if key == :or
253
+
254
+ match = key.to_s.match(PREDICATE)
255
+ case match[1]
256
+ when 'eq', 'null' then return nil
257
+ when 'gt', 'gteq' then result[key] = [old, value].max_by { |bound| comparable(bound) }
258
+ when 'in' then result[key] = old.select { |item| comparable(value).include?(comparable(item)) }
259
+ when 'lt', 'lteq' then result[key] = [old, value].min_by { |bound| comparable(bound) }
260
+ when 'not_eq'
261
+ not_in = :"#{match.pre_match}_not_in"
262
+ result.delete(key)
263
+ result[not_in] = [*result[not_in], old, value].uniq { |item| comparable(item) }
264
+ when 'not_in' then result[key] = (old + value).uniq { |item| comparable(item) }
265
+ else raise ArgumentError, "A query takes one #{key}"
266
+ end
267
+ end
268
+ end
269
+
270
+ # Values can be ISO 8601 strings, of dates or of times with different offsets, so they compare by date or instant
271
+ def comparable(value)
272
+ case value
273
+ when Array then value.map { |item| comparable(item) }
274
+ when Hash then value.transform_values { |item| comparable(item) }
275
+ when String then value.match?(/\A\d{4}-\d{2}-\d{2}\z/) ? Date.iso8601(value) : Time.iso8601(value)
276
+ when Symbol then value.to_s
277
+ else value
278
+ end
279
+ rescue ArgumentError
280
+ value
281
+ end
282
+
283
+ # `id` and `*_id`, with or without a predicate
284
+ def id_attribute?(key) = key.to_s.sub(PREDICATE, '').then { |attribute| attribute == 'id' || attribute.end_with?('_id') }
285
+
286
+ # GraphQL serializes IDs as strings, but ID filters take the integer column
287
+ def integer_id(id) = Integer(id, 10, exception: false) || id
288
+
289
+ def integer_ids(value)
290
+ case value
291
+ when Array then value.map { |id| integer_id(id) }
292
+ when Range then Range.new(integer_id(value.begin), integer_id(value.end), value.exclude_end?)
293
+ else integer_id(value)
294
+ end
295
+ end
296
+
297
+ def merge(predicates_by_condition)
298
+ merged = combine(filter, predicates_by_condition)&.then { |combined| satisfiable(combined) }
299
+
300
+ merged ? new_instance(filter: merged) : none
301
+ end
302
+
303
+ # The API pages, so the offset must start a page
304
+ def offset_page(offset, page_size)
305
+ raise ArgumentError, "offset #{offset} must be a multiple of the limit #{page_size}" unless (offset % page_size).zero?
306
+
307
+ (offset / page_size) + 1
308
+ end
309
+
310
+ # The API ignores empty lists, so an empty list matches nothing, and an empty exclusion excludes nothing
311
+ def satisfiable(conditions)
312
+ conditions = conditions.reject { |key, value| value == [] && key.end_with?('_not_in') }
313
+ return if conditions.any? { |_, value| value == [] }
314
+ return conditions unless conditions.key?(:or)
315
+
316
+ alternatives = conditions[:or].filter_map { |alternative| satisfiable(alternative) }
317
+ return if alternatives.empty?
318
+
319
+ alternatives.any?(&:empty?) ? conditions.except(:or) : conditions.merge(or: alternatives)
320
+ end
321
+
322
+ def serialize(value)
323
+ case value
324
+ when Array then value.map { |item| serialize(item) }
325
+ when Date, Time then value.iso8601
326
+ when Hash then value.transform_values { |item| serialize(item) }
327
+ when Symbol then value.to_s
328
+ else value
329
+ end
330
+ end
331
+ end
332
+ end
333
+ end
334
+ end
335
+ end
@@ -0,0 +1,12 @@
1
+ module FreightKit
2
+ module Platforms
3
+ class NextTMS
4
+ module ORM
5
+ class Customer < Base
6
+ def broker_customers = @broker_customers ||= orm::BrokerCustomer.where(customer: self)
7
+ def name = @name ||= attribute(:name)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ module FreightKit
2
+ module Platforms
3
+ class NextTMS
4
+ module ORM
5
+ # What the API key's customer owes for a shipment. {NextTMS#invoice} downloads its document.
6
+ class Invoice < Base
7
+ def balance_cents = @balance_cents ||= attribute(:balance_cents)
8
+ def balance_currency = @balance_currency ||= attribute(:balance_currency)
9
+ def due_on = @due_on ||= date(:due_on)
10
+ def issued_on = @issued_on ||= date(:issued_on)
11
+ # The load ID
12
+ def number = @number ||= attribute(:number)
13
+ def paid_at = @paid_at ||= time(:paid_at)
14
+ def shipment = @shipment ||= fetch_association(:shipment)
15
+ def total_cents = @total_cents ||= attribute(:total_cents)
16
+ def total_currency = @total_currency ||= attribute(:total_currency)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ module FreightKit
2
+ module Platforms
3
+ class NextTMS
4
+ module ORM
5
+ class RecordNotFoundError < Error; end
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,50 @@
1
+ module FreightKit
2
+ module Platforms
3
+ class NextTMS
4
+ module ORM
5
+ class Shipment < Base
6
+ class << self
7
+ def association_type_mapping = super.merge(receiver_address: 'Address', shipper_address: 'Address')
8
+ end
9
+
10
+ def bol = @bol ||= attribute(:bol)
11
+ def booked_at = @booked_at ||= time(:booked_at)
12
+ def broker_carrier = @broker_carrier ||= fetch_association(:broker_carrier)
13
+ def broker_customer = @broker_customer ||= fetch_association(:broker_customer)
14
+ def canceled_at = @canceled_at ||= time(:canceled_at)
15
+ def created_at = @created_at ||= time(:created_at)
16
+ def customer_reference = @customer_reference ||= attribute(:customer_reference)
17
+ def delivered_at = @delivered_at ||= time(:delivered_at)
18
+ def delivery_appointment_at = @delivery_appointment_at ||= time(:delivery_appointment_at)
19
+ def delivery_appointment_on = @delivery_appointment_on ||= date(:delivery_appointment_on)
20
+ def delivery_eta_on = @delivery_eta_on ||= date(:delivery_eta_on)
21
+ def departed_origin_terminal_at = @departed_origin_terminal_at ||= time(:departed_origin_terminal_at)
22
+ def dispatched_at = @dispatched_at ||= time(:dispatched_at)
23
+
24
+ # The carrier's scans, oldest first, as hashes with `occurred_at` and `type_code`, a {ShipmentEvent#type_code}
25
+ def events = @events ||= Array(attribute(:events))
26
+
27
+ def load_id = @load_id ||= attribute(:load_id)
28
+ def lost_on = @lost_on ||= date(:lost_on)
29
+ def out_for_delivery_at = @out_for_delivery_at ||= time(:out_for_delivery_at)
30
+ def pending_appointment_since = @pending_appointment_since ||= date(:pending_appointment_since)
31
+ def picked_up_at = @picked_up_at ||= time(:picked_up_at)
32
+ def pickup_driver_assigned_at = @pickup_driver_assigned_at ||= time(:pickup_driver_assigned_at)
33
+ def pickup_on = @pickup_on ||= date(:pickup_on)
34
+ def pro = @pro ||= attribute(:pro)
35
+ def receiver_address = @receiver_address ||= fetch_association(:receiver_address)
36
+ def receiver_name = @receiver_name ||= attribute(:receiver_name)
37
+ def shipper_address = @shipper_address ||= fetch_association(:shipper_address)
38
+ def shipper_bol = @shipper_bol ||= attribute(:shipper_bol)
39
+ def shipper_name = @shipper_name ||= attribute(:shipper_name)
40
+ def shipper_reference = @shipper_reference ||= attribute(:shipper_reference)
41
+ def status = @status ||= attribute(:status)&.to_sym
42
+ def total_charges_cents = @total_charges_cents ||= attribute(:total_charges_cents)
43
+ def total_charges_currency = @total_charges_currency ||= attribute(:total_charges_currency)
44
+ def truckload? = attribute(:truckload)
45
+ def updated_at = @updated_at ||= time(:updated_at)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,38 @@
1
+ module FreightKit
2
+ module Platforms
3
+ class NextTMS
4
+ # Every {NextTMS} subclass gets its own copy of these models, bound to its domain, e.g.
5
+ #
6
+ # FreightKit::Carriers::JFJTransportation::ORM.credentials = FreightKit::Credential.new(type: :api_key, api_key:)
7
+ # FreightKit::Carriers::JFJTransportation::ORM::Shipment.where(status: :delivered).order(delivered_at: :desc)
8
+ module ORM
9
+ MODELS = %i[Address BrokerCarrier BrokerCustomer Carrier Customer Invoice Shipment].freeze
10
+
11
+ # Methods of a carrier's ORM namespace, e.g. `FreightKit::Carriers::JFJTransportation::ORM`.
12
+ module Namespace
13
+ attr_reader :credentials, :platform_class
14
+
15
+ def credentials=(credentials)
16
+ @credentials = credentials
17
+ @platform = nil
18
+ end
19
+
20
+ def platform = @platform ||= platform_class.new(credentials || raise(ArgumentError, "#{name}.credentials is not set"))
21
+ end
22
+
23
+ class << self
24
+ # @param platform_class [Class<NextTMS>]
25
+ # @return [Module] `platform_class::ORM`, with a subclass of each of {MODELS}.
26
+ def define(platform_class)
27
+ namespace = Module.new.extend(Namespace)
28
+ namespace.instance_variable_set(:@platform_class, platform_class)
29
+ platform_class.const_set(:ORM, namespace)
30
+
31
+ MODELS.each { |model| namespace.const_set(model, Class.new(const_get(model)).tap { |orm_class| orm_class.orm = namespace }) }
32
+ namespace
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end