Class: RailLocatorApi::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/rail-locator-api/request.rb

Constant Summary collapse

AUTH_METHODS =
[:keycloak, :api_key, :base64]
DEFAULT_AUTH_METHOD =
:api_key
DEFAULT_TIMEOUT =
60
DEFAULT_OPEN_TIMEOUT =
60
DEFAULT_LANGUAGE =
:ru

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token: nil, refresh_token: nil, api_user_id: nil, api_key: nil, api_user_email: nil, api_user_password: nil, api_endpoint: nil, union_endpoint: nil, api_auth_method: nil, timeout: nil, open_timeout: nil, proxy: nil, ssl_options: nil, faraday_adapter: nil, symbolize_keys: false, debug: false, debug_options: { headers: true, bodies: false, errors: false, log_level: :info }, without_ratelimit: false, is_allow_access_to_coordinates: false, logger: nil, test: false, language: DEFAULT_LANGUAGE) ⇒ Request

Returns a new instance of Request.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rail-locator-api/request.rb', line 14

def initialize(access_token: nil, refresh_token: nil, api_user_id: nil, api_key: nil, api_user_email: nil, api_user_password: nil,
               api_endpoint: nil, union_endpoint: nil, api_auth_method: nil, timeout: nil, open_timeout: nil, proxy: nil, ssl_options: nil,
               faraday_adapter: nil, symbolize_keys: false, debug: false,
               debug_options: { headers: true, bodies: false, errors: false, log_level: :info },
               without_ratelimit: false, is_allow_access_to_coordinates: false,
               logger: nil, test: false, language: DEFAULT_LANGUAGE)

  @path_parts = []
  @api_key = api_key || self.class.api_key || ENV['RAIL_LOCATOR_API_KEY']
  @api_key = @api_key.strip if @api_key

  @api_user_id = api_user_id

  if @api_key == :keycloak
    @access_token = access_token || self.class.access_token || RailLocatorApi.generate_access_token.try(:dig, "access_token")
    @access_token = @access_token.strip if @access_token
    @refresh_token = refresh_token  || self.class.refresh_token
    @refresh_token = @refresh_token.strip if @refresh_token
  else
    @access_token = ""
    @refresh_token = ""
  end

  @api_user_email = api_user_email || ENV['API_USER_EMAIL'] || ""
  @api_user_password = api_user_password || ENV['API_USER_PASSWORD'] || ""

  @api_endpoint = api_endpoint || self.class.api_endpoint
  @api_endpoint = RailLocatorApi::api_endpoint if @api_endpoint.nil?
  @union_endpoint = union_endpoint || self.class.union_endpoint
  @union_endpoint = RailLocatorApi::union_endpoint if @union_endpoint.nil?

  @api_auth_method = api_auth_method || RailLocatorApi::api_auth_method || DEFAULT_AUTH_METHOD
  @timeout = timeout || self.class.timeout || DEFAULT_TIMEOUT
  @open_timeout = open_timeout || self.class.open_timeout || DEFAULT_OPEN_TIMEOUT
  @proxy = proxy || self.class.proxy || ENV['RAIL_LOCATOR_API_PROXY']
  @ssl_options = ssl_options || self.class.ssl_options || { version: "TLSv1_2" }
  @faraday_adapter = faraday_adapter || self.class.faraday_adapter || Faraday.default_adapter
  @symbolize_keys = symbolize_keys || self.class.symbolize_keys || false
  @debug = debug || self.class.debug || false
  @debug_options = debug_options || self.class.debug_options || { headers: true, bodies: false, errors: false, log_level: :info }
  @without_ratelimit = without_ratelimit || self.class.without_ratelimit
  @is_allow_access_to_coordinates = is_allow_access_to_coordinates || self.class.is_allow_access_to_coordinates
  @test = test || self.class.test || false
  @language = language || DEFAULT_LANGUAGE
  @logger = logger || self.class.logger || ::Logger.new(STDOUT)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



61
62
63
64
65
66
# File 'lib/rail-locator-api/request.rb', line 61

def method_missing(method, *args)
  @path_parts << method.to_s.downcase
  @path_parts << args if args.length > 0
  @path_parts.flatten!
  self
end

Class Attribute Details

.access_tokenObject

Returns the value of attribute access_token.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def access_token
  @access_token
end

.api_auth_methodObject

Returns the value of attribute api_auth_method.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def api_auth_method
  @api_auth_method
end

.api_endpointObject

Returns the value of attribute api_endpoint.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def api_endpoint
  @api_endpoint
end

.api_keyObject

Returns the value of attribute api_key.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def api_key
  @api_key
end

.api_user_emailObject

Returns the value of attribute api_user_email.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def api_user_email
  @api_user_email
end

.api_user_idObject

Returns the value of attribute api_user_id.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def api_user_id
  @api_user_id
end

.api_user_passwordObject

Returns the value of attribute api_user_password.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def api_user_password
  @api_user_password
end

.debugObject

Returns the value of attribute debug.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def debug
  @debug
end

.debug_optionsObject

Returns the value of attribute debug_options.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def debug_options
  @debug_options
end

.faraday_adapterObject

Returns the value of attribute faraday_adapter.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def faraday_adapter
  @faraday_adapter
end

.is_allow_access_to_coordinatesObject

Returns the value of attribute is_allow_access_to_coordinates.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def is_allow_access_to_coordinates
  @is_allow_access_to_coordinates
end

.languageObject

Returns the value of attribute language.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def language
  @language
end

.loggerObject

Returns the value of attribute logger.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def logger
  @logger
end

.notify_endpointObject

Returns the value of attribute notify_endpoint.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def notify_endpoint
  @notify_endpoint
end

.open_timeoutObject

Returns the value of attribute open_timeout.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def open_timeout
  @open_timeout
end

.proxyObject

Returns the value of attribute proxy.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def proxy
  @proxy
end

.refresh_tokenObject

Returns the value of attribute refresh_token.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def refresh_token
  @refresh_token
end

.ssl_optionsObject

Returns the value of attribute ssl_options.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def ssl_options
  @ssl_options
end

.symbolize_keysObject

Returns the value of attribute symbolize_keys.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def symbolize_keys
  @symbolize_keys
end

.testObject

Returns the value of attribute test.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def test
  @test
end

.timeoutObject

Returns the value of attribute timeout.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def timeout
  @timeout
end

.union_endpointObject

Returns the value of attribute union_endpoint.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def union_endpoint
  @union_endpoint
end

.without_ratelimitObject

Returns the value of attribute without_ratelimit.



137
138
139
# File 'lib/rail-locator-api/request.rb', line 137

def without_ratelimit
  @without_ratelimit
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def access_token
  @access_token
end

#api_auth_methodObject

Returns the value of attribute api_auth_method.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def api_auth_method
  @api_auth_method
end

#api_endpointObject

Returns the value of attribute api_endpoint.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def api_endpoint
  @api_endpoint
end

#api_keyObject

Returns the value of attribute api_key.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def api_key
  @api_key
end

#api_user_emailObject

Returns the value of attribute api_user_email.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def api_user_email
  @api_user_email
end

#api_user_idObject

Returns the value of attribute api_user_id.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def api_user_id
  @api_user_id
end

#api_user_passwordObject

Returns the value of attribute api_user_password.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def api_user_password
  @api_user_password
end

#debugObject

Returns the value of attribute debug.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def debug
  @debug
end

#debug_optionsObject

Returns the value of attribute debug_options.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def debug_options
  @debug_options
end

#faraday_adapterObject

Returns the value of attribute faraday_adapter.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def faraday_adapter
  @faraday_adapter
end

#is_allow_access_to_coordinatesObject

Returns the value of attribute is_allow_access_to_coordinates.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def is_allow_access_to_coordinates
  @is_allow_access_to_coordinates
end

#languageObject

Returns the value of attribute language.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def language
  @language
end

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def logger
  @logger
end

#notify_endpointObject

Returns the value of attribute notify_endpoint.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def notify_endpoint
  @notify_endpoint
end

#open_timeoutObject

Returns the value of attribute open_timeout.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def open_timeout
  @open_timeout
end

#proxyObject

Returns the value of attribute proxy.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def proxy
  @proxy
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def refresh_token
  @refresh_token
end

#ssl_optionsObject

Returns the value of attribute ssl_options.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def ssl_options
  @ssl_options
end

#symbolize_keysObject

Returns the value of attribute symbolize_keys.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def symbolize_keys
  @symbolize_keys
end

#testObject

Returns the value of attribute test.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def test
  @test
end

#timeoutObject

Returns the value of attribute timeout.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def timeout
  @timeout
end

#union_endpointObject

Returns the value of attribute union_endpoint.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def union_endpoint
  @union_endpoint
end

#without_ratelimitObject

Returns the value of attribute without_ratelimit.



3
4
5
# File 'lib/rail-locator-api/request.rb', line 3

def without_ratelimit
  @without_ratelimit
end

Class Method Details

.method_missing(sym, *args, &block) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rail-locator-api/request.rb', line 142

def method_missing(sym, *args, &block)
  new(access_token: self.access_token, refresh_token: self.refresh_token,
      api_user_id: self.api_user_id,
      api_key: self.api_key,
      api_user_email: self.api_user_email, api_user_password: self.api_user_password,
      api_auth_method: self.api_auth_method, api_endpoint: self.api_endpoint, union_endpoint: self.union_endpoint,
      timeout: self.timeout, open_timeout: self.open_timeout, faraday_adapter: self.faraday_adapter,
      symbolize_keys: self.symbolize_keys, debug: self.debug, debug_options: self.debug_options,
      without_ratelimit: self.without_ratelimit,
      is_allow_access_to_coordinates: self.is_allow_access_to_coordinates,
      proxy: self.proxy, ssl_options: self.ssl_options, logger: self.logger,
      test: self.test, language: self.language).send(sym, *args, &block)
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/rail-locator-api/request.rb', line 156

def respond_to_missing?(method_name, include_private = false)
  true
end

Instance Method Details

#create(params: nil, headers: nil, body: {}, format: nil) ⇒ Object



88
89
90
91
92
# File 'lib/rail-locator-api/request.rb', line 88

def create(params: nil, headers: nil, body: {}, format: nil)
  APIRequest.new(builder: self).post(params: params, headers: headers, body: body, format: format)
ensure
  reset
end

#delete(params: nil, headers: nil, body: {}) ⇒ Object



112
113
114
115
116
# File 'lib/rail-locator-api/request.rb', line 112

def delete(params: nil, headers: nil, body: {})
  APIRequest.new(builder: self).delete(params: params, headers: headers, body: body)
ensure
  reset
end

#insert(params: nil, headers: nil, body: {}, format: nil) ⇒ Object



100
101
102
103
104
# File 'lib/rail-locator-api/request.rb', line 100

def insert(params: nil, headers: nil, body: {}, format: nil)
  APIRequest.new(builder: self).put(params: params, headers: headers, body: body, format: format)
ensure
  reset
end

#pathObject



84
85
86
# File 'lib/rail-locator-api/request.rb', line 84

def path
  @path_parts.join('/')
end

#path_partsObject



80
81
82
# File 'lib/rail-locator-api/request.rb', line 80

def path_parts
  @path_parts
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/rail-locator-api/request.rb', line 68

def respond_to_missing?(method_name, include_private = false)
  true
end

#retrieve(params: nil, headers: nil, body: {}, format: nil) ⇒ Object



106
107
108
109
110
# File 'lib/rail-locator-api/request.rb', line 106

def retrieve(params: nil, headers: nil, body: {}, format: nil)
  APIRequest.new(builder: self).get(params: params, headers: headers, body: body, format: format)
ensure
  reset
end

#send(*args) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/rail-locator-api/request.rb', line 72

def send(*args)
  if args.length == 0
    method_missing(:send, args)
  else
    __send__(*args)
  end
end

#token_alive?(token, jwt_secret_code = RailLocatorApi.keycloak_client_secret) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rail-locator-api/request.rb', line 118

def token_alive?(token, jwt_secret_code=RailLocatorApi.keycloak_client_secret)
  begin
    return false if token.nil?
    exp = JWT.decode(token, jwt_secret_code, false).try(:first).try(:dig, "exp")
    return false if exp.nil?
    Time.at(exp) > Time.now + 30.second
  rescue => e
    #Sentry.capture_exception(e)
    false
  end
end

#update(params: nil, headers: nil, body: {}, format: nil) ⇒ Object



94
95
96
97
98
# File 'lib/rail-locator-api/request.rb', line 94

def update(params: nil, headers: nil, body: {}, format: nil)
  APIRequest.new(builder: self).patch(params: params, headers: headers, body: body, format: format)
ensure
  reset
end