vncpost_api 0.2.1 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f818eb7aa98bf14e740e787f2b4a3fa01a02d3fb9d21db37a8335f0b41403fa
4
- data.tar.gz: 97817de460715644959343f4311a5d7878bde714cad6c172343bec96122124d4
3
+ metadata.gz: c6f6195dc499ad77fff96cd46a7211bad7280a4a51e7d3fb2e9416600e35e8d2
4
+ data.tar.gz: b5bac1ce7349b3407e2a9852fd76a8be0b154d012a5dae545bd6d4df14793127
5
5
  SHA512:
6
- metadata.gz: 79b9f520a1154bcc81119a1aa8a7bf10d5fcab9985a2704c9507365316e6ba7b73d9e765c198f6d29a5370a6ef206335761cc2e556ecc0a793d3d8514a0d5a29
7
- data.tar.gz: 4cfa77b46bbbaf778f7e133d86aa26ab6548022450809dd7c20b2c34a97687ff8898e95c5645ab1aeaa40f57386af991768c731dd1f9d84a1fb4fbe7025a0c31
6
+ metadata.gz: 6678ab416062f19455447c0477a24145eea93149156c1707f7327e278778daeb0389fcaa40cb03dd850e0068c44dd1bbfdc73b5e5727cc518e71fda0e8b85c35
7
+ data.tar.gz: 633264c40faad909a8bd4b7a9e7726f00bd52de75ee1dfbf28e09479ada77497030aa0bd10e5b169ae61c620c5726ef61baa19bc85c9284e91e2e358d130647b
data/Gemfile.lock CHANGED
@@ -1,14 +1,15 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- vncpost_api (0.2.0)
4
+ vncpost_api (0.2.1)
5
5
  activeresource
6
+ jwt
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
9
10
  specs:
10
- activemodel (6.0.3)
11
- activesupport (= 6.0.3)
11
+ activemodel (6.1.4.1)
12
+ activesupport (= 6.1.4.1)
12
13
  activemodel-serializers-xml (1.0.2)
13
14
  activemodel (> 5.x)
14
15
  activesupport (> 5.x)
@@ -17,22 +18,23 @@ GEM
17
18
  activemodel (>= 5.0, < 7)
18
19
  activemodel-serializers-xml (~> 1.0)
19
20
  activesupport (>= 5.0, < 7)
20
- activesupport (6.0.3)
21
+ activesupport (6.1.4.1)
21
22
  concurrent-ruby (~> 1.0, >= 1.0.2)
22
- i18n (>= 0.7, < 2)
23
- minitest (~> 5.1)
24
- tzinfo (~> 1.1)
25
- zeitwerk (~> 2.2, >= 2.2.2)
23
+ i18n (>= 1.6, < 2)
24
+ minitest (>= 5.1)
25
+ tzinfo (~> 2.0)
26
+ zeitwerk (~> 2.3)
26
27
  builder (3.2.4)
27
28
  byebug (11.1.1)
28
29
  coderay (1.1.2)
29
- concurrent-ruby (1.1.6)
30
+ concurrent-ruby (1.1.9)
30
31
  diff-lcs (1.3)
31
32
  dotenv (2.7.5)
32
- i18n (1.8.2)
33
+ i18n (1.8.10)
33
34
  concurrent-ruby (~> 1.0)
35
+ jwt (2.2.3)
34
36
  method_source (1.0.0)
35
- minitest (5.14.1)
37
+ minitest (5.14.4)
36
38
  pry (0.13.1)
37
39
  coderay (~> 1.1)
38
40
  method_source (~> 1.0)
@@ -53,10 +55,9 @@ GEM
53
55
  diff-lcs (>= 1.2.0, < 2.0)
54
56
  rspec-support (~> 3.9.0)
55
57
  rspec-support (3.9.2)
56
- thread_safe (0.3.6)
57
- tzinfo (1.2.7)
58
- thread_safe (~> 0.1)
59
- zeitwerk (2.3.0)
58
+ tzinfo (2.0.4)
59
+ concurrent-ruby (~> 1.0)
60
+ zeitwerk (2.4.2)
60
61
 
61
62
  PLATFORMS
62
63
  ruby
@@ -70,4 +71,4 @@ DEPENDENCIES
70
71
  vncpost_api!
71
72
 
72
73
  BUNDLED WITH
73
- 2.1.4
74
+ 2.2.26
data/lib/dev/config.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  def set_config
2
2
  VNCPostAPI.configure do |config|
3
- config.testing = true
3
+ config.testing = true
4
4
  config.username = ENV["VNCPOST_USERNAME"]
5
5
  config.password = ENV["VNCPOST_PASSWORD"]
6
6
  end
@@ -9,6 +9,7 @@ loader.push_dir("./lib")
9
9
  loader.collapse("./lib/vncpost_api/resources")
10
10
  loader.ignore("#{__dir__}/config.rb")
11
11
  loader.ignore("./lib/vncpost_api/exceptions.rb")
12
+ loader.ignore("./lib/vncpost_api/cache.rb")
12
13
  loader.enable_reloading
13
14
  # loader.log!
14
15
  loader.setup
@@ -0,0 +1,5 @@
1
+ module VNCPostAPI
2
+ mattr_accessor :cache
3
+
4
+ self.cache = ActiveSupport::Cache::MemoryStore.new
5
+ end
@@ -4,23 +4,33 @@ module VNCPostAPI
4
4
  alias_method :testing?, :testing
5
5
  end
6
6
 
7
- def self.config
8
- @config ||= Configuration.new
9
- end
7
+ class << self
8
+ def config
9
+ @config ||= Configuration.new
10
+ end
10
11
 
11
- def self.config=(config)
12
- @config = config
13
- end
12
+ def configure
13
+ yield config
14
+
15
+ after_configure
16
+ end
17
+
18
+ private
19
+
20
+ def after_configure
21
+ if config.testing?
22
+ VNCPostAPI::Base.site = "http://api.v3.vncpost.com"
23
+ VNCPostAPI::UserLogin.site = "http://api.v3.vncpost.com"
24
+ else
25
+ VNCPostAPI::Base.site = "https://u.api.vncpost.com"
26
+ VNCPostAPI::UserLogin.site = "https://u.api.vncpost.com"
27
+ end
28
+ # Tracking is only available on production
29
+ VNCPostAPI::Tracking.site = "http://pt.vncpost.com"
30
+
31
+ return unless defined?(Rails) && Rails.respond_to?(:cache) && Rails.cache.is_a?(ActiveSupport::Cache::Store)
14
32
 
15
- def self.configure
16
- yield config
17
- # set the site once user configure
18
- if config.testing?
19
- VNCPostAPI::Base.site = "http://api.v3.vncpost.com"
20
- VNCPostAPI::Tracking.site ="https://pt.v.vncpost.com"
21
- else
22
- VNCPostAPI::Base.site = "http://u.api.vncpost.com"
23
- VNCPostAPI::Tracking.site = "https://pt.vncpost.com"
33
+ VNCPostAPI.cache = Rails.cache
24
34
  end
25
35
  end
26
36
  end
@@ -4,13 +4,14 @@ module VNCPostAPI
4
4
  body = JSON.parse(response.body)
5
5
  if body["Message"]
6
6
  message = "Failed.".dup
7
- message << " Error message: #{body["Message"]}"
7
+ message << " Message: #{body["Message"]}"
8
8
  message
9
9
  else
10
10
  super
11
11
  end
12
12
  end
13
13
  end
14
+
14
15
  class Connection < ActiveResource::Connection
15
16
  private
16
17
 
@@ -3,9 +3,10 @@ module VNCPostAPI
3
3
  self.include_root_in_json = false
4
4
  self.include_format_in_path = false
5
5
  self.connection_class = VNCPostAPI::Connection
6
+ self.auth_type = :bearer
6
7
 
7
8
  def create
8
- self.class.retrieve_token
9
+ connection.bearer_token = UserLogin.bearer_token
9
10
  format_before_send_request
10
11
  super
11
12
  rescue => e
@@ -23,26 +24,6 @@ module VNCPostAPI
23
24
 
24
25
  private
25
26
 
26
- def self.retrieve_token
27
- clear_auth_token
28
-
29
- if VNCPostAPI.config&.username && VNCPostAPI.config&.password
30
- response = connection.post("#{Base.site}/User/Login", {
31
- Username: VNCPostAPI.config.username,
32
- Password: VNCPostAPI.config.password
33
- }.to_json)
34
- connection.auth_type = :bearer
35
- connection.bearer_token = JSON.parse(response.body)["token"]
36
- else
37
- raise ArgumentError, "Please set the username and password in the config file under the initializer dir"
38
- end
39
- end
40
-
41
- def self.clear_auth_token
42
- connection.auth_type = nil
43
- connection.bearer_token = nil
44
- end
45
-
46
27
  def format_before_send_request
47
28
  @attributes.deep_transform_keys!(&:camelcase)
48
29
  end
@@ -55,6 +36,5 @@ module VNCPostAPI
55
36
  super
56
37
  reset_attributes_format
57
38
  end
58
-
59
39
  end
60
40
  end
@@ -1,17 +1,17 @@
1
1
  module VNCPostAPI
2
+ # Tracking is only available on production
2
3
  class Tracking < Base
3
4
  self.prefix = "/Track/Order"
4
5
  self.element_name = ""
6
+ self.connection_class = VNCPostAPI::Connection
5
7
 
6
8
  def self.find(code)
7
9
  body = {
8
10
  Code: code
9
11
  }
10
-
11
- self.retrieve_token
12
-
12
+ connection.bearer_token = UserLogin.bearer_token(username: "V9Cus327141793", password: "8DBNvF9Y56nnjNTU")
13
13
  response = connection.post(collection_path, body.to_json)
14
14
  new(self.format.decode(response.body).deep_transform_keys!(&:underscore))
15
15
  end
16
16
  end
17
- end
17
+ end
@@ -0,0 +1,33 @@
1
+ require "jwt"
2
+
3
+ module VNCPostAPI
4
+ class UserLogin < ::ActiveResource::Base
5
+ self.include_root_in_json = false
6
+ self.include_format_in_path = false
7
+ self.prefix = "/User/Login"
8
+ self.element_name = ""
9
+
10
+ class << self
11
+ def bearer_token(username: VNCPostAPI.config.username, password: VNCPostAPI.config.password)
12
+ cache_key = "VNCPostAPI/bearer_token"
13
+ cached_token = VNCPostAPI.cache.read(cache_key)
14
+ return cached_token if cached_token
15
+
16
+ token = create(Username: username, Password: password).token
17
+ VNCPostAPI.cache.write(
18
+ cache_key,
19
+ token,
20
+ expires_in: expires_in(token) - 15 # clear cache earlier
21
+ )
22
+
23
+ token
24
+ end
25
+
26
+ private
27
+
28
+ def expires_in(jwt)
29
+ Time.at(JWT.decode(jwt, nil, false)[0]["exp"])
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module VNCPostAPI
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/vncpost_api.rb CHANGED
@@ -2,11 +2,11 @@ require "vncpost_api/version"
2
2
  require "active_resource"
3
3
 
4
4
  module VNCPostAPI
5
+ require "vncpost_api/cache"
5
6
  require "vncpost_api/configuration"
6
-
7
7
  require "vncpost_api/connection"
8
-
9
8
  require "vncpost_api/resources/base"
10
- require "vncpost_api/resources/tracking"
11
9
  require "vncpost_api/resources/order"
10
+ require "vncpost_api/resources/tracking"
11
+ require "vncpost_api/resources/user_login"
12
12
  end
data/vncpost_api.gemspec CHANGED
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.require_paths = ["lib"]
28
28
 
29
29
  spec.add_dependency "activeresource"
30
+ spec.add_dependency "jwt"
30
31
 
31
32
  spec.add_development_dependency "rspec", "~> 3.2"
32
33
  spec.add_development_dependency "dotenv"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vncpost_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Chong
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-15 00:00:00.000000000 Z
11
+ date: 2021-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeresource
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jwt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -80,7 +94,7 @@ dependencies:
80
94
  - - ">="
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0'
83
- description:
97
+ description:
84
98
  email:
85
99
  - andygg1996personal@gmail.com
86
100
  executables: []
@@ -103,11 +117,13 @@ files:
103
117
  - lib/dev/config.rb
104
118
  - lib/dev/zeitwerk_loader.rb
105
119
  - lib/vncpost_api.rb
120
+ - lib/vncpost_api/cache.rb
106
121
  - lib/vncpost_api/configuration.rb
107
122
  - lib/vncpost_api/connection.rb
108
123
  - lib/vncpost_api/resources/base.rb
109
124
  - lib/vncpost_api/resources/order.rb
110
125
  - lib/vncpost_api/resources/tracking.rb
126
+ - lib/vncpost_api/resources/user_login.rb
111
127
  - lib/vncpost_api/version.rb
112
128
  - vncpost_api.gemspec
113
129
  homepage: https://github.com/PostCo/vncpost_api
@@ -118,7 +134,7 @@ metadata:
118
134
  homepage_uri: https://github.com/PostCo/vncpost_api
119
135
  source_code_uri: https://github.com/PostCo/vncpost_api
120
136
  changelog_uri: https://github.com/PostCo/vncpost_api/releases
121
- post_install_message:
137
+ post_install_message:
122
138
  rdoc_options: []
123
139
  require_paths:
124
140
  - lib
@@ -133,8 +149,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
149
  - !ruby/object:Gem::Version
134
150
  version: '0'
135
151
  requirements: []
136
- rubygems_version: 3.0.3
137
- signing_key:
152
+ rubygems_version: 3.2.22
153
+ signing_key:
138
154
  specification_version: 4
139
155
  summary: Ruby object based VNCPost API wrapper.
140
156
  test_files: []