Class: Icanhazpdf::Client

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

Overview

include in order to request generation of pdfs from the icanhazpf service requires that icanhazpdf_api_key is configured in your environment config

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.api_keyObject

your icanhazpf api key



8
9
10
11
12
13
14
# File 'lib/icanhazpdf/client.rb', line 8

def self.api_key
  begin
    Rails.configuration.icanhazpdf_api_key
  rescue
    raise "No API Key Configured"
  end
end

.default_service_urlObject



16
17
18
# File 'lib/icanhazpdf/client.rb', line 16

def self.default_service_url
  'http://icanhazpdf.lsfapp.com/generate_pdf'
end

Instance Method Details

#pdf_from_url(full_url, options = {}) ⇒ Object

generate a pdf from the url passed



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/icanhazpdf/client.rb', line 21

def pdf_from_url(full_url, options = {})
  uri = URI(full_url)
  params = URI.decode_www_form(uri.query || "") << ['icanhazpdf', Icanhazpdf::Client::api_key]
  uri.query = URI.encode_www_form(params)
  begin
    service_url = Rails.configuration.icanhazpdf_url
  rescue
    service_url = Icanhazpdf::Client.default_service_url
  end
  encoded_url = "#{service_url}?url=#{URI.encode(uri.to_s).gsub(':', '%3A').gsub('/', '%2F').gsub('?', '%3F').gsub('=', '%3D').gsub('&', '%26')}"
  encoded_url += "&use_wkhtmltopdf=true" if options.has_key?(:use_wkhtmltopdf) && options[:use_wkhtmltopdf] == true
  encoded_url += "&margin=#{options[:margin]}" if options.has_key?(:margin)
  encoded_url += "&landscape=true" if options.has_key?(:landscape) && options[:landscape] == true

  HTTParty.get(encoded_url, :timeout => 10000)
end