in-time-ruby 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/in-time-ruby/access_token_generator.rb +57 -0
- data/lib/in-time-ruby/client.rb +86 -0
- data/lib/in-time-ruby/configuration.rb +20 -0
- data/lib/in-time-ruby/connection.rb +40 -0
- data/lib/in-time-ruby/logger.rb +15 -0
- data/lib/in-time-ruby/version.rb +3 -0
- data/lib/in-time-ruby.rb +10 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e9ccd46228dfe6ea40fc609144bd321cf1192cd723b1fb283ed2da1e677e6b6f
|
4
|
+
data.tar.gz: 7a528c53716ed714c2ff8d13d0c46fcdab950c603df369a2f8f6e0fbd9cabdd0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ca21e205e3d60e64a65a2b4c255864e57b9b56f2825dbc957c7a4daca2618c3928b2ecc39b89c9089cf3a1a66d0ad4250ac07f0358a33935f6bfe2bfecf42e0c
|
7
|
+
data.tar.gz: 2efab27a8ce9a5474b536e89bf88f155d1acc28e012c748e26c9c0c69c04f3461b1a77963bedf0f39c266ba6ed11b9f02f7e4abb07f3af0b778e9b65df2ac380
|
@@ -0,0 +1,57 @@
|
|
1
|
+
|
2
|
+
module InTimeRuby
|
3
|
+
class AccessTokenGenerator
|
4
|
+
|
5
|
+
attr_reader :client, :client_id, :client_secret
|
6
|
+
|
7
|
+
def initialize(client_id, client_secret)
|
8
|
+
@client_id = client_id
|
9
|
+
@client_secret = client_secret
|
10
|
+
|
11
|
+
@client = InTimeRuby::Client.new(token_generation_endpoint, client_id, client_secret, with_authorization: false)
|
12
|
+
end
|
13
|
+
|
14
|
+
def generate_token
|
15
|
+
payload = {
|
16
|
+
client_id: client_id,
|
17
|
+
client_secret: client_secret,
|
18
|
+
audience: "https://api.intimebatnr.com",
|
19
|
+
grant_type: "client_credentials"
|
20
|
+
}
|
21
|
+
|
22
|
+
client.action('/oauth/token', payload: payload)
|
23
|
+
end
|
24
|
+
|
25
|
+
def token_expired?
|
26
|
+
return true unless expire_datetime
|
27
|
+
|
28
|
+
Time.now > expire_datetime
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_access_token
|
32
|
+
Thread.current["access_token_#{client_id}"]
|
33
|
+
end
|
34
|
+
|
35
|
+
def save_to_thread!(response)
|
36
|
+
save_to_thread(response)
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def save_to_thread(response)
|
42
|
+
[:access_token, :expires_in, :token_type, :scope].each do |key|
|
43
|
+
Thread.current["#{key}_#{client_id}"] = response.hash_response.dig(key.to_s)
|
44
|
+
end
|
45
|
+
|
46
|
+
Thread.current["expire_datetime_#{client_id}"] = Time.now + response.hash_response.dig('expires_in')
|
47
|
+
end
|
48
|
+
|
49
|
+
def expire_datetime
|
50
|
+
Thread.current["expire_datetime_#{client_id}"]
|
51
|
+
end
|
52
|
+
|
53
|
+
def token_generation_endpoint
|
54
|
+
InTimeRuby.config.token_endpoint
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'json'
|
3
|
+
require "base64"
|
4
|
+
|
5
|
+
module InTimeRuby
|
6
|
+
class Client
|
7
|
+
|
8
|
+
attr_reader :base_url, :client_id, :client_secret, :with_authorization, :with_logger
|
9
|
+
|
10
|
+
def initialize(base_url, client_id, client_secret, with_authorization: true, with_logger: false)
|
11
|
+
@base_url = base_url
|
12
|
+
@client_id = client_id
|
13
|
+
@client_secret = client_secret
|
14
|
+
@with_authorization = with_authorization
|
15
|
+
@with_logger = with_logger
|
16
|
+
end
|
17
|
+
|
18
|
+
def action(path, http_method: :post, payload: {}, query_params: {})
|
19
|
+
begin
|
20
|
+
response = ::RestClient::Request.execute(
|
21
|
+
method: http_method,
|
22
|
+
url: full_url(path),
|
23
|
+
payload: payload.to_json,
|
24
|
+
headers: headers,
|
25
|
+
timeout: 5,
|
26
|
+
verify_ssl: ::OpenSSL::SSL::VERIFY_NONE
|
27
|
+
)
|
28
|
+
rescue => e
|
29
|
+
return struct_response.new(false, { error: parse(e.response), message: e.message })
|
30
|
+
end
|
31
|
+
|
32
|
+
parse(response)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def get_auth_token
|
38
|
+
unless access_token_generator.token_expired?
|
39
|
+
print_log "Using existing token"
|
40
|
+
return access_token_generator.get_access_token
|
41
|
+
end
|
42
|
+
|
43
|
+
print_log "Generating new token"
|
44
|
+
|
45
|
+
response = access_token_generator.generate_token
|
46
|
+
access_token_generator.save_to_thread!(response)
|
47
|
+
access_token_generator.get_access_token
|
48
|
+
end
|
49
|
+
|
50
|
+
def headers
|
51
|
+
headers = {
|
52
|
+
'User-Agent': "Ruby In Time client v#{InTimeRuby::VERSION})",
|
53
|
+
'Content-Type': 'application/json',
|
54
|
+
'Accept': 'application/json'
|
55
|
+
}
|
56
|
+
|
57
|
+
headers['Authorization'] = "Bearer #{get_auth_token}" if with_authorization
|
58
|
+
headers
|
59
|
+
end
|
60
|
+
|
61
|
+
def full_url(path)
|
62
|
+
"#{base_url}#{path}"
|
63
|
+
end
|
64
|
+
|
65
|
+
def parse(response)
|
66
|
+
success = (200..308).to_a.include?(response.code.to_i) ? true : false
|
67
|
+
hash_response = response.body.empty? ? response.body : JSON.parse(response.body)
|
68
|
+
|
69
|
+
struct_response.new(success, hash_response)
|
70
|
+
end
|
71
|
+
|
72
|
+
def struct_response
|
73
|
+
Struct.new(:success?, :hash_response)
|
74
|
+
end
|
75
|
+
|
76
|
+
def access_token_generator
|
77
|
+
@access_token_generator ||= ::InTimeRuby::AccessTokenGenerator.new(client_id, client_secret)
|
78
|
+
end
|
79
|
+
|
80
|
+
def print_log(log, debug_type: :info)
|
81
|
+
if with_logger
|
82
|
+
InTimeRuby.config.logger.send debug_type, "<---- #{log} ----->"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module InTimeRuby
|
2
|
+
class << self
|
3
|
+
# Accessor for global configuration.
|
4
|
+
attr_accessor :config
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.configure
|
8
|
+
self.config ||= Configuration.new
|
9
|
+
yield(config) if block_given?
|
10
|
+
end
|
11
|
+
|
12
|
+
class Configuration
|
13
|
+
attr_accessor :token_endpoint, :logger
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@token_endpoint = 'https://intime-ba.eu.auth0.com'
|
17
|
+
@logger = InTimeRuby::Logger
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module InTimeRuby
|
2
|
+
class Connection
|
3
|
+
|
4
|
+
attr_reader :client, :base_url, :client_id, :client_secret
|
5
|
+
|
6
|
+
def initialize(base_url, client_id, client_secret, with_logger: false)
|
7
|
+
@base_url = base_url
|
8
|
+
@client_id = client_id
|
9
|
+
@client_secret = client_secret
|
10
|
+
|
11
|
+
@client = InTimeRuby::Client.new(base_url, client_id, client_secret, with_logger: with_logger)
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_shipment(id)
|
15
|
+
action("/api/shipments/#{id}", http_method: :get)
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_shipment(payload)
|
19
|
+
action("/api/shipments", payload: payload)
|
20
|
+
end
|
21
|
+
|
22
|
+
def confirm_shipment(trackingNumber)
|
23
|
+
action("/api/shipments/confirmshipments?trackingNumber=#{trackingNumber}", http_method: :put)
|
24
|
+
end
|
25
|
+
|
26
|
+
def update_shipment(id, payload)
|
27
|
+
action("/api/shipments/#{id}", payload: payload, http_method: :patch)
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_shipment_events(id)
|
31
|
+
action("/api/shipments/#{id}/shipmentevents", http_method: :get)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def action(path, payload: {}, http_method: :post)
|
37
|
+
client.action(path, payload: payload, http_method: http_method)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/in-time-ruby.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'in-time-ruby/version'
|
2
|
+
require 'in-time-ruby/configuration'
|
3
|
+
require 'in-time-ruby/client'
|
4
|
+
require 'in-time-ruby/connection'
|
5
|
+
require 'in-time-ruby/logger'
|
6
|
+
require 'in-time-ruby/access_token_generator'
|
7
|
+
|
8
|
+
module InTimeRuby;end
|
9
|
+
|
10
|
+
InTimeRuby.configure
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: in-time-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adem Dinarevic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-08-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: nokogiri
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.12.4
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.12.4
|
55
|
+
description: Ruby client for InTime API
|
56
|
+
email:
|
57
|
+
- ademdinarevic@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- lib/in-time-ruby.rb
|
63
|
+
- lib/in-time-ruby/access_token_generator.rb
|
64
|
+
- lib/in-time-ruby/client.rb
|
65
|
+
- lib/in-time-ruby/configuration.rb
|
66
|
+
- lib/in-time-ruby/connection.rb
|
67
|
+
- lib/in-time-ruby/logger.rb
|
68
|
+
- lib/in-time-ruby/version.rb
|
69
|
+
homepage: https://github.com/ademdc/in-time-ruby
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '2.3'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubygems_version: 3.1.2
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: Ruby client for In Time API
|
92
|
+
test_files: []
|