Class: PapierkramApi::V1::Helper::PdfFromResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/papierkram_api/v1/helper/pdf_from_response.rb

Overview

This class is responsible to convert a response to a pdf file.

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ PdfFromResponse

Returns a new instance of PdfFromResponse.



8
9
10
# File 'lib/papierkram_api/v1/helper/pdf_from_response.rb', line 8

def initialize(response)
  @response = response
end

Instance Method Details

#to_pdf(filename = Time.now.to_s) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
# File 'lib/papierkram_api/v1/helper/pdf_from_response.rb', line 12

def to_pdf(filename = Time.now.to_s)
  raise ArgumentError, 'Response is not a Faraday::Response' unless @response.is_a?(Faraday::Response)
  raise ArgumentError, 'Response is not a PDF file' unless response.body.start_with?('%PDF-1.4')

  file = Tempfile.new([filename.to_s, '.pdf'], binmode: true)
  file.write(response.body)
  file.close

  { response: @response, path_to_pdf_file: file.path }
end