Class: DockHealthApi::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/dock_health_api/client.rb

Constant Summary collapse

BUFFER =
120

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



12
13
14
# File 'lib/dock_health_api/client.rb', line 12

def initialize
  @config = DockHealthApi.config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/dock_health_api/client.rb', line 10

def config
  @config
end

Instance Method Details

#connectionObject



16
17
18
# File 'lib/dock_health_api/client.rb', line 16

def connection
  @connection ||= OAuth2::Client.new(config.api_key, config.api_secret, token_url: config.token_url, raise_errors: false)
end

#get_iframe_tokenObject



60
61
62
63
64
65
# File 'lib/dock_health_api/client.rb', line 60

def get_iframe_token
  @iframe_token_connection = connection.client_credentials.get_token(scope:"dockhealth/system.embedded.launch")
  return @iframe_token_connection if @iframe_token_connection.nil?
  @iframe_token_expiration_time = Time.now + @iframe_token_connection.expires_in
  @iframe_token_connection
end

#get_tokenObject



54
55
56
57
58
# File 'lib/dock_health_api/client.rb', line 54

def get_token
  @token_connection = connection.client_credentials.get_token(scope:"dockhealth/system.developer.read dockhealth/user.all.write dockhealth/user.all.read dockhealth/system.developer.write dockhealth/patient.all.read dockhealth/patient.all.write")
  @token_expiration_time = Time.now + @token_connection.expires_in
  @token_connection
end

#iframe_tokenObject



50
51
52
# File 'lib/dock_health_api/client.rb', line 50

def iframe_token
  @iframe_token ||= iframe_token_connection.token
end

#iframe_token_about_to_expire?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/dock_health_api/client.rb', line 42

def iframe_token_about_to_expire?
  Time.now + BUFFER > iframe_token_expiration_time
end

#iframe_token_connectionObject



33
34
35
36
# File 'lib/dock_health_api/client.rb', line 33

def iframe_token_connection
  get_iframe_token if @iframe_token_connection.nil? || iframe_token_about_to_expire?
  @iframe_token_connection
end

#iframe_token_expiration_timeObject



38
39
40
# File 'lib/dock_health_api/client.rb', line 38

def iframe_token_expiration_time
  @iframe_token_expiration_time ||= Time.now + @iframe_token_connection.expires_in
end

#tokenObject



46
47
48
# File 'lib/dock_health_api/client.rb', line 46

def token
  @token ||= token_connection.token
end

#token_about_to_expire?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/dock_health_api/client.rb', line 29

def token_about_to_expire?
  Time.now + BUFFER > token_expiration_time
end

#token_connectionObject



20
21
22
23
# File 'lib/dock_health_api/client.rb', line 20

def token_connection
  get_token if @token_connection.nil? || token_about_to_expire?
  @token_connection
end

#token_expiration_timeObject



25
26
27
# File 'lib/dock_health_api/client.rb', line 25

def token_expiration_time
  @token_expiration_time ||= Time.now + @token_connection.expires_in
end