hermes_api 0.4.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a6253995c5e13c6cc4c920b3825dff5563bef6a7f9ffc69aaafcb7e918378c6
4
- data.tar.gz: 0cda194a1f1464eebb3c98634fdd31fcf9b8347e2c05028ca0fbd1998bae387b
3
+ metadata.gz: e46834a6a9ce66e5a73e5a352ebe1384e53cadb20933d4e51cbcf93bfa1fc9e8
4
+ data.tar.gz: cc774117acd165f1a01ed476ea88b65eeb68b0a4b37b2122e199bfcdd55be7d8
5
5
  SHA512:
6
- metadata.gz: 626e0a3607ea22c200fa724f6ed5bd09cea386c8563a8d918d5a05fd14b1ae05fa47f8787f94500ca20b9ddbf5cd09b27c5160c0eac6bf702b5afbf67dac0b18
7
- data.tar.gz: fca544bc66a51feea9ca45e9a84245c2e5da4db47d96853c5dd026f990b103e9e0e55d338da8f6a84663e1180d6f9593e2ac22a52f5ce2efb884efbab9533131
6
+ metadata.gz: 05c7e2f756c0c68c8dfa535cbb8cbf064582352146f346bca765bfd31c146b94e4045b0f2f49170e97d2561ca2f5ba4012161632e110afdcd12e149e34aeb56c
7
+ data.tar.gz: 1df67df9cd32108bbf3de5da31b1b25b3f646f4394fc5fd4cd390d303065b3f3f16de0532b30ed6ac1f2434db9d55cb7a0d7e611ed32b256d5064bf9a904dbc3
data/Gemfile.lock CHANGED
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hermes_api (0.4.0)
4
+ hermes_api (0.5.2)
5
5
  activeresource (>= 4.1.0, < 6.0.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activemodel (6.1.4)
11
- activesupport (= 6.1.4)
10
+ activemodel (6.1.4.1)
11
+ activesupport (= 6.1.4.1)
12
12
  activemodel-serializers-xml (1.0.2)
13
13
  activemodel (> 5.x)
14
14
  activesupport (> 5.x)
@@ -17,7 +17,7 @@ GEM
17
17
  activemodel (>= 5.0, < 7)
18
18
  activemodel-serializers-xml (~> 1.0)
19
19
  activesupport (>= 5.0, < 7)
20
- activesupport (6.1.4)
20
+ activesupport (6.1.4.1)
21
21
  concurrent-ruby (~> 1.0, >= 1.0.2)
22
22
  i18n (>= 1.6, < 2)
23
23
  minitest (>= 5.1)
data/bin/console CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
+ require "pry"
4
5
  require "bundler/setup"
5
6
  require "dotenv/load"
6
7
  require "dev/zeitwerk_loader"
@@ -11,5 +12,4 @@ require "hermes_api"
11
12
  set_config
12
13
 
13
14
  # (If you use this, don't forget to add pry to your Gemfile!)
14
- require "pry"
15
15
  Pry.start
data/lib/dev/config.rb CHANGED
@@ -2,12 +2,9 @@ require "dotenv/load"
2
2
 
3
3
  def set_config
4
4
  HermesAPI.configure do |config|
5
- config.user = ENV["HERMES_API_USER"]
6
- config.password = ENV["HERMES_API_PASSWORD"]
7
5
  config.env = :test
6
+ # Set to production when testing HermesAPI::TrackingEvent resource
7
+ # config.env = :production
8
8
  config.proxy = ENV["HERMES_API_PROXY"]
9
- config.auth_id = ENV["HERMES_API_AUTH_ID"]
10
- config.auth_secret = ENV["HERMES_API_AUTH_SECRET"]
11
- config.api_key = ENV["HERMES_API_KEY"]
12
9
  end
13
10
  end
@@ -0,0 +1,40 @@
1
+ module HermesAPI
2
+ module BearerAuth
3
+ def with_oauth_session(api_key, client_id, client_secret)
4
+ existing_apikey = headers["apikey"]
5
+ existing_bearer_token = connection.bearer_token
6
+ headers["apikey"] = api_key
7
+ connection.bearer_token = fetch_token(client_id, client_secret)
8
+ response = yield
9
+ headers["apikey"] = existing_apikey
10
+ connection.bearer_token = existing_bearer_token
11
+ response
12
+ rescue ActiveResource::UnauthorizedAccess => e
13
+ clear_token_cache(client_id, client_secret)
14
+ raise e
15
+ end
16
+
17
+ def oauth_audience
18
+ prefix.match(/^\/?([^\/]*)/).captures.first
19
+ end
20
+
21
+ def clear_token_cache(client_id, client_secret)
22
+ cache_key = "HermesAPI/#{client_id}/#{client_secret}/#{oauth_audience}/oauth_token"
23
+ HermesAPI.cache.delete(cache_key)
24
+ end
25
+
26
+ def fetch_token(client_id, client_secret)
27
+ cache_key = "HermesAPI/#{client_id}/#{client_secret}/#{oauth_audience}/oauth_token"
28
+ cached_token = HermesAPI.cache.read(cache_key)
29
+ return cached_token if cached_token
30
+
31
+ response = OAuth.create(audience: oauth_audience, client_id: client_id, client_secret: client_secret)
32
+ HermesAPI.cache.write(
33
+ cache_key,
34
+ response.access_token,
35
+ expires_in: response.expires_in - 15 # clear cache earlier
36
+ )
37
+ response.access_token
38
+ end
39
+ end
40
+ end
@@ -1,10 +1,5 @@
1
1
  module HermesAPI
2
2
  mattr_accessor :cache
3
3
 
4
- self.cache = if defined?(Rails) && Rails.respond_to?(:cache) &&
5
- Rails.cache.is_a?(ActiveSupport::Cache::Store)
6
- Rails.cache
7
- else
8
- ActiveSupport::Cache::MemoryStore.new
9
- end
10
- end
4
+ self.cache = ActiveSupport::Cache::MemoryStore.new
5
+ end
@@ -1,6 +1,6 @@
1
1
  module HermesAPI
2
2
  class Configuration
3
- attr_accessor :proxy, :env, :user, :password, :auth_id, :auth_secret, :api_key
3
+ attr_accessor :proxy, :env
4
4
  end
5
5
 
6
6
  PRODUCTION_SITE = "https://www.hermes-europe.co.uk"
@@ -10,9 +10,9 @@ module HermesAPI
10
10
  OAUTH_TESTING_SITE = "https://hermes-client-integration-pre.eu.auth0.com"
11
11
 
12
12
  JSON_PRODUCTION_SITE = "https://api.hermesworld.co.uk"
13
- JSON_TESTING_SITE = "https://api.hermesworld.co.uk"
14
13
  # temporarily disabled until it is fixed
15
14
  # JSON_TESTING_SITE = "https://hermeslive-pre-prod.apigee.net"
15
+ JSON_TESTING_SITE = JSON_PRODUCTION_SITE
16
16
 
17
17
  class << self
18
18
  def config
@@ -22,13 +22,13 @@ module HermesAPI
22
22
  def after_configure
23
23
  HermesAPI::Base.site = config.env.to_s == "production" ? PRODUCTION_SITE : TESTING_SITE
24
24
  HermesAPI::Base.proxy = config.proxy
25
- HermesAPI::Base.user = config.user
26
- HermesAPI::Base.password = config.password
27
25
 
28
26
  HermesAPI::JsonBase.site = config.env.to_s == "production" ? JSON_PRODUCTION_SITE : JSON_TESTING_SITE
29
27
  HermesAPI::OAuth.site = config.env.to_s == "production" ? OAUTH_PRODUCTION_SITE : OAUTH_TESTING_SITE
30
28
 
31
- HermesAPI::JsonBase.headers["apikey"] = config.api_key
29
+ if defined?(Rails) && Rails.respond_to?(:cache) && Rails.cache.is_a?(ActiveSupport::Cache::Store)
30
+ HermesAPI.cache = Rails.cache
31
+ end
32
32
  end
33
33
 
34
34
  def configure
@@ -4,18 +4,50 @@ module HermesAPI
4
4
  ActiveResource::Formats::XmlFormat.decode(@response.body)
5
5
  end
6
6
 
7
- def to_s
8
- response = ActiveResource::Formats::XmlFormat.decode(@response.body)
9
- entries = response.dig("routingResponseEntries", "routingResponseEntry")
7
+ def code
8
+ if entries.is_a? Array
9
+ entries.map do |entry|
10
+ entry.dig("errorMessages", "errorCode")
11
+ end
12
+ else
13
+ entries.dig("errorMessages", "errorCode")
14
+ end
15
+ end
16
+
17
+ alias_method :codes, :code
18
+
19
+ def description
20
+ unless entries.is_a? Array
21
+ entries.dig("errorMessages", "errorDescription")
22
+ end
23
+ end
24
+
25
+ def code_with_descriptions
10
26
  if entries.is_a? Array
11
27
  entries.map do |entry|
12
- error = entry.dig("errorMessages")
13
- "Something went wrong, Error Code: #{error["errorCode"]}, Error Description: #{error["errorDescription"]}."
14
- end.to_s
28
+ code = entry.dig("errorMessages", "errorCode")
29
+ description = entry.dig("errorMessages", "errorDescription")
30
+ "#{code}: #{description}"
31
+ end
32
+ end
33
+ end
34
+
35
+ def to_s
36
+ if entries.is_a? Array
37
+ "#{code_with_descriptions.join(", ")}."
15
38
  else
16
- error = entries.dig("errorMessages")
17
- "Something went wrong. Error Code: #{error["errorCode"]} Error Description: #{error["errorDescription"]}"
39
+ "#{code}: #{description}."
18
40
  end
19
41
  end
42
+
43
+ private
44
+
45
+ def decoded_response
46
+ ActiveResource::Formats::XmlFormat.decode(@response.body)
47
+ end
48
+
49
+ def entries
50
+ decoded_response.dig("routingResponseEntries", "routingResponseEntry")
51
+ end
20
52
  end
21
53
  end
@@ -29,8 +29,13 @@ module HermesAPI
29
29
  super
30
30
  end
31
31
 
32
+ def load(attributes, remove_root = false, persisted = false)
33
+ attributes.deep_transform_keys! { |k| k.to_s.underscore }
34
+ super
35
+ end
36
+
32
37
  def to_xml(options = {})
33
- super({root: self.class.root}.merge(options))
38
+ super({root: self.class.root, camelize: :lower}.merge(options))
34
39
  end
35
40
  end
36
41
  end
@@ -0,0 +1,166 @@
1
+ module HermesAPI
2
+ class DropOffReturnLabel < Base
3
+ # Create return label(s) wheredrop off at ParcelShops.
4
+ # HermesUK doc: https://drive.google.com/file/d/1lVuHd2o4nDWrGkacG0GrvNm7LYVL_bFd/view?usp=sharing
5
+ # You can choose to create a batch of return labels by passing in multiple collectionRoutingRequestEntry.
6
+ # Example:
7
+ # HermesAPI::Base.with_session("username", "password") do
8
+ # request_body = {client_id: "1234",
9
+ # client_name: "Leggings",
10
+ # child_client_id: "",
11
+ # child_client_name: "",
12
+ # source_of_request: "CLIENTWS",
13
+ # collection_routing_request_entries: [{ # collectionRoutingRequestEntry
14
+ # customer: {
15
+ # address: {
16
+ # first_name: "Leonie", lastame: "E", houseName: "2", streetName: "Street",
17
+ # addressLine1: "2 Street", addressLine2: "Fulham", postCode: "SW6 6EL",
18
+ # city: "London", region: "", countryCode: "GB"
19
+ # },
20
+ # mobilePhoneNo: "+447884571522",
21
+ # email: "leonie@london.com",
22
+ # customerReference1: "8284"
23
+ # },
24
+ # countryOfOrigin: "GB"
25
+ # }]}
26
+ # @order = HermesAPI::DropOffReturnLabel.new(request_body)
27
+ # @order.save
28
+
29
+ # # Request for a single print in store QR code by wrapping in an oauth session block, only work with 1 label.
30
+ # # To request a batch of QR codes, use the HermesAPI::PrintInStoreQrCode#create directly.
31
+
32
+ # HermesAPI::PrintInStoreQrCode.with_oauth_session("api_key", "client_id/auth_id", "client_secret/auth_secret") do
33
+ # @order.request_print_in_store_qr_code(
34
+ # delivery_address: {
35
+ # name: "Andy",
36
+ # address_line1: "7 Street",
37
+ # address_line2: "Fulham",
38
+ # country_code: "GB",
39
+ # postcode: "SW6 6EL"
40
+ # },
41
+ # dimensions: {
42
+ # depth: 15,
43
+ # length: 20,
44
+ # width: 15,
45
+ # weight: 1
46
+ # },
47
+ # value: {
48
+ # currency: "GBP",
49
+ # amount: 10
50
+ # }
51
+ # )
52
+ # end
53
+ # end
54
+
55
+ include ReturnLabelHelper
56
+
57
+ self.prefix = "/routing/service/rest/v4/createReturnBarcodeAndLabel"
58
+ self.element_name = ""
59
+
60
+ DEFAULT_ATTRS = {
61
+ client_id: "",
62
+ client_name: "",
63
+ child_client_id: "",
64
+ child_client_name: "",
65
+ source_of_request: "",
66
+ collection_routing_request_entries: [{
67
+ customer: {
68
+ address: {
69
+ first_name: "",
70
+ last_name: "",
71
+ house_name: "",
72
+ street_name: "",
73
+ address_line1: "",
74
+ post_code: "",
75
+ city: "",
76
+ region: "",
77
+ country_code: ""
78
+ },
79
+ mobile_phone_no: "",
80
+ email: "",
81
+ customer_reference1: ""
82
+ },
83
+ country_of_origin: ""
84
+ }]
85
+ }
86
+
87
+ def request_print_in_store_qr_code(**attrs)
88
+ if missing_required_qr_code_attributes?(attrs)
89
+ raise ArgumentError, request_print_in_store_qr_code_error_message
90
+ end
91
+
92
+ return nil if attributes["routing_response_entries"].blank?
93
+
94
+ entries = routing_response_entries.routing_response_entry
95
+ entry = entries.is_a?(Array) ? entries[0] : entries
96
+ carrier = entry.inbound_carriers.carrier1
97
+ barcode = carrier.barcode1
98
+ customer = collection_routing_request_entries[0].customer
99
+ address = customer.address
100
+
101
+ self.print_in_store_qr_code = PrintInStoreQrCode.create(
102
+ customer: {
103
+ customer_reference1: customer.customer_reference1
104
+ },
105
+ label_type: "RETURN",
106
+ barcode: {
107
+ barcode: barcode.barcode_number,
108
+ barcode_display: barcode.barcode_display
109
+ },
110
+ client: {
111
+ client_id: client_id,
112
+ client_name: client_name
113
+ },
114
+ routing: {
115
+ delivery_method: {
116
+ delivery_method_id: carrier.delivery_method_code,
117
+ delivery_method_description: carrier.delivery_method_desc
118
+ },
119
+ sort_levels: {
120
+ sort_level1: carrier.sort_level1.strip,
121
+ sort_level2: carrier.sort_level2
122
+ }
123
+ },
124
+ service_offers: [],
125
+ **attrs
126
+ )
127
+ end
128
+
129
+ private
130
+
131
+ def missing_required_qr_code_attributes?(attrs)
132
+ ([:dimensions, :value, :delivery_address] - attrs.keys).length > 0
133
+ end
134
+
135
+ def request_print_in_store_qr_code_error_message
136
+ <<~ERR_MESSAGE
137
+ Missing required attributes
138
+ Example:
139
+
140
+ label = HermesAPI::DropOffReturnLabel.new(...)
141
+
142
+ if label.save
143
+ label.request_print_in_store_qr_code(
144
+ dimensions: {
145
+ depth: 15,
146
+ length: 20,
147
+ width: 15,
148
+ weight: 1
149
+ },
150
+ value: {
151
+ currency: 'GBP',
152
+ amount: 10
153
+ },
154
+ delivery_address: {
155
+ name: 'Don Joe',
156
+ address_line1: 'Real Logic',
157
+ address_line2: '4-4 Ridings Park, Eastern Way',
158
+ country_code: 'GB',
159
+ postcode: 'WS117FJ'
160
+ }
161
+ )
162
+ end
163
+ ERR_MESSAGE
164
+ end
165
+ end
166
+ end
@@ -2,5 +2,19 @@ module HermesAPI
2
2
  class JsonBase < ActiveResource::Base
3
3
  self.include_format_in_path = false
4
4
  self.auth_type = :bearer
5
+
6
+ def self.inherited(subclass)
7
+ subclass.extend(BearerAuth)
8
+ end
9
+
10
+ def load(attributes, remove_root = false, persisted = false)
11
+ attributes.deep_transform_keys! { |k| k.to_s.underscore }
12
+ super
13
+ end
14
+
15
+ def to_json(options = {})
16
+ attributes.as_json.deep_transform_keys { |k| k.to_s.camelize(:lower) }
17
+ .to_json(include_root_in_json ? {root: self.class.element_name}.merge(options) : options)
18
+ end
5
19
  end
6
20
  end
@@ -1,17 +1,15 @@
1
1
  module HermesAPI
2
2
  class OAuth < ActiveResource::Base
3
3
  self.include_format_in_path = false
4
- self.element_name=""
5
- self.prefix="/oauth/token"
4
+ self.element_name = ""
5
+ self.prefix = "/oauth/token"
6
6
 
7
7
  def initialize(attributes = {}, persisted = false)
8
8
  attributes = {
9
9
  grant_type: "client_credentials",
10
- client_id: HermesAPI.config.auth_id,
11
- client_secret: HermesAPI.config.auth_secret,
12
10
  **attributes
13
11
  }
14
12
  super
15
13
  end
16
14
  end
17
- end
15
+ end
@@ -0,0 +1,85 @@
1
+ module HermesAPI
2
+ class PickupReturnLabel < Base
3
+ # Create return label(s) with courier pickup request.
4
+ # HermesUK doc: https://drive.google.com/file/d/1lVuHd2o4nDWrGkacG0GrvNm7LYVL_bFd/view?usp=sharing
5
+ # You can choose to create a batch of return labels by passing in multiple collectionRoutingRequestEntry.
6
+ # Example:
7
+ # HermesAPI::Base.with_session("username", "password") do
8
+ # request_body = {clientId: "249",
9
+ # clientName: "Leggings",
10
+ # childClientId: "",
11
+ # childClientName: "",
12
+ # sourceOfRequest: "CLIENTWS",
13
+ # routingStartDate: Time.now.tomorrow.strftime("%Y-%m-%dT%H:%M:%S"),
14
+ # collectionRoutingRequestEntries: [{ # collectionRoutingRequestEntry
15
+ # customer: {
16
+ # address: {
17
+ # firstName: "Leonie", lastName: "E", houseName: "2", streetName: "Street",
18
+ # addressLine1: "2 Street", addressLine2: "Fulham", postCode: "SW6 6EL",
19
+ # city: "London", region: "", countryCode: "GB"
20
+ # },
21
+ # mobilePhoneNo: "+447884571522",
22
+ # email: "leonie@london.com",
23
+ # customerReference1: "8284"
24
+ # },
25
+ # parcel: {
26
+ # weight: 1,
27
+ # length: 10,
28
+ # width: 10,
29
+ # depth: 10,
30
+ # girth: 0,
31
+ # combinedDimension: 0,
32
+ # volume: 0,
33
+ # value: 10,
34
+ # description: "Parcel"
35
+ # },
36
+ # countryOfOrigin: "GB"
37
+ # }]}
38
+ # @order = HermesAPI::PickupReturnLabel.new(request_body)
39
+ # @order.save
40
+ # end
41
+
42
+ include ReturnLabelHelper
43
+
44
+ self.prefix = "/routing/service/rest/v4/routeCollectionCreatePreadviceReturnBarcodeAndLabel"
45
+ self.element_name = ""
46
+
47
+ DEFAULT_ATTRS = {
48
+ client_id: "",
49
+ client_name: "",
50
+ child_client_id: "",
51
+ child_client_name: "",
52
+ source_of_request: "",
53
+ collection_routing_request_entries: [{
54
+ customer: {
55
+ address: {
56
+ first_name: "",
57
+ last_name: "",
58
+ house_name: "",
59
+ street_name: "",
60
+ address_line1: "",
61
+ post_code: "",
62
+ city: "",
63
+ region: "",
64
+ country_code: ""
65
+ },
66
+ parcel: {
67
+ weight: 1,
68
+ length: 10,
69
+ width: 10,
70
+ depth: 10,
71
+ girth: 0,
72
+ combined_dimension: 0,
73
+ volume: 0,
74
+ value: 10,
75
+ description: "Parcel"
76
+ },
77
+ mobile_phone_no: "",
78
+ email: "",
79
+ customer_reference1: ""
80
+ },
81
+ country_of_origin: ""
82
+ }]
83
+ }
84
+ end
85
+ end
@@ -1,7 +1,5 @@
1
1
  module HermesAPI
2
2
  class PrintInStoreQrCode < JsonBase
3
- extend HermesAPI::BearerTokenSetup
4
-
5
3
  self.element_name = ""
6
4
  self.prefix = "/client-print-in-store-api/v1/references"
7
5
 
@@ -11,8 +9,8 @@ module HermesAPI
11
9
  super
12
10
  end
13
11
 
14
- def qr_code
15
- base64_data = as_json.dig("qrCode", "base64EncodedBytes")
12
+ def qr_code_image
13
+ base64_data = as_json.dig("qr_code", "base64_encoded_bytes")
16
14
  Base64.decode64(base64_data) if base64_data
17
15
  end
18
16
  end
@@ -1,7 +1,11 @@
1
1
  module HermesAPI
2
2
  class TrackingEvent < JsonBase
3
- extend HermesAPI::BearerTokenSetup
4
-
3
+ # # Retrieve TrackingEvents by wrapping in an oauth session block
4
+ #
5
+ # HermesAPI::TrackingEvent.with_oauth_session("api_key", "client_id/auth_id", "client_secret/auth_secret") do
6
+ # HermesAPI::TrackingEvent.where(barcode: "123456789")
7
+ # end
8
+ #
5
9
  self.element_name = ""
6
10
  self.prefix = "/client-tracking-api/v1/events"
7
11
 
@@ -1,5 +1,9 @@
1
1
  module HermesAPI
2
2
  class WebTracking < ActiveResource::Base
3
+ # # Retrieve TrackingEvents from web site: https://www.myhermes.co.uk/track
4
+
5
+ # HermesAPI::WebTracking.find("1234512345")
6
+
3
7
  self.element_name = ""
4
8
  self.site = "https://api.hermesworld.co.uk"
5
9
  self.prefix = "/enterprise-tracking-api/v1/parcels"
@@ -18,11 +22,14 @@ module HermesAPI
18
22
 
19
23
  def load(attributes, remove_root = false, persisted = false)
20
24
  attributes = attributes.dig("results", 0)
21
- super(attributes)
25
+ attributes.deep_transform_keys! { |k| k.to_s.underscore }
26
+ super
22
27
  end
23
28
 
24
29
  def self.find(barcode)
25
30
  uniqueId = format.decode(connection.get("#{prefix}/search/#{barcode}", headers).body).first
31
+ return nil if uniqueId.nil?
32
+
26
33
  find_single("", params: {uniqueIds: uniqueId})
27
34
  end
28
35
  end
@@ -0,0 +1,41 @@
1
+ module HermesAPI
2
+ module ReturnLabelHelper
3
+ module ClassMethods
4
+ def root
5
+ :collectionRoutingRequest
6
+ end
7
+ end
8
+
9
+ def self.included(base_klass)
10
+ base_klass.extend(ClassMethods)
11
+ end
12
+
13
+ def labels
14
+ entries = routing_response_entries.routing_response_entry
15
+ if entries.is_a?(Array)
16
+ entries.map do |entry|
17
+ Base64.decode64(entry.inbound_carriers.label_image)
18
+ end
19
+ else
20
+ Base64.decode64(entries.inbound_carriers.label_image)
21
+ end
22
+ rescue NameError
23
+ end
24
+
25
+ alias_method :label, :labels
26
+
27
+ def tracking_numbers
28
+ entries = routing_response_entries.routing_response_entry
29
+ if entries.is_a?(Array)
30
+ entries.map do |entry|
31
+ entry.inbound_carriers.carrier1.barcode1.barcode_number
32
+ end
33
+ else
34
+ entries.inbound_carriers.carrier1.barcode1.barcode_number
35
+ end
36
+ rescue NameError
37
+ end
38
+
39
+ alias_method :tracking_number, :tracking_numbers
40
+ end
41
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HermesAPI
4
- VERSION = "0.4.1"
4
+ VERSION = "0.7.0"
5
5
  end
data/lib/hermes_api.rb CHANGED
@@ -4,17 +4,19 @@ require_relative "hermes_api/version"
4
4
  require "active_resource"
5
5
 
6
6
  module HermesAPI
7
+ require "hermes_api/bearer_auth"
7
8
  require "hermes_api/cache"
8
- require "hermes_api/bearer_token_setup"
9
9
  require "hermes_api/configuration"
10
- require "hermes_api/creation_error"
11
10
  require "hermes_api/connection"
11
+ require "hermes_api/creation_error"
12
12
 
13
- require "hermes_api/resources/base"
14
- require "hermes_api/resources/return_label"
13
+ require "hermes_api/shared/return_label_helper"
15
14
 
16
- require "hermes_api/resources/o_auth"
15
+ require "hermes_api/resources/base"
16
+ require "hermes_api/resources/drop_off_return_label"
17
17
  require "hermes_api/resources/json_base"
18
+ require "hermes_api/resources/o_auth"
19
+ require "hermes_api/resources/pickup_return_label"
18
20
  require "hermes_api/resources/print_in_store_qr_code"
19
21
  require "hermes_api/resources/tracking_event"
20
22
  require "hermes_api/resources/web_tracking"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hermes_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Chong
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-24 00:00:00.000000000 Z
11
+ date: 2021-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeresource
@@ -151,18 +151,20 @@ files:
151
151
  - lib/dev/config.rb
152
152
  - lib/dev/zeitwerk_loader.rb
153
153
  - lib/hermes_api.rb
154
- - lib/hermes_api/bearer_token_setup.rb
154
+ - lib/hermes_api/bearer_auth.rb
155
155
  - lib/hermes_api/cache.rb
156
156
  - lib/hermes_api/configuration.rb
157
157
  - lib/hermes_api/connection.rb
158
158
  - lib/hermes_api/creation_error.rb
159
159
  - lib/hermes_api/resources/base.rb
160
+ - lib/hermes_api/resources/drop_off_return_label.rb
160
161
  - lib/hermes_api/resources/json_base.rb
161
162
  - lib/hermes_api/resources/o_auth.rb
163
+ - lib/hermes_api/resources/pickup_return_label.rb
162
164
  - lib/hermes_api/resources/print_in_store_qr_code.rb
163
- - lib/hermes_api/resources/return_label.rb
164
165
  - lib/hermes_api/resources/tracking_event.rb
165
166
  - lib/hermes_api/resources/web_tracking.rb
167
+ - lib/hermes_api/shared/return_label_helper.rb
166
168
  - lib/hermes_api/version.rb
167
169
  homepage: https://github.com/PostCo/hermes_api
168
170
  licenses:
@@ -1,23 +0,0 @@
1
- module HermesAPI
2
- module BearerTokenSetup
3
- def connection(refresh = false)
4
- connection = super
5
- connection.bearer_token = fetch_token
6
- connection
7
- end
8
-
9
- def fetch_token
10
- oauth_audience = prefix.match(/^\/?([^\/]*)/).captures.first
11
- cached_token = HermesAPI.cache.read("#{oauth_audience}/oauth_token")
12
- return cached_token if cached_token
13
-
14
- response = OAuth.create(audience: oauth_audience)
15
- HermesAPI.cache.write(
16
- "#{oauth_audience}/oauth_token",
17
- response.access_token,
18
- expires_in: response.expires_in - 15 # clear cache earlier
19
- )
20
- response.access_token
21
- end
22
- end
23
- end
@@ -1,139 +0,0 @@
1
- "
2
- Create return label(s).
3
- You can choose to create a batch of return labels by passing in multiple collectionRoutingRequestEntry.
4
- "
5
- module HermesAPI
6
- class ReturnLabel < Base
7
- self.prefix = "/routing/service/rest/v4/createReturnBarcodeAndLabel"
8
- self.element_name = ""
9
-
10
- DEFAULT_ATTRS = {
11
- clientId: "",
12
- clientName: "",
13
- childClientId: "",
14
- childClientName: "",
15
- sourceOfRequest: "",
16
- collectionRoutingRequestEntries: [{
17
- customer: {
18
- address: {
19
- firstName: "",
20
- lastName: "",
21
- houseName: "",
22
- streetName: "",
23
- addressLine1: "",
24
- postCode: "",
25
- city: "",
26
- region: "",
27
- countryCode: ""
28
- },
29
- mobilePhoneNo: "",
30
- email: "",
31
- customerReference1: ""
32
- },
33
- countryOfOrigin: ""
34
- }]
35
- }
36
-
37
- def self.root
38
- :collectionRoutingRequest
39
- end
40
-
41
- def labels
42
- entries = routingResponseEntries.routingResponseEntry
43
- if entries.is_a?(Array)
44
- entries.map do |entry|
45
- Base64.decode64(entry.inboundCarriers.labelImage)
46
- end
47
- else
48
- Base64.decode64(entries.inboundCarriers.labelImage)
49
- end
50
- rescue NameError
51
- end
52
-
53
- alias_method :label, :labels
54
-
55
- def tracking_numbers
56
- entries = routingResponseEntries.routingResponseEntry
57
- if entries.is_a?(Array)
58
- entries.map do |entry|
59
- entry.inboundCarriers.carrier1.barcode1.barcodeNumber
60
- end
61
- else
62
- entries.inboundCarriers.carrier1.barcode1.barcodeNumber
63
- end
64
- rescue NameError
65
- end
66
-
67
- alias_method :tracking_number, :tracking_numbers
68
-
69
- def request_print_in_store_qr_code(**attrs)
70
- if ([:dimensions, :value, :deliveryAddress] - attrs.keys).length > 0
71
- raise ArgumentError, request_print_in_store_qr_code_error_message
72
- end
73
-
74
- return nil if attributes["routingResponseEntries"].blank?
75
-
76
- entries = routingResponseEntries.routingResponseEntry
77
- entry = entries.is_a?(Array) ? entries[0] : entries
78
- carrier = entry.inboundCarriers.carrier1
79
- barcode = carrier.barcode1
80
- customer = collectionRoutingRequestEntries[0].customer
81
- address = customer.address
82
-
83
- self.print_in_store_qr_code = PrintInStoreQrCode.create(
84
- customer: {
85
- customerReference1: customer.customerReference1
86
- },
87
- labelType: "RETURN",
88
- barcode: {
89
- barcode: barcode.barcodeNumber,
90
- barcodeDisplay: barcode.barcodeDisplay
91
- },
92
- client: {
93
- clientId: clientId,
94
- clientName: clientName
95
- },
96
- routing: {
97
- deliveryMethod: {
98
- deliveryMethodId: carrier.deliveryMethodCode,
99
- deliveryMethodDescription: carrier.deliveryMethodDesc
100
- },
101
- sortLevels: {
102
- sortLevel1: carrier.sortLevel1.strip,
103
- sortLevel2: carrier.sortLevel2
104
- }
105
- },
106
- serviceOffers: [],
107
- **attrs
108
- )
109
- end
110
-
111
- private
112
-
113
- def request_print_in_store_qr_code_error_message
114
- <<~HEREDOC
115
- Missing attributes
116
- Example:
117
- HermesAPI::ReturnLabel#request_print_in_store_qr_code(
118
- dimensions: {
119
- depth: 15,
120
- length: 20,
121
- width: 15,
122
- weight: 1
123
- },
124
- value: {
125
- currency: 'GBP',
126
- amount: 10
127
- },
128
- deliveryAddress: {
129
- name: 'Don Joe',
130
- addressLine1: 'Real Logic',
131
- addressLine2: '4-4 Ridings Park, Eastern Way',
132
- countryCode: 'GB',
133
- postcode: 'WS117FJ'
134
- }
135
- )
136
- HEREDOC
137
- end
138
- end
139
- end