Class: ZyteClient

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

Overview

require ‘watir’

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h = {}) ⇒ ZyteClient

Returns a new instance of ZyteClient.



11
12
13
# File 'lib/zyte-client.rb', line 11

def initialize(h={})
    self.key = h[:key] # mandatory
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/zyte-client.rb', line 9

def key
  @key
end

Instance Method Details

#extract(url:, json_parsing: true, options: {}) ⇒ Object

call Zyte API’s extract access point.

url: the URL to extract. options: additonal Zyte API’s options to the default request. Default request is: url. json_parsing: if true, the response is parsed as JSON using jq command, and return the value of the ‘httpResponseBody` key. Default is true.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zyte-client.rb', line 21

def extract(url:, json_parsing: true, options:{})
    data = {
        "url": url,
    }.merge(options)

    File.open('zyte.data.json', 'w') { |file| file.write(data.to_json) }

    if json_parsing
        cmd = "curl \
        --silent \
        --user #{self.key}: \
        --header 'Content-Type: application/json' \
        --data @zyte.data.json \
        --compressed \
        https://api.zyte.com/v1/extract \
        | jq --raw-output .httpResponseBody \
        | base64 --decode 
        "
    else
        cmd = "curl \
        --silent \
        --user #{self.key}: \
        --header 'Content-Type: application/json' \
        --data @zyte.data.json \
        --compressed \
        https://api.zyte.com/v1/extract 
        "
    end

    ret = `#{cmd}`

    return ret
end