Class: Zenrows::ApiResponse
- Inherits:
-
Object
- Object
- Zenrows::ApiResponse
- 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.
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
Response body as string.
-
#options ⇒ Hash
readonly
Request options used.
-
#raw ⇒ HTTP::Response
readonly
Raw HTTP response.
-
#status ⇒ Integer
readonly
HTTP status code.
Instance Method Summary collapse
-
#concurrency_limit ⇒ Integer?
Concurrency limit from headers.
-
#concurrency_remaining ⇒ Integer?
Remaining concurrency from headers.
-
#data ⇒ Hash, ...
Parsed data (for JSON responses).
-
#extracted ⇒ Hash
Alias for parsed data when using css_extractor.
-
#final_url ⇒ String?
Final URL after redirects.
-
#headers ⇒ Hash
Response headers.
-
#html ⇒ String
HTML content.
-
#initialize(http_response, options = {}) ⇒ ApiResponse
constructor
Initialize response wrapper.
-
#js_instructions_report ⇒ Hash?
JS instructions execution report (when json_response: true).
-
#markdown ⇒ String
Markdown content (when response_type: 'markdown').
-
#parsed ⇒ Hash
Parsed/extracted data (for autoparse or css_extractor).
-
#request_cost ⇒ Float?
Request cost from headers.
-
#screenshot ⇒ String?
Screenshot data (when screenshot options used with json_response).
-
#success? ⇒ Boolean
Check if response is successful.
-
#xhr ⇒ Array?
XHR/fetch request data (when json_response: true).
Constructor Details
#initialize(http_response, options = {}) ⇒ ApiResponse
Initialize response wrapper
50 51 52 53 54 55 56 |
# File 'lib/zenrows/api_response.rb', line 50 def initialize(http_response, = {}) @raw = http_response @status = http_response.status.code = @body = http_response.body.to_s @parsed_json = nil end |
Instance Attribute Details
#body ⇒ String (readonly)
Response body as string
61 62 63 |
# File 'lib/zenrows/api_response.rb', line 61 def body @body end |
#options ⇒ Hash (readonly)
Returns Request options used.
44 45 46 |
# File 'lib/zenrows/api_response.rb', line 44 def end |
#raw ⇒ HTTP::Response (readonly)
Returns Raw HTTP response.
38 39 40 |
# File 'lib/zenrows/api_response.rb', line 38 def raw @raw end |
#status ⇒ Integer (readonly)
Returns HTTP status code.
41 42 43 |
# File 'lib/zenrows/api_response.rb', line 41 def status @status end |
Instance Method Details
#concurrency_limit ⇒ Integer?
Concurrency limit from headers
135 136 137 |
# File 'lib/zenrows/api_response.rb', line 135 def concurrency_limit headers["Concurrency-Limit"]&.to_i end |
#concurrency_remaining ⇒ Integer?
Remaining concurrency from headers
142 143 144 |
# File 'lib/zenrows/api_response.rb', line 142 def concurrency_remaining headers["Concurrency-Remaining"]&.to_i end |
#data ⇒ Hash, ...
Parsed data (for JSON responses)
66 67 68 |
# File 'lib/zenrows/api_response.rb', line 66 def data @parsed_json ||= parse_body end |
#extracted ⇒ Hash
Alias for parsed data when using css_extractor
100 101 102 |
# File 'lib/zenrows/api_response.rb', line 100 def extracted data end |
#final_url ⇒ String?
Final URL after redirects
156 157 158 |
# File 'lib/zenrows/api_response.rb', line 156 def final_url headers["Zr-Final-Url"] end |
#headers ⇒ Hash
Response headers
128 129 130 |
# File 'lib/zenrows/api_response.rb', line 128 def headers @raw.headers.to_h end |
#html ⇒ String
HTML content
Returns HTML from json_response data or raw body
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_report ⇒ Hash?
JS instructions execution report (when json_response: true)
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 |
#markdown ⇒ String
Markdown content (when response_type: 'markdown')
86 87 88 |
# File 'lib/zenrows/api_response.rb', line 86 def markdown @body end |
#parsed ⇒ Hash
Parsed/extracted data (for autoparse or css_extractor)
93 94 95 |
# File 'lib/zenrows/api_response.rb', line 93 def parsed data end |
#request_cost ⇒ Float?
Request cost from headers
149 150 151 |
# File 'lib/zenrows/api_response.rb', line 149 def request_cost headers["X-Request-Cost"]&.to_f end |
#screenshot ⇒ String?
Screenshot data (when screenshot options used with json_response)
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
163 164 165 |
# File 'lib/zenrows/api_response.rb', line 163 def success? status >= 200 && status < 300 end |
#xhr ⇒ Array?
XHR/fetch request data (when json_response: true)
107 108 109 |
# File 'lib/zenrows/api_response.rb', line 107 def xhr data.is_a?(Hash) ? data["xhr"] : nil end |