Class: Etna::Clients::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/etna/clients/base_client.rb

Direct Known Subclasses

Janus, Magma, Metis, Polyphemus

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, token:, ignore_ssl: false, logger: nil) ⇒ BaseClient

Returns a new instance of BaseClient.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/etna/clients/base_client.rb', line 9

def initialize(host:, token:, ignore_ssl: false, logger: nil)
  raise "#{self.class.name} client configuration is missing host." unless host

  @token = token
  raise "Your token is expired." if token && token_expired?

  @etna_client = ::Etna::Client.new(
    host,
    token,
    routes_available: false,
    ignore_ssl: ignore_ssl,
    logger: logger)
  @host = host
  @ignore_ssl = ignore_ssl
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/etna/clients/base_client.rb', line 8

def host
  @host
end

#ignore_sslObject (readonly)

Returns the value of attribute ignore_ssl.



8
9
10
# File 'lib/etna/clients/base_client.rb', line 8

def ignore_ssl
  @ignore_ssl
end

#tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/etna/clients/base_client.rb', line 8

def token
  @token
end

Instance Method Details

#token_expired?Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/etna/clients/base_client.rb', line 25

def token_expired?
  # Has the token already expired?
  token_will_expire?(0)
end

#token_will_expire?(offset = 3000) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
# File 'lib/etna/clients/base_client.rb', line 30

def token_will_expire?(offset=3000)
  # offset in seconds
  # Will the user's token expire in the given amount of time?
  payload = JSON.parse(Base64.urlsafe_decode64(token.split('.')[1]))
  epoch_seconds = payload["exp"]
  expiration = DateTime.strptime(epoch_seconds.to_s, "%s")
  expiration <= DateTime.now.new_offset + offset
end