Class: Chip::Service

Inherits:
Common::Client::Base show all
Includes:
Common::Client::Concerns::Monitoring, SentryLogging
Defined in:
lib/chip/service.rb

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.chip'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common::Client::Concerns::Monitoring

#increment, #increment_failure, #increment_total, #with_monitoring

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Methods inherited from Common::Client::Base

#config, configuration, #connection, #delete, #get, #perform, #post, #put, #raise_backend_exception, #raise_not_authenticated, #request, #sanitize_headers!, #service_name

Constructor Details

#initialize(opts = {}) ⇒ Service

Returns a new instance of Service.



34
35
36
37
38
39
40
41
42
43
# File 'lib/chip/service.rb', line 34

def initialize(opts = {})
  @tenant_name = opts[:tenant_name]
  @tenant_id = opts[:tenant_id]
  @username = opts[:username]
  @password = opts[:password]
  validate_arguments!

  @redis_client = RedisClient.build(tenant_id)
  super()
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



17
18
19
# File 'lib/chip/service.rb', line 17

def password
  @password
end

#redis_clientObject (readonly)

Returns the value of attribute redis_client.



17
18
19
# File 'lib/chip/service.rb', line 17

def redis_client
  @redis_client
end

#tenant_idObject (readonly)

Returns the value of attribute tenant_id.



17
18
19
# File 'lib/chip/service.rb', line 17

def tenant_id
  @tenant_id
end

#tenant_nameObject (readonly)

Returns the value of attribute tenant_name.



17
18
19
# File 'lib/chip/service.rb', line 17

def tenant_name
  @tenant_name
end

#usernameObject (readonly)

Returns the value of attribute username.



17
18
19
# File 'lib/chip/service.rb', line 17

def username
  @username
end

Class Method Details

.build(opts = {}) ⇒ Service

Builds a Service instance

Parameters:

  • opts (Hash) (defaults to: {})

    options to create the object

Options Hash (opts):

  • :tenant_name (String)
  • :tenant_id (String)
  • :username (String)
  • :password (String)

Returns:

  • (Service)

    an instance of this class



30
31
32
# File 'lib/chip/service.rb', line 30

def self.build(opts = {})
  new(opts)
end

Instance Method Details

#default_headersObject (private)



105
106
107
108
109
110
# File 'lib/chip/service.rb', line 105

def default_headers
  {
    'Content-Type' => 'application/json',
    'x-apigw-api-id' => config.api_gtwy_id
  }
end

#get_demographics(patient_dfn:, station_no:) ⇒ Faraday::Response

Get the auth demographics data from CHIP

Returns:

  • (Faraday::Response)

    response from CHIP authenticated-demographics endpoint



50
51
52
53
54
55
# File 'lib/chip/service.rb', line 50

def get_demographics(patient_dfn:, station_no:)
  with_monitoring_and_error_handling do
    perform(:get, "/#{config.base_path}/actions/authenticated-demographics",
            { patientDfn: patient_dfn, stationNo: station_no }, request_headers)
  end
end

#get_tokenFaraday::Response

Get the auth token from CHIP

Returns:

  • (Faraday::Response)

    response from CHIP token endpoint



75
76
77
78
79
# File 'lib/chip/service.rb', line 75

def get_token
  with_monitoring do
    perform(:post, "/#{config.base_path}/token", {}, token_headers)
  end
end

#post_patient_check_in(appointment_ien:, patient_dfn:, station_no:) ⇒ Faraday::Response

Post the check-in status to CHIP

Returns:

  • (Faraday::Response)

    response from CHIP authenticated-check-in endpoint



86
87
88
89
90
91
92
# File 'lib/chip/service.rb', line 86

def post_patient_check_in(appointment_ien:, patient_dfn:, station_no:)
  with_monitoring_and_error_handling do
    perform(:post, "/#{config.base_path}/actions/authenticated-checkin",
            { appointmentIen: appointment_ien, patientDfn: patient_dfn, stationNo: station_no },
            request_headers)
  end
end

#request_headersObject (private)



96
97
98
# File 'lib/chip/service.rb', line 96

def request_headers
  default_headers.merge('Authorization' => "Bearer #{token}")
end

#tokenObject (private)



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/chip/service.rb', line 112

def token
  @token ||= begin
    token = redis_client.get
    if token.present?
      token
    else
      resp = get_token

      Oj.load(resp.body)&.fetch('token').tap do |jwt_token|
        redis_client.save(token: jwt_token)
      end
    end
  end
end

#token_headersObject (private)



100
101
102
103
# File 'lib/chip/service.rb', line 100

def token_headers
  claims_token = Base64.encode64("#{username}:#{password}")
  default_headers.merge('Authorization' => "Basic #{claims_token}")
end

#update_demographics(patient_dfn:, station_no:, demographic_confirmations:) ⇒ Faraday::Response

Post the demographics confirmation data to CHIP

Returns:

  • (Faraday::Response)

    response from CHIP authenticated-demographics endpoint



62
63
64
65
66
67
68
# File 'lib/chip/service.rb', line 62

def update_demographics(patient_dfn:, station_no:, demographic_confirmations:)
  with_monitoring_and_error_handling do
    perform(:post, "/#{config.base_path}/actions/authenticated-demographics",
            { patientDfn: patient_dfn, stationNo: station_no, demographicConfirmations: demographic_confirmations },
            request_headers)
  end
end

#validate_arguments!Object (private)

Raises:

  • (ArgumentError)


140
141
142
143
144
145
# File 'lib/chip/service.rb', line 140

def validate_arguments!
  raise ArgumentError, 'Invalid username' if username.blank?
  raise ArgumentError, 'Invalid password' if password.blank?
  raise ArgumentError, 'Invalid tenant parameters' if tenant_name.blank? || tenant_id.blank?
  raise ArgumentError, 'Tenant parameters do not exist' unless config.valid_tenant?(tenant_name:, tenant_id:)
end

#with_monitoring_and_error_handlingObject (private)



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/chip/service.rb', line 127

def with_monitoring_and_error_handling(&)
  with_monitoring(2, &)
rescue => e
  log_exception_to_sentry(e,
                          {
                            url: "#{config.url}/#{config.base_path}",
                            original_body: e.original_body,
                            original_status: e.original_status
                          },
                          { external_service: self.class.to_s.underscore, team: 'check-in' })
  raise e
end