Class: Zenrows::ApiResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/zenrows/api_response.rb

Overview

Response wrapper for ZenRows API responses

Provides convenient accessors for different response types based on the options used in the request.

Examples:

HTML response

response = api.get(url)
response.html  # => "<html>..."

JSON response with XHR data

response = api.get(url, json_response: true)
response.data  # => { "html" => "...", "xhr" => [...] }
response.html  # => "<html>..."
response.xhr   # => [...]

Autoparse response

response = api.get(url, autoparse: true)
response.parsed  # => { "title" => "...", "price" => "..." }

CSS extraction

response = api.get(url, css_extractor: { title: 'h1' })
response.extracted  # => { "title" => "Page Title" }

Markdown response

response = api.get(url, response_type: 'markdown')
response.markdown  # => "# Page Title\n\n..."

Since:

  • 0.2.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response, options = {}) ⇒ ApiResponse

Initialize response wrapper

Parameters:

  • http_response (HTTP::Response)

    Raw HTTP response

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

    Request options

Since:

  • 0.2.0



50
51
52
53
54
55
56
# File 'lib/zenrows/api_response.rb', line 50

def initialize(http_response, options = {})
  @raw = http_response
  @status = http_response.status.code
  @options = options
  @body = http_response.body.to_s
  @parsed_json = nil
end

Instance Attribute Details

#bodyString (readonly)

Response body as string

Returns:

  • (String)

    Raw response body

Since:

  • 0.2.0



61
62
63
# File 'lib/zenrows/api_response.rb', line 61

def body
  @body
end

#optionsHash (readonly)

Returns Request options used.

Returns:

  • (Hash)

    Request options used

Since:

  • 0.2.0



44
45
46
# File 'lib/zenrows/api_response.rb', line 44

def options
  @options
end

#rawHTTP::Response (readonly)

Returns Raw HTTP response.

Returns:

  • (HTTP::Response)

    Raw HTTP response

Since:

  • 0.2.0



38
39
40
# File 'lib/zenrows/api_response.rb', line 38

def raw
  @raw
end

#statusInteger (readonly)

Returns HTTP status code.

Returns:

  • (Integer)

    HTTP status code

Since:

  • 0.2.0



41
42
43
# File 'lib/zenrows/api_response.rb', line 41

def status
  @status
end

Instance Method Details

#concurrency_limitInteger?

Concurrency limit from headers

Returns:

  • (Integer, nil)

    Max concurrent requests

Since:

  • 0.2.0



135
136
137
# File 'lib/zenrows/api_response.rb', line 135

def concurrency_limit
  headers["Concurrency-Limit"]&.to_i
end

#concurrency_remainingInteger?

Remaining concurrency from headers

Returns:

  • (Integer, nil)

    Available concurrent request slots

Since:

  • 0.2.0



142
143
144
# File 'lib/zenrows/api_response.rb', line 142

def concurrency_remaining
  headers["Concurrency-Remaining"]&.to_i
end

#dataHash, ...

Parsed data (for JSON responses)

Returns:

  • (Hash, Array, String)

    Parsed response data

Since:

  • 0.2.0



66
67
68
# File 'lib/zenrows/api_response.rb', line 66

def data
  @parsed_json ||= parse_body
end

#extractedHash

Alias for parsed data when using css_extractor

Returns:

  • (Hash)

    Extracted data

Since:

  • 0.2.0



100
101
102
# File 'lib/zenrows/api_response.rb', line 100

def extracted
  data
end

#final_urlString?

Final URL after redirects

Returns:

  • (String, nil)

    Final URL

Since:

  • 0.2.0



156
157
158
# File 'lib/zenrows/api_response.rb', line 156

def final_url
  headers["Zr-Final-Url"]
end

#headersHash

Response headers

Returns:

  • (Hash)

    Response headers

Since:

  • 0.2.0



128
129
130
# File 'lib/zenrows/api_response.rb', line 128

def headers
  @raw.headers.to_h
end

#htmlString

HTML content

Returns HTML from json_response data or raw body

Returns:

  • (String)

    HTML content

Since:

  • 0.2.0



75
76
77
78
79
80
81
# File 'lib/zenrows/api_response.rb', line 75

def html
  if json_response?
    data.is_a?(Hash) ? data["html"] : data
  else
    @body
  end
end

#js_instructions_reportHash?

JS instructions execution report (when json_response: true)

Returns:

  • (Hash, nil)

    Instructions report

Since:

  • 0.2.0



114
115
116
# File 'lib/zenrows/api_response.rb', line 114

def js_instructions_report
  data.is_a?(Hash) ? data["js_instructions_report"] : nil
end

#markdownString

Markdown content (when response_type: 'markdown')

Returns:

  • (String)

    Markdown content

Since:

  • 0.2.0



86
87
88
# File 'lib/zenrows/api_response.rb', line 86

def markdown
  @body
end

#parsedHash

Parsed/extracted data (for autoparse or css_extractor)

Returns:

  • (Hash)

    Structured data

Since:

  • 0.2.0



93
94
95
# File 'lib/zenrows/api_response.rb', line 93

def parsed
  data
end

#request_costFloat?

Request cost from headers

Returns:

  • (Float, nil)

    Credit cost of request

Since:

  • 0.2.0



149
150
151
# File 'lib/zenrows/api_response.rb', line 149

def request_cost
  headers["X-Request-Cost"]&.to_f
end

#screenshotString?

Screenshot data (when screenshot options used with json_response)

Returns:

  • (String, nil)

    Base64 encoded screenshot

Since:

  • 0.2.0



121
122
123
# File 'lib/zenrows/api_response.rb', line 121

def screenshot
  data.is_a?(Hash) ? data["screenshot"] : nil
end

#success?Boolean

Check if response is successful

Returns:

  • (Boolean)

Since:

  • 0.2.0



163
164
165
# File 'lib/zenrows/api_response.rb', line 163

def success?
  status >= 200 && status < 300
end

#xhrArray?

XHR/fetch request data (when json_response: true)

Returns:

  • (Array, nil)

    XHR request data

Since:

  • 0.2.0



107
108
109
# File 'lib/zenrows/api_response.rb', line 107

def xhr
  data.is_a?(Hash) ? data["xhr"] : nil
end