Class: Pictify::Client
- Inherits:
-
Object
- Object
- Pictify::Client
- Defined in:
- lib/pictify/client.rb
Overview
Client for the Pictify API.
Generate images, PDFs, and GIFs from raw HTML, live URLs, and reusable templates.
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://api.pictify.io"- DEFAULT_TIMEOUT =
30- DEFAULT_MAX_RETRIES =
3
Instance Method Summary collapse
-
#create_template(html:, name: nil, width: nil, height: nil, variable_definitions: nil, output_format: nil) ⇒ Template
Create a template from HTML.
-
#get_batch_results(batch_id) ⇒ BatchResults
Get the status, progress, and per-item results of a batch job.
-
#get_template(template_id) ⇒ Template
Get a single template by its UID.
-
#initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES) ⇒ Client
constructor
A new instance of Client.
-
#list_templates(page: nil, limit: nil, sort: nil) ⇒ ListTemplatesResult
List templates in your account.
-
#render(template_id:, variables: {}, format: nil, quality: nil, width: nil, height: nil, layout: nil, layouts: nil) ⇒ RenderResult
Render a single image (or PDF) from a template.
-
#render_batch(template_id:, variable_sets:, format: nil, quality: nil, concurrency: nil, layout: nil, layouts: nil) ⇒ BatchRenderResult
Submit an async batch render of a template across many variable sets.
-
#render_gif(html: nil, url: nil, template_id: nil, variables: nil, width: nil, height: nil, quality: nil) ⇒ GifRenderResult
Render an animated GIF from raw HTML, a live URL, or a template.
-
#render_html(html:, css: nil, width: nil, height: nil, selector: nil, format: nil) ⇒ ImageResult
Render an image (or PDF) directly from HTML.
-
#render_layouts(template_id:, layouts:, variables: {}, format: nil, quality: nil, width: nil, height: nil) ⇒ RenderResult
Render multiple layout variants of a template in a single call.
-
#render_url(url:, width: nil, height: nil, selector: nil, format: nil) ⇒ ImageResult
Screenshot a live URL.
Constructor Details
#initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES) ⇒ Client
Returns a new instance of Client.
32 33 34 35 36 37 38 39 |
# File 'lib/pictify/client.rb', line 32 def initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES) raise AuthenticationError, "API key is required" if api_key.nil? || api_key.empty? @api_key = api_key @base_url = base_url.chomp("/") @timeout = timeout @max_retries = max_retries end |
Instance Method Details
#create_template(html:, name: nil, width: nil, height: nil, variable_definitions: nil, output_format: nil) ⇒ Template
Create a template from HTML.
POST /templates — unwraps the { template } envelope. Variables are auto-discovered from {{variableName}} tokens in the HTML body.
278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/pictify/client.rb', line 278 def create_template(html:, name: nil, width: nil, height: nil, variable_definitions: nil, output_format: nil) response = request(:post, "templates", { html: html, name: name, width: width, height: height, variableDefinitions: variable_definitions, outputFormat: output_format.nil? ? nil : output_format.to_s }) Template.new(response["template"] || {}) end |
#get_batch_results(batch_id) ⇒ BatchResults
Get the status, progress, and per-item results of a batch job.
GET /templates/batch/:batch_id/results. Results carry index, success, and variables (plus error on failures) but NOT rendered URLs.
226 227 228 229 |
# File 'lib/pictify/client.rb', line 226 def get_batch_results(batch_id) response = request(:get, "templates/batch/#{encode(batch_id)}/results") BatchResults.new(response) end |
#get_template(template_id) ⇒ Template
Get a single template by its UID.
GET /templates/:uid — unwraps the { template } envelope.
241 242 243 244 |
# File 'lib/pictify/client.rb', line 241 def get_template(template_id) response = request(:get, "templates/#{encode(template_id)}") Template.new(response["template"] || {}) end |
#list_templates(page: nil, limit: nil, sort: nil) ⇒ ListTemplatesResult
List templates in your account.
GET /templates — returns a ListTemplatesResult with templates and pagination.
255 256 257 258 259 260 261 262 263 |
# File 'lib/pictify/client.rb', line 255 def list_templates(page: nil, limit: nil, sort: nil) params = {} params[:page] = page unless page.nil? params[:limit] = limit unless limit.nil? params[:sort] = sort.to_s unless sort.nil? response = request(:get, "templates", nil, params) ListTemplatesResult.new(response) end |
#render(template_id:, variables: {}, format: nil, quality: nil, width: nil, height: nil, layout: nil, layouts: nil) ⇒ RenderResult
Render a single image (or PDF) from a template.
POST /templates/:uid/render — returns the RenderResult results envelope, with a convenience url accessor (results.first.url).
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/pictify/client.rb', line 112 def render(template_id:, variables: {}, format: nil, quality: nil, width: nil, height: nil, layout: nil, layouts: nil) response = request(:post, "templates/#{encode(template_id)}/render", { variables: variables || {}, format: (format || :png).to_s, quality: quality, width: width, height: height, layout: layout, layouts: layouts }) RenderResult.new(response) end |
#render_batch(template_id:, variable_sets:, format: nil, quality: nil, concurrency: nil, layout: nil, layouts: nil) ⇒ BatchRenderResult
Submit an async batch render of a template across many variable sets.
POST /templates/:uid/batch-render — returns a BatchRenderResult with batch_id, status, total_items immediately (HTTP 202). Poll #get_batch_results to track progress.
Rendered URLs are NOT returned by the poll endpoint — they are delivered via the render.completed webhook.
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/pictify/client.rb', line 206 def render_batch(template_id:, variable_sets:, format: nil, quality: nil, concurrency: nil, layout: nil, layouts: nil) response = request(:post, "templates/#{encode(template_id)}/batch-render", { variableSets: variable_sets, format: (format || :png).to_s, quality: quality, concurrency: concurrency, layout: layout, layouts: layouts }) BatchRenderResult.new(response) end |
#render_gif(html: nil, url: nil, template_id: nil, variables: nil, width: nil, height: nil, quality: nil) ⇒ GifRenderResult
Render an animated GIF from raw HTML, a live URL, or a template.
POST /gif — the { gif: {…} } envelope is flattened to a GifRenderResult (url, uid, width, height, animation_length). Provide exactly one source: html, url, or template_id.
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/pictify/client.rb', line 171 def render_gif(html: nil, url: nil, template_id: nil, variables: nil, width: nil, height: nil, quality: nil) response = request(:post, "gif", { html: html, url: url, template: template_id, variables: variables, width: width, height: height, quality: (quality || :medium).to_s }) GifRenderResult.new(response["gif"] || {}) end |
#render_html(html:, css: nil, width: nil, height: nil, selector: nil, format: nil) ⇒ ImageResult
Render an image (or PDF) directly from HTML.
POST /image — returns an ImageResult with url, id, created_at.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/pictify/client.rb', line 59 def render_html(html:, css: nil, width: nil, height: nil, selector: nil, format: nil) body = css ? "<style>#{css}</style>#{html}" : html response = request(:post, "image", { html: body, width: width, height: height, selector: selector, fileExtension: (format || :png).to_s }) ImageResult.new(response) end |
#render_layouts(template_id:, layouts:, variables: {}, format: nil, quality: nil, width: nil, height: nil) ⇒ RenderResult
Render multiple layout variants of a template in a single call.
POST /templates/:uid/render with layouts — returns one results item per successful layout; missing/invalid layouts appear in errors.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/pictify/client.rb', line 140 def render_layouts(template_id:, layouts:, variables: {}, format: nil, quality: nil, width: nil, height: nil) render( template_id: template_id, variables: variables, format: format, quality: quality, width: width, height: height, layouts: layouts ) end |
#render_url(url:, width: nil, height: nil, selector: nil, format: nil) ⇒ ImageResult
Screenshot a live URL.
POST /image with url — returns an ImageResult.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/pictify/client.rb', line 83 def render_url(url:, width: nil, height: nil, selector: nil, format: nil) response = request(:post, "image", { url: url, width: width, height: height, selector: selector, fileExtension: (format || :png).to_s }) ImageResult.new(response) end |