Class: ScreenshotFreeAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/screenshotfreeapi/client.rb

Overview

Main entry point for the ScreenshotFreeAPI Ruby SDK.

Instantiate once (e.g., in a Rails initializer) and reuse across requests. All resource objects share the same underlying HttpClient instance.

Quick start:

client = ScreenshotFreeAPI::Client.new(api_key: ENV["SCREENSHOTFREEAPI_KEY"])
result = client.capture("https://stripe.com", format: "png")
puts result["screenshots"].first["url"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: HttpClient::DEFAULT_BASE_URL, timeout: HttpClient::DEFAULT_TIMEOUT, max_retries: HttpClient::DEFAULT_RETRIES) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String)

    Your ScreenshotFreeAPI key (sfa_…)

  • base_url (String) (defaults to: HttpClient::DEFAULT_BASE_URL)

    Override base URL for staging / local dev

  • timeout (Integer) (defaults to: HttpClient::DEFAULT_TIMEOUT)

    HTTP open/read timeout in seconds (default: 30)

  • max_retries (Integer) (defaults to: HttpClient::DEFAULT_RETRIES)

    Max retry attempts on 429 / 5xx (default: 3)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/screenshotfreeapi/client.rb', line 22

def initialize(
  api_key:,
  base_url:    HttpClient::DEFAULT_BASE_URL,
  timeout:     HttpClient::DEFAULT_TIMEOUT,
  max_retries: HttpClient::DEFAULT_RETRIES
)
  @http = HttpClient.new(
    api_key:     api_key,
    base_url:    base_url,
    timeout:     timeout,
    max_retries: max_retries
  )

  @screenshots  = Resources::Screenshots.new(@http)
  @jobs         = Resources::Jobs.new(@http)
  @auth         = Resources::Auth.new(@http)
  @billing      = Resources::Billing.new(@http)
  @workspaces   = Resources::Workspaces.new(@http)
  @monitors     = Resources::Monitors.new(@http)
  @integrations = Resources::Integrations.new(@http)
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



16
17
18
# File 'lib/screenshotfreeapi/client.rb', line 16

def auth
  @auth
end

#billingObject (readonly)

Returns the value of attribute billing.



16
17
18
# File 'lib/screenshotfreeapi/client.rb', line 16

def billing
  @billing
end

#integrationsObject (readonly)

Returns the value of attribute integrations.



16
17
18
# File 'lib/screenshotfreeapi/client.rb', line 16

def integrations
  @integrations
end

#jobsObject (readonly)

Returns the value of attribute jobs.



16
17
18
# File 'lib/screenshotfreeapi/client.rb', line 16

def jobs
  @jobs
end

#monitorsObject (readonly)

Returns the value of attribute monitors.



16
17
18
# File 'lib/screenshotfreeapi/client.rb', line 16

def monitors
  @monitors
end

#screenshotsObject (readonly)

Returns the value of attribute screenshots.



16
17
18
# File 'lib/screenshotfreeapi/client.rb', line 16

def screenshots
  @screenshots
end

#workspacesObject (readonly)

Returns the value of attribute workspaces.



16
17
18
# File 'lib/screenshotfreeapi/client.rb', line 16

def workspaces
  @workspaces
end

Instance Method Details

#capture(url, poll_interval: 2, timeout: 120, **opts) {|status| ... } ⇒ Hash

Convenience method: capture a URL and wait for the result.

Equivalent to ‘client.screenshots.web_and_wait({ url: url }.merge(opts))`.

Parameters:

  • url (String)

    URL to capture

  • poll_interval (Numeric) (defaults to: 2)

    Seconds between status polls (default: 2)

  • timeout (Numeric) (defaults to: 120)

    Maximum seconds to wait (default: 120)

  • opts (Hash)

    Any additional web screenshot options

Yield Parameters:

  • status (Hash)

    Progress callback, called on each poll

Returns:

  • (Hash)

    Full result hash with “screenshots” array



55
56
57
58
# File 'lib/screenshotfreeapi/client.rb', line 55

def capture(url, poll_interval: 2, timeout: 120, **opts, &on_progress)
  options = { url: url }.merge(opts)
  @screenshots.web_and_wait(options, poll_interval: poll_interval, timeout: timeout, &on_progress)
end

#healthHash

Check API health (no authentication required).

Returns:

  • (Hash)

    { “status” => “ok”, “version” => “…” }



63
64
65
# File 'lib/screenshotfreeapi/client.rb', line 63

def health
  @http.request(:get, "/health", auth_header: "")
end