sticapi_client 4.0.1 → 4.0.2

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: 96923b65d26e328930491c4a94a14d34e70a56322cbfd192c5f3120d2e8d5691
4
- data.tar.gz: 8963340be8db1c64c43b9ca8fee6b06711e6a6648c3d37cf429778cc38cd3183
3
+ metadata.gz: 842accc086921b1cb14fcc607e37a70050104724d1ea984a200f47e9c315a1a9
4
+ data.tar.gz: cc36ccf42b4bb49b5c654a3bb1f29797d8af06ff0deb2d0814ac86869e6f14c2
5
5
  SHA512:
6
- metadata.gz: '04593a147f4e05765d4d726c988aeb0cbedd37b2804f3482a8249e99254bf7f601cd21e6626372afce3ab1c03e8d4f77e5c1844b52128c6b4fbe740c3a724dfa'
7
- data.tar.gz: 0ca9e42d7dc19c74a88a87a73bc31e391d057ddf2a4b33240ab317c7883733cbd3462ed07b019a9a6b3bec4684dcd408ed632c41198c54f0573d4593d3ea7ace
6
+ metadata.gz: 915f1686cee6715e68b18f2e1a9ba02f5119419ebab8d5b177088e6dfcfe5d14dff5d045df4f98f682cf927d2221c661fbd9ea079a754ba3f4aa7ed10fc0936e
7
+ data.tar.gz: 7127b8f5a3435f98aaff66cc50015ff75efdf2fccb3e0efaaa80974ddc9cd0ab9959cdbedf546e5fbb28182a1b850d873a57dab37c7ecf0575829780a41f0305
@@ -1,3 +1,3 @@
1
1
  module SticapiClient
2
- VERSION = "4.0.1"
2
+ VERSION = "4.0.2"
3
3
  end
@@ -21,15 +21,19 @@ require "net/http"
21
21
  require "erb"
22
22
  require "yaml"
23
23
  require "fileutils"
24
+ require "monitor"
24
25
 
25
26
  module Sticapi
26
27
  class SticapiClient
27
28
  include Singleton
28
29
 
30
+ MUTEX = Monitor.new
31
+
29
32
  attr_accessor :host, :urn, :port, :ssl, :user, :password
30
33
  attr_accessor :access_token, :client, :uid, :expiry
31
34
 
32
- TOKEN_CACHE_PATH = "tmp/sticapi_token.yml"
35
+ OPEN_TIMEOUT = 10
36
+ READ_TIMEOUT = 30
33
37
 
34
38
  def initialize
35
39
  load_config
@@ -41,65 +45,77 @@ module Sticapi
41
45
  end
42
46
 
43
47
  def get_token
44
- return unless @access_token.blank? || (DateTime.now > Time.at(@expiry.to_i))
48
+ return unless token_expired?
45
49
 
46
- if @access_token.present?
47
- sign_out
48
- end
50
+ MUTEX.synchronize do
51
+ return unless token_expired?
49
52
 
50
- uri = URI.parse("#{self.uri}/auth/sign_in")
51
- http = Net::HTTP.new(uri.host, uri.port)
52
- http.use_ssl = @ssl
53
- request = Net::HTTP::Post.new(uri.request_uri)
54
- request["Content-Type"] = "application/json"
55
- request.body = { email: @user, password: @password }.to_json
56
- response = http.request(request)
57
- @access_token = response["access-token"] if response["access-token"]
58
- @client = response["client"] if response["client"]
59
- @uid = response["uid"] if response["uid"]
60
- @expiry = response["expiry"] if response["expiry"]
61
- save_token
53
+ sign_out if @access_token.present?
54
+
55
+ uri = URI.parse("#{self.uri}/auth/sign_in")
56
+ http = Net::HTTP.new(uri.host, uri.port)
57
+ http.use_ssl = @ssl
58
+ http.open_timeout = OPEN_TIMEOUT
59
+ http.read_timeout = READ_TIMEOUT
60
+ request = Net::HTTP::Post.new(uri.request_uri)
61
+ request["Content-Type"] = "application/json"
62
+ request.body = { email: @user, password: @password }.to_json
63
+ response = http.request(request)
64
+ @access_token = response["access-token"] if response["access-token"]
65
+ @client = response["client"] if response["client"]
66
+ @uid = response["uid"] if response["uid"]
67
+ @expiry = response["expiry"] if response["expiry"]
68
+ save_token
69
+ end
62
70
  end
63
71
 
64
72
  def expiry_now
65
- @expiry = 0
73
+ MUTEX.synchronize do
74
+ @expiry = 0
75
+ end
66
76
  end
67
77
 
68
78
  def sticapi_request(route, options = {})
69
- get_token
70
- response = http_request(route, options)
71
-
72
- if response.code.to_i == 401
73
- expiry_now
79
+ MUTEX.synchronize do
74
80
  get_token
75
81
  response = http_request(route, options)
76
- end
77
82
 
78
- @access_token = response["access-token"] if response["access-token"]
79
- @client = response["client"] if response["client"]
80
- @uid = response["uid"] if response["uid"]
81
- @expiry = response["expiry"] if response["expiry"]
82
- JSON.parse(response.body)
83
+ if response.code.to_i == 401
84
+ expiry_now
85
+ get_token
86
+ response = http_request(route, options)
87
+ end
88
+
89
+ @access_token = response["access-token"] if response["access-token"]
90
+ @client = response["client"] if response["client"]
91
+ @uid = response["uid"] if response["uid"]
92
+ @expiry = response["expiry"] if response["expiry"]
93
+ parse_response(response)
94
+ end
83
95
  end
84
96
 
85
97
  def sign_out
86
- if @access_token.present?
87
- uri = URI.parse("#{self.uri}/auth/sign_out")
88
- http = Net::HTTP.new(uri.host, uri.port)
89
- http.use_ssl = @ssl
90
- request = Net::HTTP::Delete.new(uri.request_uri)
91
- request["Content-Type"] = "application/json"
92
- request["access-token"] = @access_token
93
- request["client"] = @client
94
- request["uid"] = @uid
95
- request["expiry"] = @expiry
96
- http.request(request)
98
+ MUTEX.synchronize do
99
+ if @access_token.present?
100
+ uri = URI.parse("#{self.uri}/auth/sign_out")
101
+ http = Net::HTTP.new(uri.host, uri.port)
102
+ http.use_ssl = @ssl
103
+ http.open_timeout = OPEN_TIMEOUT
104
+ http.read_timeout = READ_TIMEOUT
105
+ request = Net::HTTP::Delete.new(uri.request_uri)
106
+ request["Content-Type"] = "application/json"
107
+ request["access-token"] = @access_token
108
+ request["client"] = @client
109
+ request["uid"] = @uid
110
+ request["expiry"] = @expiry
111
+ http.request(request)
112
+ end
113
+ @access_token = ""
114
+ @client = ""
115
+ @uid = ""
116
+ @expiry = ""
117
+ File.delete(token_cache_path) if File.exist?(token_cache_path)
97
118
  end
98
- @access_token = ""
99
- @client = ""
100
- @uid = ""
101
- @expiry = ""
102
- File.delete(token_cache_path) if File.exist?(token_cache_path)
103
119
  end
104
120
 
105
121
  private
@@ -114,9 +130,9 @@ module Sticapi
114
130
  @ssl = ENV.fetch("STICAPI_SSL", "false") == "true"
115
131
  else
116
132
  configs = YAML.safe_load(
117
- ERB.new(File.read("#{Rails.root}/config/sticapi.yml")).result,
133
+ ERB.new(File.read("#{app_root}/config/sticapi.yml")).result,
118
134
  aliases: true
119
- )[Rails.env]
135
+ )[app_env]
120
136
  @host = configs["host"]
121
137
  @port = configs["port"] || 80
122
138
  @user = configs["user"]
@@ -131,6 +147,8 @@ module Sticapi
131
147
  uri = URI.parse("#{self.uri}#{route}")
132
148
  http = Net::HTTP.new(uri.host, uri.port)
133
149
  http.use_ssl = @ssl
150
+ http.open_timeout = OPEN_TIMEOUT
151
+ http.read_timeout = READ_TIMEOUT
134
152
 
135
153
  request = if kind == "get"
136
154
  params = options.except(:kind)
@@ -152,7 +170,11 @@ module Sticapi
152
170
  end
153
171
 
154
172
  def token_cache_path
155
- "#{Rails.root}/#{TOKEN_CACHE_PATH}"
173
+ "#{app_root}/tmp/sticapi_token_#{host_namespace}.yml"
174
+ end
175
+
176
+ def host_namespace
177
+ (@host || "default").gsub(/[^a-zA-Z0-9]/, "_")
156
178
  end
157
179
 
158
180
  def load_token
@@ -181,12 +203,37 @@ module Sticapi
181
203
  expiry: @expiry
182
204
  }.to_yaml)
183
205
  end
206
+
207
+ def token_expired?
208
+ @access_token.blank? || (DateTime.now > Time.at(@expiry.to_i))
209
+ end
210
+
211
+ def parse_response(response)
212
+ JSON.parse(response.body)
213
+ rescue JSON::ParserError => e
214
+ log_error("Invalid JSON (status #{response.code}): #{e.message}")
215
+ raise JSON::ParserError, "Sticapi #{response.code}: #{e.message}"
216
+ end
217
+
218
+ def log_error(msg)
219
+ if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
220
+ Rails.logger.error("[SticapiClient] #{msg}")
221
+ else
222
+ $stderr.puts("[SticapiClient] #{msg}")
223
+ end
224
+ end
225
+
226
+ def app_root
227
+ defined?(Rails) ? Rails.root : Dir.pwd
228
+ end
229
+
230
+ def app_env
231
+ defined?(Rails) ? Rails.env : ENV.fetch("RAILS_ENV", "development")
232
+ end
184
233
  end
185
234
  end
186
235
 
187
- # Add sticapi_authenticatable strategy to defaults.
188
- #
189
236
  Devise.add_module(:sticapi_authenticatable,
190
- route: :session, ## This will add the routes, rather than in the routes.rb
237
+ route: :session,
191
238
  strategy: true,
192
239
  controller: :sessions)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sticapi_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 4.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Viana