Class: BenefitsIntake::Service

Inherits:
Common::Client::Base show all
Defined in:
lib/lighthouse/benefits_intake/service.rb

Overview

Proxy Service for the Lighthouse Benefits Intake API

Use this to submit claims that cannot be auto-established, via paper submission (electronic PDF submission to CMP). It is the responsibility of any team sending submissions to Lighthouse to monitor those submissions.

Defined Under Namespace

Classes: InvalidDocumentError

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.benefits_intake'
PDF_VALIDATOR_OPTIONS =
{
  size_limit_in_bytes: 100_000_000, # 100 MB
  check_page_dimensions: true,
  check_encryption: true,
  width_limit_in_inches: 78,
  height_limit_in_inches: 101
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



33
34
35
# File 'lib/lighthouse/benefits_intake/service.rb', line 33

def location
  @location
end

#uuidObject (readonly)

Returns the value of attribute uuid.



33
34
35
# File 'lib/lighthouse/benefits_intake/service.rb', line 33

def uuid
  @uuid
end

Instance Method Details

#bulk_status(uuids:) ⇒ Object

Get the status for a set of prior uploads

Parameters:

  • uuids (Array<String>)


96
97
98
99
100
# File 'lib/lighthouse/benefits_intake/service.rb', line 96

def bulk_status(uuids:)
  headers = { 'Content-Type' => Mime[:json].to_s, 'Accept' => Mime[:json].to_s }
  data = { ids: uuids }.to_json
  perform :post, 'uploads/report', data, headers
end

#download(uuid:) ⇒ Object

Download a zip of ‘what the server sees’ for a previous upload

Parameters:

  • uuid (String)


107
108
109
110
# File 'lib/lighthouse/benefits_intake/service.rb', line 107

def download(uuid:)
  headers = { 'Accept' => Mime[:zip].to_s }
  perform :get, "uploads/#{uuid}/download", {}, headers
end

#get_status(uuid:) ⇒ Object

Get the status for a previous upload

Parameters:

  • uuid (String)


86
87
88
89
# File 'lib/lighthouse/benefits_intake/service.rb', line 86

def get_status(uuid:)
  headers = { 'Accept' => Mime[:json].to_s }
  perform :get, "uploads/#{uuid}", {}, headers
end

#perform_upload(metadata:, document:, attachments: [], upload_url: nil) ⇒ Object

Perform the upload to BenefitsIntake parameters should be run through validation functions first, to prevent downstream processing errors

Parameters:

  • metadata (JSONString)

    metadata to be sent with upload, must be valid JSON

  • document (String)

    main document file path

  • attachments (Array<String>) (defaults to: [])

    attachment file path; optional, default = []

  • upload_url (String) (defaults to: nil)

    override instance upload_url; optional, default = @location

Raises:

  • (JSON::ParserError)

    if metadata is not a valid JSON String

  • (Errno::ENOENT)

    if document or each attachment are not valid Files



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lighthouse/benefits_intake/service.rb', line 47

def perform_upload(metadata:, document:, attachments: [], upload_url: nil)
  upload_url, _uuid = request_upload unless upload_url

   = JSON.parse()
  meta_tmp = Common::FileHelpers.generate_random_file(.to_json)

  params = {}
  params[:metadata] = Faraday::UploadIO.new(meta_tmp, Mime[:json].to_s, 'metadata.json')
  params[:content] = Faraday::UploadIO.new(document, Mime[:pdf].to_s, File.basename(document))
  attachments.each.with_index do |attachment, i|
    params[:"attachment#{i + 1}"] = Faraday::UploadIO.new(attachment, Mime[:pdf].to_s, File.basename(attachment))
  end

  perform :put, upload_url, params, { 'Content-Type' => 'multipart/form-data' }
ensure
  Common::FileHelpers.delete_file_if_exists(meta_tmp) if meta_tmp
end

#request_upload(refresh: false) ⇒ Object

Instantiates a new location and uuid for upload to BenefitsIntake

Parameters:

  • refresh (Boolean) (defaults to: false)


70
71
72
73
74
75
76
77
78
79
# File 'lib/lighthouse/benefits_intake/service.rb', line 70

def request_upload(refresh: false)
  if refresh || !(@location && @uuid)
    uploads = perform :post, 'uploads', {}, {}

    @location = uploads.body.dig('data', 'attributes', 'location')
    @uuid = uploads.body.dig('data', 'id')
  end

  [@location, @uuid]
end

#valid_document?(document:) ⇒ Boolean

Validate a file satisfies BenefitsIntake specifications. ** File must be a PDF.

Parameters:

  • document: (String)

    path to file

Returns:

  • (Boolean)

Raises:

See Also:



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/lighthouse/benefits_intake/service.rb', line 135

def valid_document?(document:)
  result = PDFUtilities::PDFValidator::Validator.new(document, PDF_VALIDATOR_OPTIONS).validate
  raise InvalidDocumentError, "Invalid Document: #{result.errors}" unless result.valid_pdf?

  doc = File.read(document, mode: 'rb')
  headers = { 'Content-Type': Marcel::MimeType.for(doc) }
  response = perform :post, 'uploads/validate_document', doc, headers
  raise InvalidDocumentError, "Invalid Document: #{response}" unless response.success?

  document
end

#valid_metadata?(metadata:) ⇒ Hash

Validate the metadata satisfies BenefitsIntake specifications.

Parameters:

  • metadata (Hash)

Returns:

  • (Hash)

    validated and corrected metadata

See Also:



120
121
122
# File 'lib/lighthouse/benefits_intake/service.rb', line 120

def valid_metadata?(metadata:)
  BenefitsIntake::Metadata.validate()
end

#valid_upload?(metadata:, document:, attachments: []) ⇒ Hash

Validate the upload meets BenefitsIntake specifications.

Parameters:

  • metadata (Hash)
  • document (String)
  • attachments; (Array<String>)

    optional, default []

Returns:

  • (Hash)

    payload for upload



156
157
158
159
160
161
162
# File 'lib/lighthouse/benefits_intake/service.rb', line 156

def valid_upload?(metadata:, document:, attachments: [])
  {
    metadata: valid_metadata?(metadata:),
    document: valid_document?(document:),
    attachments: attachments.map { |attachment| valid_document?(document: attachment) }
  }
end