rail-locator-api 0.1.15 → 0.1.17
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f669bd03427eda818b445e6639fa78c663defa18a11a491144dace854ab8906
|
4
|
+
data.tar.gz: a3eaabab8cff46cbb00334454833e2bc1b53c58e8f3668b3402cd98eea868e00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce8f47cbc300948b32217bcc745340f952cecde7790e0c4cbd4cf0bc78aa7fcc289bc29a0988588509fbbd7b25ad0f32ac46092e4cd0a00faffa43c25ffc2abc
|
7
|
+
data.tar.gz: 1c15323aee4561cd964048188cdc1bc402275d2f6e1cd81590a1499a165b1a97358f1ea51dbc1963181d3d059877a1391ef92206e55f8c6637f81f178fc48af3
|
@@ -13,6 +13,8 @@ RailLocatorApi.setup do |config|
|
|
13
13
|
config::Request.api_user_email ||= ENV['API_USER_EMAIL']
|
14
14
|
config::Request.api_user_password ||= ENV['API_USER_PASSWORD']
|
15
15
|
|
16
|
+
config::Request.jwt_secret_code ||= ENV['JWT_SECRET_CODE']
|
17
|
+
|
16
18
|
config::Request.timeout = 60
|
17
19
|
config::Request.open_timeout = 60
|
18
20
|
config::Request.symbolize_keys = true
|
@@ -33,6 +33,9 @@ module RailLocatorApi
|
|
33
33
|
protected
|
34
34
|
|
35
35
|
# Convenience accessors
|
36
|
+
def jwt_secret_code
|
37
|
+
@request_builder.jwt_secret_code
|
38
|
+
end
|
36
39
|
|
37
40
|
def access_token
|
38
41
|
@request_builder.access_token
|
@@ -121,6 +124,13 @@ module RailLocatorApi
|
|
121
124
|
if request
|
122
125
|
request.params.merge!(params) if params
|
123
126
|
request.headers['Content-Type'] = 'application/json'
|
127
|
+
unless RailLocatorApi::Request.token_alive?(RailLocatorApi::Request.access_token)
|
128
|
+
if RailLocatorApi::Request.token_alive?(RailLocatorApi::Request.refresh_token)
|
129
|
+
response = RailLocatorApi.refresh_access_token
|
130
|
+
RailLocatorApi::Request.access_token = response.try(:dig, "access_token")
|
131
|
+
RailLocatorApi::Request.refresh_token = response.try(:dig, "refresh_token")
|
132
|
+
end
|
133
|
+
end
|
124
134
|
if self.api_auth_method == :base64
|
125
135
|
request.headers['Authorization'] = "Basic " + Base64::encode64("#{self.api_user_email}:#{self.api_user_password}")
|
126
136
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module RailLocatorApi
|
2
2
|
class Request
|
3
|
-
attr_accessor :access_token, :refresh_token, :api_key, :api_user_email, :api_user_password, :api_auth_method, :api_endpoint,
|
3
|
+
attr_accessor :jwt_secret_code, :access_token, :refresh_token, :api_key, :api_user_email, :api_user_password, :api_auth_method, :api_endpoint,
|
4
4
|
:timeout, :open_timeout, :proxy, :ssl_options, :faraday_adapter, :symbolize_keys, :debug,
|
5
5
|
:without_ratelimit, :logger, :test
|
6
6
|
|
@@ -9,7 +9,7 @@ module RailLocatorApi
|
|
9
9
|
DEFAULT_TIMEOUT = 60
|
10
10
|
DEFAULT_OPEN_TIMEOUT = 60
|
11
11
|
|
12
|
-
def initialize(access_token: nil, refresh_token: nil, api_key: nil, api_user_email: nil, api_user_password: nil,
|
12
|
+
def initialize(jwt_secret_code: nil, access_token: nil, refresh_token: nil, api_key: nil, api_user_email: nil, api_user_password: nil,
|
13
13
|
api_endpoint: nil, api_auth_method: nil, timeout: nil, open_timeout: nil, proxy: nil, ssl_options: nil,
|
14
14
|
faraday_adapter: nil, symbolize_keys: false, debug: false, without_ratelimit: false,
|
15
15
|
logger: nil, test: false)
|
@@ -22,14 +22,9 @@ module RailLocatorApi
|
|
22
22
|
@access_token = @access_token.strip if @access_token
|
23
23
|
@refresh_token = refresh_token || self.class.refresh_token
|
24
24
|
@refresh_token = @refresh_token.strip if @refresh_token
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
response = RailLocatorApi.refresh_access_token
|
29
|
-
@access_token = response.try(:dig, "access_token")
|
30
|
-
@refresh_token = response.try(:dig, "refresh_token")
|
31
|
-
end
|
32
|
-
end
|
25
|
+
|
26
|
+
@jwt_secret_code = jwt_secret_code || self.class.jwt_secret_code
|
27
|
+
@jwt_secret_code = @jwt_secret_code.strip if @jwt_secret_code
|
33
28
|
|
34
29
|
@api_user_email = api_user_email || ENV['API_USER_EMAIL'] || ""
|
35
30
|
@api_user_password = api_user_password || ENV['API_USER_PASSWORD'] || ""
|
@@ -95,33 +90,10 @@ module RailLocatorApi
|
|
95
90
|
reset
|
96
91
|
end
|
97
92
|
|
98
|
-
|
99
|
-
|
100
|
-
def need_login?
|
101
|
-
RailLocatorApi::Request.api_key = state
|
102
|
-
RailLocatorApi::Request.access_token = token
|
103
|
-
if token_alive?(token)
|
104
|
-
return false
|
105
|
-
else
|
106
|
-
if token_alive?(refresh_token)
|
107
|
-
RailLocatorApi::Request.refresh_token = refresh_token
|
108
|
-
response = RailLocatorApi.refresh_access_token
|
109
|
-
self.token = response.try(:dig, "access_token")
|
110
|
-
self.refresh_token = response.try(:dig, "refresh_token")
|
111
|
-
RailLocatorApi::Request.api_key = state
|
112
|
-
RailLocatorApi::Request.access_token = token
|
113
|
-
return !save
|
114
|
-
else
|
115
|
-
true
|
116
|
-
end
|
117
|
-
end
|
118
|
-
true
|
119
|
-
end
|
120
|
-
|
121
|
-
def token_alive?(token)
|
93
|
+
def token_alive?(token, jwt_secret_code=@jwt_secret_code)
|
122
94
|
begin
|
123
95
|
return false if token.nil?
|
124
|
-
exp = JWT.decode(token,
|
96
|
+
exp = JWT.decode(token, jwt_secret_code, false).try(:first).try(:dig, "exp")
|
125
97
|
return false if exp.nil?
|
126
98
|
Time.at(exp) > Time.now + 30.second
|
127
99
|
rescue => e
|
@@ -130,17 +102,20 @@ module RailLocatorApi
|
|
130
102
|
end
|
131
103
|
end
|
132
104
|
|
105
|
+
protected
|
106
|
+
|
133
107
|
def reset
|
134
108
|
@path_parts = []
|
135
109
|
end
|
136
110
|
|
137
111
|
class << self
|
138
|
-
attr_accessor :access_token, :refresh_token, :api_key, :api_user_email, :api_user_password, :api_auth_method,
|
112
|
+
attr_accessor :jwt_secret_code, :access_token, :refresh_token, :api_key, :api_user_email, :api_user_password, :api_auth_method,
|
139
113
|
:timeout, :open_timeout, :api_endpoint, :proxy, :ssl_options, :faraday_adapter, :symbolize_keys,
|
140
114
|
:debug, :without_ratelimit, :logger, :test
|
141
115
|
|
142
116
|
def method_missing(sym, *args, &block)
|
143
|
-
new(
|
117
|
+
new(jwt_secret_code: self.jwt_secret_code, access_token: self.access_token, refresh_token: self.refresh_token,
|
118
|
+
api_key: self.api_key,
|
144
119
|
api_user_email: self.api_user_email, api_user_password: self.api_user_email,
|
145
120
|
api_auth_method: self.api_auth_method, api_endpoint: self.api_endpoint,
|
146
121
|
timeout: self.timeout, open_timeout: self.open_timeout, faraday_adapter: self.faraday_adapter,
|