class Html2Pdf::Rails::Client

Public Class Methods

new(endpoint) click to toggle source
# File lib/html2pdf/rails/client.rb, line 12
def initialize(endpoint)
  @uri = URI.parse(endpoint)
end
post(*args) click to toggle source
# File lib/html2pdf/rails/client.rb, line 8
def self.post(*args)
  self.new(Html2Pdf.config.endpoint).post(*args)
end

Public Instance Methods

post(html:, put_to_storage: false, file_name: nil, disposition: nil, pdf_options: {}) click to toggle source
# File lib/html2pdf/rails/client.rb, line 16
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 = {
    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

Private Instance Methods

headers() click to toggle source
# File lib/html2pdf/rails/client.rb, line 42
def headers
  { 'Content-Type' => 'application/json' }
end