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, app) ⇒ Client

Returns a new instance of Client.



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

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

Class Method Details

.post(**options) ⇒ Object



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

def self.post(**options)
  self.new(Html2Pdf.config.endpoint, Html2Pdf.config.app).post(**options)
end

Instance Method Details

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



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

def post(html:, put_to_storage: false, file_name: nil, disposition: nil, 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 = {
    app: @app,
    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