hermes_api 0.4.1 → 0.5.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: b071e3f67cee6a1475d31ef398886a256a6caa74af59f57a4cd94b35982298e0
4
+ data.tar.gz: 0501c14dc98e8de9e9c1d3bdb703e19f69d41f55a048249ade0ab1cb102793ed
5
5
  SHA512:
6
- metadata.gz: 626e0a3607ea22c200fa724f6ed5bd09cea386c8563a8d918d5a05fd14b1ae05fa47f8787f94500ca20b9ddbf5cd09b27c5160c0eac6bf702b5afbf67dac0b18
7
- data.tar.gz: fca544bc66a51feea9ca45e9a84245c2e5da4db47d96853c5dd026f990b103e9e0e55d338da8f6a84663e1180d6f9593e2ac22a52f5ce2efb884efbab9533131
6
+ metadata.gz: 7dd454207d4bcb9eea759890d4243b9486fbf704c29117614e51d1654621bbcde2c458916a0e89e6b1bd884cc65d40f41d113dcbe460c0f654b7a445e114a1bc
7
+ data.tar.gz: c6f727e2435134d77e00b856c61bd08467c374dd6cb95a58b9c517871fe325d50f2e468aad228a5f09b78958e507cff404216a32c206670763c18215138548b5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hermes_api (0.4.0)
4
+ hermes_api (0.4.1)
5
5
  activeresource (>= 4.1.0, < 6.0.0)
6
6
 
7
7
  GEM
data/lib/dev/config.rb CHANGED
@@ -2,12 +2,7 @@ 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
8
6
  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
7
  end
13
8
  end
@@ -0,0 +1,38 @@
1
+ module HermesAPI
2
+ module BearerAuth
3
+ def with_oauth_session(api_key, client_id, client_secret)
4
+ headers["apikey"] = api_key
5
+ connection.bearer_token = fetch_token(client_id, client_secret)
6
+ response = yield
7
+ connection.bearer_token = nil
8
+ headers["apikey"] = nil
9
+ response
10
+ rescue ActiveResource::UnauthorizedAccess => e
11
+ clear_token_cache(client_id, client_secret)
12
+ raise e
13
+ end
14
+
15
+ def oauth_audience
16
+ prefix.match(/^\/?([^\/]*)/).captures.first
17
+ end
18
+
19
+ def clear_token_cache(client_id, client_secret)
20
+ cache_key = "HermesAPI/#{client_id}/#{client_secret}/#{oauth_audience}/oauth_token"
21
+ HermesAPI.cache.delete(cache_key)
22
+ end
23
+
24
+ def fetch_token(client_id, client_secret)
25
+ cache_key = "HermesAPI/#{client_id}/#{client_secret}/#{oauth_audience}/oauth_token"
26
+ cached_token = HermesAPI.cache.read(cache_key)
27
+ return cached_token if cached_token
28
+
29
+ response = OAuth.create(audience: oauth_audience, client_id: client_id, client_secret: client_secret)
30
+ HermesAPI.cache.write(
31
+ cache_key,
32
+ response.access_token,
33
+ expires_in: response.expires_in - 15 # clear cache earlier
34
+ )
35
+ response.access_token
36
+ end
37
+ end
38
+ 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,9 @@ 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
-
31
- HermesAPI::JsonBase.headers["apikey"] = config.api_key
32
28
  end
33
29
 
34
30
  def configure
@@ -2,5 +2,9 @@ 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
5
9
  end
6
10
  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
@@ -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
 
@@ -1,9 +1,56 @@
1
- "
2
- Create return label(s).
3
- You can choose to create a batch of return labels by passing in multiple collectionRoutingRequestEntry.
4
- "
5
1
  module HermesAPI
6
2
  class ReturnLabel < Base
3
+ # Create return label(s).
4
+ # You can choose to create a batch of return labels by passing in multiple collectionRoutingRequestEntry.
5
+ # Example:
6
+ # HermesAPI::Base.with_session("username", "password") do
7
+ # request_body = {clientId: "1234",
8
+ # clientName: "Life",
9
+ # childClientId: "",
10
+ # childClientName: "",
11
+ # sourceOfRequest: "CLIENTWS",
12
+ # collectionRoutingRequestEntries: [{ #collectionRoutingRequestEntry
13
+ # customer: {
14
+ # address: {
15
+ # firstName: "Leonie", lastName: "E", houseName: "2", streetName: "Street",
16
+ # addressLine1: "2 Street", addressLine2: "Fulham", postCode: "SW6 6EL",
17
+ # city: "London", region: "", countryCode: "GB"
18
+ # },
19
+ # mobilePhoneNo: "+447884571522",
20
+ # email: "leonie@london.com",
21
+ # customerReference1: "8284"
22
+ # },
23
+ # countryOfOrigin: "GB"
24
+ # }]}
25
+ # @order = HermesAPI::ReturnLabel.new(request_body)
26
+ # @order.save
27
+ #
28
+ # # Request for a single print in store QR code by wrapping in an oauth session block, only work with 1 label.
29
+ # # To request a batch of QR codes, use the HermesAPI::PrintInStoreQrCode#create directly.
30
+ #
31
+ # HermesAPI::PrintInStoreQrCode.with_oauth_session("api_key", "client_id/auth_id", "client_secret/auth_secret") do
32
+ # @order.request_print_in_store_qr_code(
33
+ # deliveryAddress: {
34
+ # name: "Andy",
35
+ # addressLine1: "7 Street",
36
+ # addressLine2: "Fulham",
37
+ # countryCode: "GB",
38
+ # postcode: "SW6 6EL"
39
+ # },
40
+ # dimensions: {
41
+ # depth: 15,
42
+ # length: 20,
43
+ # width: 15,
44
+ # weight: 1
45
+ # },
46
+ # value: {
47
+ # currency: "GBP",
48
+ # amount: 10
49
+ # }
50
+ # )
51
+ # end
52
+ # end
53
+ #
7
54
  self.prefix = "/routing/service/rest/v4/createReturnBarcodeAndLabel"
8
55
  self.element_name = ""
9
56
 
@@ -79,7 +126,6 @@ module HermesAPI
79
126
  barcode = carrier.barcode1
80
127
  customer = collectionRoutingRequestEntries[0].customer
81
128
  address = customer.address
82
-
83
129
  self.print_in_store_qr_code = PrintInStoreQrCode.create(
84
130
  customer: {
85
131
  customerReference1: customer.customerReference1
@@ -112,27 +158,27 @@ module HermesAPI
112
158
 
113
159
  def request_print_in_store_qr_code_error_message
114
160
  <<~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
- )
161
+ Missing attributes
162
+ Example:
163
+ HermesAPI::ReturnLabel#request_print_in_store_qr_code(
164
+ dimensions: {
165
+ depth: 15,
166
+ length: 20,
167
+ width: 15,
168
+ weight: 1
169
+ },
170
+ value: {
171
+ currency: 'GBP',
172
+ amount: 10
173
+ },
174
+ deliveryAddress: {
175
+ name: 'Don Joe',
176
+ addressLine1: 'Real Logic',
177
+ addressLine2: '4-4 Ridings Park, Eastern Way',
178
+ countryCode: 'GB',
179
+ postcode: 'WS117FJ'
180
+ }
181
+ )
136
182
  HEREDOC
137
183
  end
138
184
  end
@@ -1,6 +1,10 @@
1
1
  module HermesAPI
2
2
  class TrackingEvent < JsonBase
3
- extend HermesAPI::BearerTokenSetup
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
4
8
 
5
9
  self.element_name = ""
6
10
  self.prefix = "/client-tracking-api/v1/events"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HermesAPI
4
- VERSION = "0.4.1"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/hermes_api.rb CHANGED
@@ -5,7 +5,7 @@ require "active_resource"
5
5
 
6
6
  module HermesAPI
7
7
  require "hermes_api/cache"
8
- require "hermes_api/bearer_token_setup"
8
+ require "hermes_api/bearer_auth"
9
9
  require "hermes_api/configuration"
10
10
  require "hermes_api/creation_error"
11
11
  require "hermes_api/connection"
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.5.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-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeresource
@@ -151,7 +151,7 @@ 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
@@ -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