Class: Zyterb::Client

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

Constant Summary collapse

API_URL =
"https://api.zyte.com/v1/extract".freeze
AI_FIELDS =

List of known Zyte AI extraction fields

[
  :article, :articleList, :articleNavigation,
  :product, :productList, :productNavigation,
  :jobPosting, :jobPostingNavigation,
  :forumThread, :pageContent, :serp
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


22
23
24
25
# File 'lib/zyterb/client.rb', line 22

def initialize(api_key: nil)
  @api_key = api_key || ENV['ZYTE_API_KEY'] || Zyterb.configuration.api_key
  raise ArgumentError, "API key is required. Set ENV['ZYTE_API_KEY'], use Zyterb.configure, or pass it to Client.new" if @api_key.nil? || @api_key.empty?
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



20
21
22
# File 'lib/zyterb/client.rb', line 20

def api_key
  @api_key
end

Instance Method Details

#extract(url, options = {}) ⇒ Hash

Extract data from a URL.

Parameters:

  • url (String)

    The URL to extract data from.

  • options (Hash) (defaults to: {})

    Additional options for Zyte API (e.g., article: true, browserHtml: true).

Returns:

  • (Hash)

    The parsed JSON response from Zyte.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/zyterb/client.rb', line 31

def extract(url, options = {})
  payload = { url: url }.merge(options)
  
  # Default to browserHtml if no specific extraction type is requested
  extraction_keys = [:browserHtml, :httpResponseBody, :screenshot] + AI_FIELDS
  if (payload.keys & extraction_keys).empty?
    payload[:browserHtml] = true
  end

  response = request(payload)
  parse_response(response)
end

#screenshot(url, format: 'png', full_page: false, options: {}) ⇒ String

Take a screenshot of a URL.

Parameters:

  • url (String)

    The URL to screenshot.

  • format (String) (defaults to: 'png')

    'png' (default) or 'jpeg'.

  • full_page (Boolean) (defaults to: false)

    Capture the full page or just the viewport.

  • options (Hash) (defaults to: {})

    Additional options to merge into the payload.

Returns:

  • (String)

    The raw binary image data.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/zyterb/client.rb', line 50

def screenshot(url, format: 'png', full_page: false, options: {})
  payload = {
    url: url,
    screenshot: true,
    screenshotOptions: { format: format, fullPage: full_page }
  }.merge(options)
  
  response = request(payload)
  parsed = parse_response(response)
  
  if parsed['screenshot']
    Base64.decode64(parsed['screenshot'])
  else
    raise Error, "No screenshot returned from Zyte API"
  end
end