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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/dev/config.rb +0 -5
- data/lib/hermes_api/bearer_auth.rb +38 -0
- data/lib/hermes_api/configuration.rb +2 -6
- data/lib/hermes_api/resources/json_base.rb +4 -0
- data/lib/hermes_api/resources/o_auth.rb +3 -5
- data/lib/hermes_api/resources/print_in_store_qr_code.rb +0 -2
- data/lib/hermes_api/resources/return_label.rb +72 -26
- data/lib/hermes_api/resources/tracking_event.rb +5 -1
- data/lib/hermes_api/version.rb +1 -1
- data/lib/hermes_api.rb +1 -1
- metadata +3 -3
- data/lib/hermes_api/bearer_token_setup.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b071e3f67cee6a1475d31ef398886a256a6caa74af59f57a4cd94b35982298e0
|
4
|
+
data.tar.gz: 0501c14dc98e8de9e9c1d3bdb703e19f69d41f55a048249ade0ab1cb102793ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7dd454207d4bcb9eea759890d4243b9486fbf704c29117614e51d1654621bbcde2c458916a0e89e6b1bd884cc65d40f41d113dcbe460c0f654b7a445e114a1bc
|
7
|
+
data.tar.gz: c6f727e2435134d77e00b856c61bd08467c374dd6cb95a58b9c517871fe325d50f2e468aad228a5f09b78958e507cff404216a32c206670763c18215138548b5
|
data/Gemfile.lock
CHANGED
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
|
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
|
@@ -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,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
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
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
|
-
|
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"
|
data/lib/hermes_api/version.rb
CHANGED
data/lib/hermes_api.rb
CHANGED
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
|
+
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-
|
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/
|
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
|