Sha256: 22edf9942c70577f14a9658c6330f0819f232445786965fb06af326988e18b22

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

require "oauth2"
require 'singleton'

module DockHealthApi
  class Client
    BUFFER = 120

    include Singleton

    attr_reader :config

    def initialize
      @config = DockHealthApi.config
    end

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

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

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

    def token_about_to_expire?
      Time.now + BUFFER > token_expiration_time
    end

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

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

    def iframe_token_about_to_expire?
      Time.now + BUFFER > iframe_token_expiration_time
    end

    def token
      @token ||= token_connection.token
    end

    def iframe_token
      @iframe_token ||= iframe_token_connection.token
    end

    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

    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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dock_health_api-0.5.11 lib/dock_health_api/client.rb
dock_health_api-0.5.10 lib/dock_health_api/client.rb
dock_health_api-0.5.9 lib/dock_health_api/client.rb
dock_health_api-0.5.8 lib/dock_health_api/client.rb