Class: Zyterb::Client
- Inherits:
-
Object
- Object
- Zyterb::Client
- 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
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
Instance Method Summary collapse
-
#extract(url, options = {}) ⇒ Hash
Extract data from a URL.
-
#initialize(api_key: nil) ⇒ Client
constructor
A new instance of Client.
-
#screenshot(url, format: 'png', full_page: false, options: {}) ⇒ String
Take a screenshot of a URL.
Constructor Details
#initialize(api_key: nil) ⇒ Client
Returns a new instance of Client.
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_key ⇒ Object (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.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/zyterb/client.rb', line 31 def extract(url, = {}) payload = { url: url }.merge() # 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.
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() 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 |