Class: Html2img::Client
- Inherits:
-
Object
- Object
- Html2img::Client
- Defined in:
- lib/html2img/client.rb
Overview
Client for the html2img.com API.
Render HTML documents you control, capture live URLs, or render named templates, each returning a RenderResponse. Every failure surfaces as an Error; no raw Net::HTTP exception escapes.
client = Html2img::Client.new # reads HTML2IMG_API_KEY
response = client.html("<h1>Hello</h1>", width: 1200, height: 630)
response.url # => "https://i.html2img.com/abc123.png"
A client is cheap to build and safe to share between threads.
Constant Summary collapse
- HTML_PATH =
"/api/html"- SCREENSHOT_PATH =
"/api/screenshot"- TEMPLATE_PATH =
"/api/v1/templates"
Instance Attribute Summary collapse
-
#base_url ⇒ String
readonly
The API base URL in use.
-
#timeout ⇒ Float
readonly
The per-request timeout in seconds.
Instance Method Summary collapse
-
#download(image) ⇒ String
Download the rendered bytes from a render's CDN URL.
-
#html(html, **options) ⇒ RenderResponse
Render an HTML document to an image or PDF (POST /api/html).
-
#initialize(api_key: nil, base_url: nil, timeout: nil, transport: nil) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
-
#save(image, path) ⇒ String
Download a render and write it to a local file.
-
#screenshot(url, **options) ⇒ RenderResponse
Capture a screenshot of a live URL (POST /api/screenshot).
-
#template(slug, data = {}, **fields) ⇒ RenderResponse
Render a named template from a data payload.
Constructor Details
#initialize(api_key: nil, base_url: nil, timeout: nil, transport: nil) ⇒ Client
Returns a new instance of Client.
40 41 42 43 44 45 46 47 |
# File 'lib/html2img/client.rb', line 40 def initialize(api_key: nil, base_url: nil, timeout: nil, transport: nil) config = Html2img.configuration @api_key = resolve_api_key(api_key || config.api_key) @base_url = resolve_base_url(base_url || config.base_url) @timeout = resolve_timeout(timeout || config.timeout) @transport = transport || config.transport || Transport.new end |
Instance Attribute Details
#base_url ⇒ String (readonly)
Returns the API base URL in use.
30 31 32 |
# File 'lib/html2img/client.rb', line 30 def base_url @base_url end |
#timeout ⇒ Float (readonly)
Returns the per-request timeout in seconds.
33 34 35 |
# File 'lib/html2img/client.rb', line 33 def timeout @timeout end |
Instance Method Details
#download(image) ⇒ String
Download the rendered bytes from a render's CDN URL.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/html2img/client.rb', line 95 def download(image) url = url_for(image) status, body = @transport.call( method: "GET", url: url, headers: { "Accept" => "*/*", "User-Agent" => user_agent }, body: nil, timeout: timeout ) if status >= 400 raise Error.new("Could not download the render from #{url}: HTTP #{status}.", status_code: status) end body end |
#html(html, **options) ⇒ RenderResponse
Render an HTML document to an image or PDF (POST /api/html).
client.html(document, width: 1200, height: 630, dpi: 2)
client.html(document, format: "pdf")
57 58 59 |
# File 'lib/html2img/client.rb', line 57 def html(html, **) post(HTML_PATH, Request.html_body(html, )) end |
#inspect ⇒ Object
132 133 134 |
# File 'lib/html2img/client.rb', line 132 def inspect "#<Html2img::Client base_url=#{base_url.inspect} timeout=#{timeout.inspect}>" end |
#save(image, path) ⇒ String
Download a render and write it to a local file.
Parent directories are created for you.
client.save(response, "og/post-42.png")
122 123 124 125 126 127 128 129 130 |
# File 'lib/html2img/client.rb', line 122 def save(image, path) require "fileutils" contents = download(image) FileUtils.mkdir_p(File.dirname(path)) File.binwrite(path, contents) path end |
#screenshot(url, **options) ⇒ RenderResponse
Capture a screenshot of a live URL (POST /api/screenshot).
client.screenshot("https://example.com", fullpage: true, selector: "#hero")
68 69 70 |
# File 'lib/html2img/client.rb', line 68 def screenshot(url, **) post(SCREENSHOT_PATH, Request.screenshot_body(url, )) end |
#template(slug, data = {}, **fields) ⇒ RenderResponse
Render a named template from a data payload.
client.template("invoice-image", number: 1042, amount: "£240.00")
client.template("invoice-image", { "number" => 1042 })
The data is validated server-side per template. Templates output PNG only.
84 85 86 87 88 |
# File 'lib/html2img/client.rb', line 84 def template(slug, data = {}, **fields) Request.required_string!("slug", slug) post("#{TEMPLATE_PATH}/#{encode(slug)}", data.merge(fields)) end |