Class: Html2Pdf::Rails::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/html2pdf/rails/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint) ⇒ Client

Returns a new instance of Client.



12
13
14
# File 'lib/html2pdf/rails/client.rb', line 12

def initialize(endpoint)
  @uri = URI.parse(endpoint)
end

Class Method Details

.post(*args) ⇒ Object



8
9
10
# File 'lib/html2pdf/rails/client.rb', line 8

def self.post(*args)
  self.new(Html2Pdf.config.endpoint).post(*args)
end

Instance Method Details

#post(html:, put_to_storage: false, file_name: nil, disposition: null, pdf_options: {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/html2pdf/rails/client.rb', line 16

def post(html:, put_to_storage: false, file_name: nil, disposition: null, pdf_options: {})
  http = Net::HTTP.new(@uri.host, @uri.port).tap { |h| h.use_ssl = @uri.scheme == 'https' }
  request = Net::HTTP::Post.new(@uri.request_uri, headers)
  request.body = {
    html: html,
    putToStorage: put_to_storage,
    fileName: file_name,
    responseDisposition: disposition,
    pdfOptions: pdf_options
  }.to_json
  response = http.request(request)

  case response.code
  when '200'
    response.body
  when '503'
    raise Html2Pdf::Rails::ServiceUnavailableError.new(response)
  else
    raise Html2Pdf::Rails::RequestError.new(response)
  end
rescue Net::ReadTimeout
  raise Html2Pdf::Rails::NetworkError
end