Class: Html2img::RenderResponse

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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 = message
  @template = template
  @raw = raw
  freeze
end

Instance Attribute Details

#credits_remainingInteger? (readonly)

Returns credits left after this call.

Returns:

  • (Integer, nil)

    credits left after this call



25
26
27
# File 'lib/html2img/render_response.rb', line 25

def credits_remaining
  @credits_remaining
end

#expires_atString? (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.

Returns:

  • (String, nil)

    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

#idString? (readonly)

Returns the render id.

Returns:

  • (String, nil)

    the render id



14
15
16
# File 'lib/html2img/render_response.rb', line 14

def id
  @id
end

#messageString? (readonly)

Returns:

  • (String, nil)


31
32
33
# File 'lib/html2img/render_response.rb', line 31

def message
  @message
end

#rawHash (readonly)

Returns the full decoded JSON payload.

Returns:

  • (Hash)

    the full decoded JSON payload



37
38
39
# File 'lib/html2img/render_response.rb', line 37

def raw
  @raw
end

#statusString? (readonly)

Returns "processing" for async jobs.

Returns:

  • (String, nil)

    "processing" for async jobs



28
29
30
# File 'lib/html2img/render_response.rb', line 28

def status
  @status
end

#successBoolean (readonly)

Returns:

  • (Boolean)


11
12
13
# File 'lib/html2img/render_response.rb', line 11

def success
  @success
end

#templateString? (readonly)

Returns the template slug, when applicable.

Returns:

  • (String, nil)

    the template slug, when applicable



34
35
36
# File 'lib/html2img/render_response.rb', line 34

def template
  @template
end

#urlString? (readonly)

Returns the CDN URL of the render.

Returns:

  • (String, nil)

    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

#inspectObject



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


90
91
92
# File 'lib/html2img/render_response.rb', line 90

def processing?
  status == "processing"
end

#success?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/html2img/render_response.rb', line 101

def success?
  success
end

#to_sObject

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