Class: FreightKit::Next

Inherits:
Platform show all
Defined in:
lib/freight_kit/platforms/next.rb

Direct Known Subclasses

JFJTransportation

Constant Summary collapse

REACTIVE_FREIGHT_PLATFORM =
true
JSON_HEADERS =
{
  Accept: 'application/json',
  'Content-Type': 'application/json',
  charset: 'utf-8'
}.freeze

Constants inherited from Carrier

Carrier::BOL_NUMBER_TRACKING_URL_TEMPLATE, Carrier::NUMBERS, Carrier::ORDER_NUMBER_TRACKING_URL_TEMPLATE, Carrier::PICKUP_NUMBER_TRACKING_URL_TEMPLATE, Carrier::PO_NUMBER_TRACKING_URL_TEMPLATE, Carrier::TRACKING_NUMBER_TRACKING_URL_TEMPLATE, Carrier::VALID_BOL_NUMBER_REGEX, Carrier::VALID_ORDER_NUMBER_REGEX, Carrier::VALID_PICKUP_NUMBER_REGEX, Carrier::VALID_PO_NUMBER_REGEX, Carrier::VALID_TRACKING_NUMBER_REGEX

Instance Attribute Summary

Attributes inherited from Carrier

#conf, #credentials, #customer_location, #last_request, #rates_with_excessive_length_fees, #tariff, #tmpdir

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Platform

#initialize

Methods inherited from Carrier

#available_services, #bol, bol_requires_tracking_number?, #cancel_shipment, #create_pickup, default_location, #fetch_credential, #find_estimate, #find_locations, #find_rates, find_rates_with_declared_value?, #find_tracking_info, #find_tracking_number_from_pickup_number, implemented?, #initialize, maximum_address_field_length, maximum_height, maximum_weight, minimum_length_for_overlength_fees, #overlength_fee, overlength_fees_require_tariff?, pickup_number_is_tracking_number?, #pod, requirements, #scanned_bol, #serviceable_accessorials?, tracking_url_template, #valid_credentials?, valid_number_regex, #valid_tracking_number?, #validate_packages

Constructor Details

This class inherits a constructor from FreightKit::Platform

Class Method Details

.required_credential_typesObject



6
7
8
# File 'lib/freight_kit/platforms/next.rb', line 6

def required_credential_types
  %i[api]
end

Instance Method Details

#base_urlObject



77
78
79
# File 'lib/freight_kit/platforms/next.rb', line 77

def base_url
  "https://#{@conf.dig(:api, :domain)}#{@conf.dig(:api, :prefix)}#{@conf.dig(:api, :scope, @options[:scope])}"
end

#build_request(action, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/freight_kit/platforms/next.rb', line 40

def build_request(action, options = {})
  headers = JSON_HEADERS
  headers = headers.merge(options[:headers]) if options[:headers].present?
  body = options[:body].to_json if options[:body].present?

  unless action == :authenticate
    set_auth_token
    headers = headers.merge(token)
  end

  request = {
              url: build_url(action, options),
              headers:,
              method: @conf.dig(:api, :methods, action),
              body:
            }

  save_request(request)
  request
end

#build_url(action, options = {}) ⇒ Object

protected



32
33
34
35
36
37
38
# File 'lib/freight_kit/platforms/next.rb', line 32

def build_url(action, options = {})
  url = ''.dup
  url += "#{base_url}#{@conf.dig(:api, :scopes, options[:scope])}#{@conf.dig(:api, :endpoints, action)}"
  url = url.sub(@conf.dig(:api, :scopes, options[:scope]), '') if action == :authenticate
  url += options[:params] if options[:params].present?
  url
end

#commit(request) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/freight_kit/platforms/next.rb', line 61

def commit(request)
  url = request[:url]
  headers = request[:headers]
  method = request[:method]
  body = request[:body]

  response = case method
             when :post
               HTTParty.post(url, headers:, body:)
             else
               HTTParty.get(url, headers:)
             end

  JSON.parse(response.body) if response&.body
end

#set_auth_tokenObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/freight_kit/platforms/next.rb', line 81

def set_auth_token
  return @auth_token if @auth_token.present?

  api_credentials = fetch_credential(:api)

  request = build_request(
    :authenticate,
    body: { email: api_credentials.username, password: api_credentials.password },
  )

  response = commit(request)
  @auth_token = response['auth_token']
end

#show(id) ⇒ Object

Rates



23
24
25
26
# File 'lib/freight_kit/platforms/next.rb', line 23

def show(id)
  request = build_request(:show, params: "/#{id}")
  commit(request)
end

#tokenObject



95
96
97
# File 'lib/freight_kit/platforms/next.rb', line 95

def token
  { Authorization: "Bearer #{@auth_token}" }
end