Class: MailAutomation::Client

Inherits:
Common::Client::Base show all
Includes:
Common::Client::Concerns::Monitoring
Defined in:
lib/mail_automation/client.rb

Instance Method Summary collapse

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

#increment, #increment_failure, #increment_total, #with_monitoring

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

Methods included from SentryLogging

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

Constructor Details

#initialize(params) ⇒ Client

Initializes the MAS client.

MailAutomation::Client.new(

claim_id: 1234
file_number: 1234
form526: {
form526_uploads: []

})

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mail_automation/client.rb', line 22

def initialize(params)
  @claim_id = params[:claim_id]
  @file_number = params[:file_number]
  @form526 = params[:form526]
  @form526_uploads = params[:form526_uploads]

  raise ArgumentError, 'no file_number passed in for API request.' if @file_number.blank?
  raise ArgumentError, 'no claim_id passed in for API request.' if @claim_id.blank?
  raise ArgumentError, 'no form526 passed in for API request.' if @form526.blank?
  raise ArgumentError, 'no disabilities passed in for API request.' if @form526.dig('form526', 'form526',
                                                                                    'disabilities').blank?
end

Instance Method Details

#authenticate(params) ⇒ Object (private)



48
49
50
51
52
53
54
55
# File 'lib/mail_automation/client.rb', line 48

def authenticate(params)
  perform(
    :post,
    Settings.mail_automation.token_endpoint,
    URI.encode_www_form(params),
    { 'Content-Type': 'application/x-www-form-urlencoded' }
  )
end

#authentication_bodyObject (private)



66
67
68
69
70
71
72
73
# File 'lib/mail_automation/client.rb', line 66

def authentication_body
  {
    grant_type: 'client_credentials',
    scope: 'openid',
    client_id: Settings.mail_automation.client_id,
    client_secret: Settings.mail_automation.client_secret
  }.as_json
end

#headers_hashObject (private)



57
58
59
# File 'lib/mail_automation/client.rb', line 57

def headers_hash
  Configuration.base_request_headers.merge({ Authorization: "Bearer #{retrieve_bearer_token}" })
end

#initiate_apcas_processingObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/mail_automation/client.rb', line 35

def initiate_apcas_processing
  params = {
    file_number: @file_number,
    claim_id: @claim_id,
    form526: @form526['form526'],
    form526_uploads: @form526_uploads
  }

  perform(:post, Settings.mail_automation.endpoint, params.to_json.to_s, headers_hash)
end

#retrieve_bearer_tokenObject (private)

NOTE: This is intentionally not memoized because each request needs to re-authorize



62
63
64
# File 'lib/mail_automation/client.rb', line 62

def retrieve_bearer_token
  authenticate(authentication_body).body['access_token']
end