Class: BenefitsIntake::Service
- Inherits:
-
Common::Client::Base
- Object
- Common::Client::Base
- BenefitsIntake::Service
- 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
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
Instance Method Summary collapse
-
#bulk_status(uuids:) ⇒ Object
Get the status for a set of prior uploads.
-
#download(uuid:) ⇒ Object
Download a zip of ‘what the server sees’ for a previous upload.
-
#get_status(uuid:) ⇒ Object
Get the status for a previous upload.
-
#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.
-
#request_upload(refresh: false) ⇒ Object
Instantiates a new location and uuid for upload to BenefitsIntake.
-
#valid_document?(document:) ⇒ Boolean
Validate a file satisfies BenefitsIntake specifications.
-
#valid_metadata?(metadata:) ⇒ Hash
Validate the metadata satisfies BenefitsIntake specifications.
-
#valid_upload?(metadata:, document:, attachments: []) ⇒ Hash
Validate the upload meets BenefitsIntake specifications.
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
#location ⇒ Object (readonly)
Returns the value of attribute location.
33 34 35 |
# File 'lib/lighthouse/benefits_intake/service.rb', line 33 def location @location end |
#uuid ⇒ Object (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
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
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
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
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() = Common::FileHelpers.generate_random_file(.to_json) params = {} params[:metadata] = Faraday::UploadIO.new(, Mime[:json].to_s, 'metadata.json') params[:content] = Faraday::UploadIO.new(document, Mime[:pdf].to_s, File.basename(document)) .each.with_index do |, i| params[:"attachment#{i + 1}"] = Faraday::UploadIO.new(, Mime[:pdf].to_s, File.basename()) end perform :put, upload_url, params, { 'Content-Type' => 'multipart/form-data' } ensure Common::FileHelpers.delete_file_if_exists() if end |
#request_upload(refresh: false) ⇒ Object
Instantiates a new location and uuid for upload to BenefitsIntake
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.
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.
120 121 122 |
# File 'lib/lighthouse/benefits_intake/service.rb', line 120 def (metadata:) BenefitsIntake::Metadata.validate() end |
#valid_upload?(metadata:, document:, attachments: []) ⇒ Hash
Validate the upload meets BenefitsIntake specifications.
156 157 158 159 160 161 162 |
# File 'lib/lighthouse/benefits_intake/service.rb', line 156 def valid_upload?(metadata:, document:, attachments: []) { metadata: (metadata:), document: valid_document?(document:), attachments: .map { || valid_document?(document: ) } } end |