Class: PayuPl::Statements::Retrieve

Inherits:
Operations::Base show all
Defined in:
lib/payu_pl/statements/retrieve.rb

Constant Summary collapse

FILENAME_REGEX =
/filename="?(?<filename>[^";]+)"?/i.freeze

Instance Method Summary collapse

Instance Method Details

#call(report_id) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/payu_pl/statements/retrieve.rb', line 8

def call(report_id)
  validate_id!(report_id, input_key: :report_id)

  response = transport.request(
    :get,
    Endpoints.report(report_id),
    headers: { "Accept" => "application/octet-stream" },
    return_headers: true
  )

  headers = response.fetch(:headers)
  content_type = headers["content-type"]
  content_disposition = headers["content-disposition"].to_s

  filename = content_disposition.match(FILENAME_REGEX)&.named_captures&.fetch("filename", nil)

  {
    data: response.fetch(:body),
    filename: filename,
    content_type: content_type,
    http_status: response.fetch(:http_status)
  }
end