Class: Html2img::RenderResponse
- Inherits:
-
Object
- Object
- Html2img::RenderResponse
- Defined in:
- lib/html2img/render_response.rb
Overview
The result of a successful render call.
Covers both the synchronous envelope (url populated) and the asynchronous
acceptance envelope returned when a webhook_url was supplied (status is
"processing" and url is nil until the webhook fires).
Instance Attribute Summary collapse
-
#credits_remaining ⇒ Integer?
readonly
Credits left after this call.
-
#expires_at ⇒ String?
readonly
When a hosted render expires, as an ISO 8601 string.
-
#id ⇒ String?
readonly
The render id.
- #message ⇒ String? readonly
-
#raw ⇒ Hash
readonly
The full decoded JSON payload.
-
#status ⇒ String?
readonly
"processing" for async jobs.
- #success ⇒ Boolean readonly
-
#template ⇒ String?
readonly
The template slug, when applicable.
-
#url ⇒ String?
readonly
The CDN URL of the render.
Class Method Summary collapse
-
.from_hash(data) ⇒ Object
Build a response from a decoded JSON payload.
Instance Method Summary collapse
-
#initialize(success: false, id: nil, url: nil, expires_at: nil, credits_remaining: nil, status: nil, message: nil, template: nil, raw: {}) ⇒ RenderResponse
constructor
A new instance of RenderResponse.
- #inspect ⇒ Object
-
#pdf? ⇒ Boolean
Whether the render came back as a PDF rather than an image.
-
#processing? ⇒ Boolean
Whether this is an async job still being rendered.
- #success? ⇒ Boolean
-
#to_s ⇒ Object
The URL, so a response drops straight into string interpolation.
Constructor Details
#initialize(success: false, id: nil, url: nil, expires_at: nil, credits_remaining: nil, status: nil, message: nil, template: nil, raw: {}) ⇒ RenderResponse
Returns a new instance of RenderResponse.
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/html2img/render_response.rb', line 72 def initialize(success: false, id: nil, url: nil, expires_at: nil, credits_remaining: nil, status: nil, message: nil, template: nil, raw: {}) @success = success @id = id @url = url @expires_at = expires_at @credits_remaining = credits_remaining @status = status @message = @template = template @raw = raw freeze end |
Instance Attribute Details
#credits_remaining ⇒ Integer? (readonly)
Returns credits left after this call.
25 26 27 |
# File 'lib/html2img/render_response.rb', line 25 def credits_remaining @credits_remaining end |
#expires_at ⇒ String? (readonly)
Returns when a hosted render expires, as an ISO 8601 string. Nil on paid plans, where renders stay hosted permanently; set on free-tier renders, which are hosted for seven days.
22 23 24 |
# File 'lib/html2img/render_response.rb', line 22 def expires_at @expires_at end |
#id ⇒ String? (readonly)
Returns the render id.
14 15 16 |
# File 'lib/html2img/render_response.rb', line 14 def id @id end |
#message ⇒ String? (readonly)
31 32 33 |
# File 'lib/html2img/render_response.rb', line 31 def @message end |
#raw ⇒ Hash (readonly)
Returns the full decoded JSON payload.
37 38 39 |
# File 'lib/html2img/render_response.rb', line 37 def raw @raw end |
#status ⇒ String? (readonly)
Returns "processing" for async jobs.
28 29 30 |
# File 'lib/html2img/render_response.rb', line 28 def status @status end |
#success ⇒ Boolean (readonly)
11 12 13 |
# File 'lib/html2img/render_response.rb', line 11 def success @success end |
#template ⇒ String? (readonly)
Returns the template slug, when applicable.
34 35 36 |
# File 'lib/html2img/render_response.rb', line 34 def template @template end |
#url ⇒ String? (readonly)
Returns the CDN URL of the render.
17 18 19 |
# File 'lib/html2img/render_response.rb', line 17 def url @url end |
Class Method Details
.from_hash(data) ⇒ Object
Build a response from a decoded JSON payload.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/html2img/render_response.rb', line 40 def self.from_hash(data) data = {} unless data.is_a?(Hash) new( success: data["success"] ? true : false, id: string_or_nil(data["id"]), url: string_or_nil(data["url"]), expires_at: string_or_nil(data["expires_at"]), credits_remaining: integer_or_nil(data["credits_remaining"]), status: string_or_nil(data["status"]), message: string_or_nil(data["message"]), template: string_or_nil(data["template"]), raw: data ) end |
Instance Method Details
#inspect ⇒ Object
110 111 112 113 |
# File 'lib/html2img/render_response.rb', line 110 def inspect "#<Html2img::RenderResponse url=#{url.inspect} status=#{status.inspect} " \ "credits_remaining=#{credits_remaining.inspect}>" end |
#pdf? ⇒ Boolean
Whether the render came back as a PDF rather than an image.
95 96 97 98 99 |
# File 'lib/html2img/render_response.rb', line 95 def pdf? return false if url.nil? url.to_s.split("?").first.to_s.downcase.end_with?(".pdf") end |
#processing? ⇒ Boolean
Whether this is an async job still being rendered.
When true, the final URL is delivered to the request's webhook_url
rather than being available on this response.
90 91 92 |
# File 'lib/html2img/render_response.rb', line 90 def processing? status == "processing" end |
#success? ⇒ Boolean
101 102 103 |
# File 'lib/html2img/render_response.rb', line 101 def success? success end |
#to_s ⇒ Object
The URL, so a response drops straight into string interpolation.
106 107 108 |
# File 'lib/html2img/render_response.rb', line 106 def to_s url.to_s end |