sticapi_client 4.0.1 → 4.0.3

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: 7f3371b95279762a94030c62e10800d62055fb95674b44303250e87eb311c858
4
+ data.tar.gz: ea17919f9b725ccc5e09beb593edb3968ea5348860e7830736fef391f22a97f6
5
5
  SHA512:
6
- metadata.gz: '04593a147f4e05765d4d726c988aeb0cbedd37b2804f3482a8249e99254bf7f601cd21e6626372afce3ab1c03e8d4f77e5c1844b52128c6b4fbe740c3a724dfa'
7
- data.tar.gz: 0ca9e42d7dc19c74a88a87a73bc31e391d057ddf2a4b33240ab317c7883733cbd3462ed07b019a9a6b3bec4684dcd408ed632c41198c54f0573d4593d3ea7ace
6
+ metadata.gz: a554af2400a3baaf1c51e76906ad525013cd848903190656588ff6de4a865c735db8672efd52f451880094693c5e88072fd0e28d2fd9cd0560418368014cb7cd
7
+ data.tar.gz: ca3b1c42e5c7ac016fb228185fc0486733739d75853b948de828b7601ab8b08cc0d2848063d56ba36693af8766410a90dc0f2a1a20ae3c970d121278ac7396e4
@@ -1,3 +1,3 @@
1
1
  module SticapiClient
2
- VERSION = "4.0.1"
2
+ VERSION = "4.0.3"
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"]
@@ -126,15 +142,28 @@ module Sticapi
126
142
  end
127
143
  end
128
144
 
145
+ def flatten_params(params, prefix = nil)
146
+ case params
147
+ when Hash
148
+ params.flat_map { |key, value| flatten_params(value, prefix ? "#{prefix}[#{key}]" : key.to_s) }
149
+ when Array
150
+ params.flat_map { |value| flatten_params(value, "#{prefix}[]") }
151
+ else
152
+ [[prefix, params]]
153
+ end
154
+ end
155
+
129
156
  def http_request(route, options = {})
130
157
  kind = options[:kind] || "post"
131
158
  uri = URI.parse("#{self.uri}#{route}")
132
159
  http = Net::HTTP.new(uri.host, uri.port)
133
160
  http.use_ssl = @ssl
161
+ http.open_timeout = OPEN_TIMEOUT
162
+ http.read_timeout = READ_TIMEOUT
134
163
 
135
164
  request = if kind == "get"
136
- params = options.except(:kind)
137
- uri.query = URI.encode_www_form(params) if params.any?
165
+ flat_params = flatten_params(options.except(:kind))
166
+ uri.query = URI.encode_www_form(flat_params) if flat_params.any?
138
167
  Net::HTTP::Get.new(uri.request_uri)
139
168
  else
140
169
  req = Net::HTTP::Post.new(uri.request_uri)
@@ -152,7 +181,11 @@ module Sticapi
152
181
  end
153
182
 
154
183
  def token_cache_path
155
- "#{Rails.root}/#{TOKEN_CACHE_PATH}"
184
+ "#{app_root}/tmp/sticapi_token_#{host_namespace}.yml"
185
+ end
186
+
187
+ def host_namespace
188
+ (@host || "default").gsub(/[^a-zA-Z0-9]/, "_")
156
189
  end
157
190
 
158
191
  def load_token
@@ -181,12 +214,37 @@ module Sticapi
181
214
  expiry: @expiry
182
215
  }.to_yaml)
183
216
  end
217
+
218
+ def token_expired?
219
+ @access_token.blank? || (DateTime.now > Time.at(@expiry.to_i))
220
+ end
221
+
222
+ def parse_response(response)
223
+ JSON.parse(response.body)
224
+ rescue JSON::ParserError => e
225
+ log_error("Invalid JSON (status #{response.code}): #{e.message}")
226
+ raise JSON::ParserError, "Sticapi #{response.code}: #{e.message}"
227
+ end
228
+
229
+ def log_error(msg)
230
+ if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
231
+ Rails.logger.error("[SticapiClient] #{msg}")
232
+ else
233
+ $stderr.puts("[SticapiClient] #{msg}")
234
+ end
235
+ end
236
+
237
+ def app_root
238
+ defined?(Rails) ? Rails.root : Dir.pwd
239
+ end
240
+
241
+ def app_env
242
+ defined?(Rails) ? Rails.env : ENV.fetch("RAILS_ENV", "development")
243
+ end
184
244
  end
185
245
  end
186
246
 
187
- # Add sticapi_authenticatable strategy to defaults.
188
- #
189
247
  Devise.add_module(:sticapi_authenticatable,
190
- route: :session, ## This will add the routes, rather than in the routes.rb
248
+ route: :session,
191
249
  strategy: true,
192
250
  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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Viana