hermes_api 0.4.0 → 0.5.2

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: cfeafd10d08ea6ffa236a36b66d94dc11f3b7883931aade9995a93488d9a17e9
4
- data.tar.gz: 3d4fb22d7d6f794988fedbefc1d79c384b744a13085fb9b9e6073fed00bd7638
3
+ metadata.gz: 87f92e8ae4fdbdc63d9e57027707610b8e46beb0017887e3653b30fb7100917e
4
+ data.tar.gz: 9943983c9eb55d666baf6941450a5c960df500af17a653bd4f4c652b949b581d
5
5
  SHA512:
6
- metadata.gz: 4fc203dac84c6484c990dcca4d2c070c083d8f0c588d465daa6c3de2eb0a02a0c2523376d9d30e9d37e2dc518c20b57b76c63c80070c5c45dc90404e6a5b46c8
7
- data.tar.gz: 59160916b4fe767ead70309ef93f7c67c0392c577159ccde3f6d8fb8bded452194401db92781636cce250be07b614723461729d61fc9144fac4b153659051c45
6
+ metadata.gz: b0424b7e0049fd9fba5c542b22007b6232e5188f11c7711d4fcdab8d8255957d7382a04a8a7e66cb8ed0ef226021971ae0ebd76f1808f125c204ec81f5939038
7
+ data.tar.gz: 8118f608d127c49d58c843136d6269553dcabfccff2e05fe703dbbbafd970dfe86f280321252d54a8b7f38f17ed0c4977604094c2134696cb6bdad9c26fec81f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hermes_api (0.3.1)
4
+ hermes_api (0.5.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
- config.env = :test
5
+ config.env = :production
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,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
@@ -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 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 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"
@@ -23,6 +23,8 @@ module HermesAPI
23
23
 
24
24
  def self.find(barcode)
25
25
  uniqueId = format.decode(connection.get("#{prefix}/search/#{barcode}", headers).body).first
26
+ return nil if uniqueId.nil?
27
+
26
28
  find_single("", params: {uniqueIds: uniqueId})
27
29
  end
28
30
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HermesAPI
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.2"
5
5
  end
data/lib/hermes_api.rb CHANGED
@@ -5,6 +5,7 @@ require "active_resource"
5
5
 
6
6
  module HermesAPI
7
7
  require "hermes_api/cache"
8
+ require "hermes_api/bearer_auth"
8
9
  require "hermes_api/configuration"
9
10
  require "hermes_api/creation_error"
10
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.0
4
+ version: 0.5.2
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-11 00:00:00.000000000 Z
11
+ date: 2021-08-31 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