Class: Ecfr::PrinceXmlService::Pdf

Inherits:
Base show all
Defined in:
lib/ecfr/prince_xml_service/pdf.rb

Defined Under Namespace

Classes: PdfGenerationFailure

Constant Summary collapse

PDF_PATH =
""

Constants inherited from Base

Base::SUPPORTED_ARRAY_ACCESSORS

Instance Attribute Summary

Attributes inherited from Base

#metadata, #request_data, #response_status, #results

Class Method Summary collapse

Methods inherited from Base

basic_auth_client_options, #each, #initialize, metadata, metadata_key, result_key

Methods included from Extensible

#inherited

Methods included from AttributeMethodDefinition

included, #initialize

Methods inherited from Client

build, cache_key, client_pool, delete, execute, get, handle_response, perform, post, purge

Methods included from Ecfr::ParallelClient

included

Constructor Details

This class inherits a constructor from Ecfr::Base

Class Method Details

.client(client_options = {}, &block) ⇒ Object



8
9
10
11
# File 'lib/ecfr/prince_xml_service/pdf.rb', line 8

def self.client(client_options = {}, &block)
  client_options[:timeout] = Ecfr.config.prince_xml_service_pdf_timeout
  super(client_options: client_options, &block)
end

.html_to_pdf(string, output_path, sha) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ecfr/prince_xml_service/pdf.rb', line 13

def self.html_to_pdf(string, output_path, sha)
  response = post(PDF_PATH, {html: string})

  if response.status != 200
    raise PdfGenerationFailure.new(response.body)
  else
    directory = File.dirname(output_path)
    FileUtils.mkdir_p(directory) unless File.directory?(directory)

    # re Encoding::UndefinedConversionError ("\xE2" from ASCII-8BIT to UTF-8)
    # faraday force_encoding https://github.com/lostisland/faraday/issues/139
    File.write(output_path, response.body.force_encoding("utf-8"))

    File.write(File.join(directory, "#{sha}.digest"), output_path)
  end
end