Class: ScreenshotFreeAPI::Resources::Screenshots
- Inherits:
-
Object
- Object
- ScreenshotFreeAPI::Resources::Screenshots
- Defined in:
- lib/screenshotfreeapi/resources/screenshots.rb
Overview
Screenshot capture endpoints with optional synchronous polling helpers.
All ‘web`, `mobile`, and `html` methods return a 202 job receipt. The `*_and_wait` variants block until the job completes and return the full result hash directly.
Instance Method Summary collapse
-
#html(options = {}) ⇒ Hash
Enqueue an HTML-string screenshot job.
-
#html_and_wait(options = {}, poll_interval: 2, timeout: 120) ⇒ Hash
Render an HTML string and block until the screenshot is ready.
-
#initialize(http) ⇒ Screenshots
constructor
A new instance of Screenshots.
-
#mobile(options = {}) ⇒ Hash
Enqueue a mobile app screenshot job.
-
#mobile_and_wait(options = {}, poll_interval: 2, timeout: 120) ⇒ Hash
Capture a mobile screenshot and block until it completes.
-
#wait(job_id, poll_interval: 2, timeout: 120) {|status| ... } ⇒ Hash
Poll a job until it reaches a terminal state.
-
#web(options = {}) ⇒ Hash
Enqueue a web screenshot job.
-
#web_and_wait(options = {}, poll_interval: 2, timeout: 120) {|status| ... } ⇒ Hash
Capture a web screenshot and block until it completes.
Constructor Details
#initialize(http) ⇒ Screenshots
Returns a new instance of Screenshots.
12 13 14 |
# File 'lib/screenshotfreeapi/resources/screenshots.rb', line 12 def initialize(http) @http = http end |
Instance Method Details
#html(options = {}) ⇒ Hash
Enqueue an HTML-string screenshot job.
64 65 66 |
# File 'lib/screenshotfreeapi/resources/screenshots.rb', line 64 def html( = {}) @http.request(:post, "/screenshots/html", body: camelize_keys()) end |
#html_and_wait(options = {}, poll_interval: 2, timeout: 120) ⇒ Hash
Render an HTML string and block until the screenshot is ready.
102 103 104 105 |
# File 'lib/screenshotfreeapi/resources/screenshots.rb', line 102 def html_and_wait( = {}, poll_interval: 2, timeout: 120) job = html() wait(job["jobId"], poll_interval: poll_interval, timeout: timeout) end |
#mobile(options = {}) ⇒ Hash
Enqueue a mobile app screenshot job.
51 52 53 |
# File 'lib/screenshotfreeapi/resources/screenshots.rb', line 51 def mobile( = {}) @http.request(:post, "/screenshots/mobile", body: camelize_keys()) end |
#mobile_and_wait(options = {}, poll_interval: 2, timeout: 120) ⇒ Hash
Capture a mobile screenshot and block until it completes.
90 91 92 93 |
# File 'lib/screenshotfreeapi/resources/screenshots.rb', line 90 def mobile_and_wait( = {}, poll_interval: 2, timeout: 120) job = mobile() wait(job["jobId"], poll_interval: poll_interval, timeout: timeout) end |
#wait(job_id, poll_interval: 2, timeout: 120) {|status| ... } ⇒ Hash
Poll a job until it reaches a terminal state.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/screenshotfreeapi/resources/screenshots.rb', line 117 def wait(job_id, poll_interval: 2, timeout: 120, &on_progress) deadline = Time.now + timeout loop do status_data = @http.request(:get, "/jobs/#{job_id}/status") on_progress.call(status_data) if on_progress case status_data["status"] when "completed" return @http.request(:get, "/jobs/#{job_id}/result") when "failed" reason = status_data["error"] || "Unknown failure" raise JobFailedError.new(job_id, reason) end if Time.now >= deadline raise JobTimeoutError.new(job_id, timeout) end sleep(poll_interval) end end |
#web(options = {}) ⇒ Hash
Enqueue a web screenshot job.
37 38 39 |
# File 'lib/screenshotfreeapi/resources/screenshots.rb', line 37 def web( = {}) @http.request(:post, "/screenshots/web", body: camelize_keys()) end |
#web_and_wait(options = {}, poll_interval: 2, timeout: 120) {|status| ... } ⇒ Hash
Capture a web screenshot and block until it completes.
Combines ‘web` + `wait` in a single call.
78 79 80 81 |
# File 'lib/screenshotfreeapi/resources/screenshots.rb', line 78 def web_and_wait( = {}, poll_interval: 2, timeout: 120, &on_progress) job = web() wait(job["jobId"], poll_interval: poll_interval, timeout: timeout, &on_progress) end |