Class: Html2rss::RequestService::Response
- Inherits:
-
Object
- Object
- Html2rss::RequestService::Response
- Defined in:
- lib/html2rss/request_service/response.rb
Overview
To be used by strategies to provide their response.
Constant Summary collapse
- EMPTY_TRANSPORT_META =
Default when a strategy does not attach transport telemetry.
{}.freeze
- EMPTY_CAPTURED_RESPONSES =
Default when a strategy does not capture sub-resource responses.
[].freeze
- HTML_BODY_SNIFF =
Bodies that look like HTML even when Content-Type is missing, empty, or wrong.
/\A\s*(?:<!DOCTYPE\s+html|<html)/i
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
The raw body of the response.
-
#captured_responses ⇒ Array<Hash>
readonly
Captured JSON XHR/fetch responses (empty when unsupported).
-
#headers ⇒ Hash{String => Object}
readonly
The headers of the response.
-
#status ⇒ Integer?
readonly
The HTTP status code when known.
-
#transport_meta ⇒ Hash
readonly
Allowlisted upstream transport telemetry.
-
#url ⇒ Html2rss::Url
readonly
The URL of the response.
Instance Method Summary collapse
-
#content_type ⇒ String
Normalized content type header value.
-
#feed_response? ⇒ Boolean
Whether response content is RSS/Atom (header or sniffed body).
-
#html_response? ⇒ Boolean
Whether response content is HTML (header or sniffed body, never JSON).
-
#initialize(body:, url:, headers: {}, status: nil, transport_meta: EMPTY_TRANSPORT_META, captured_responses: EMPTY_CAPTURED_RESPONSES) ⇒ Response
constructor
rubocop:disable-next Metrics/MethodLength, Metrics/ParameterLists -- transport + capture fields stay co-located.
-
#json_response? ⇒ Boolean
Whether response content is JSON.
-
#parsed_body ⇒ Nokogiri::HTML::Document, Hash
The parsed body of the response, frozen object.
Constructor Details
#initialize(body:, url:, headers: {}, status: nil, transport_meta: EMPTY_TRANSPORT_META, captured_responses: EMPTY_CAPTURED_RESPONSES) ⇒ Response
rubocop:disable-next Metrics/MethodLength, Metrics/ParameterLists -- transport + capture fields stay co-located
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/html2rss/request_service/response.rb', line 35 def initialize(body:, url:, headers: {}, status: nil, transport_meta: EMPTY_TRANSPORT_META, captured_responses: EMPTY_CAPTURED_RESPONSES) @body = body headers = headers.dup headers.transform_keys!(&:to_s) HashUtil.assert_string_keys!(headers, context: 'response headers', deep: false) @headers = headers @status = status @url = url @transport_meta = .nil? || .empty? ? EMPTY_TRANSPORT_META : .freeze @captured_responses = if captured_responses.nil? || captured_responses.empty? EMPTY_CAPTURED_RESPONSES else captured_responses.freeze end end |
Instance Attribute Details
#body ⇒ String (readonly)
Returns the raw body of the response.
55 56 57 |
# File 'lib/html2rss/request_service/response.rb', line 55 def body @body end |
#captured_responses ⇒ Array<Hash> (readonly)
Returns captured JSON XHR/fetch responses (empty when unsupported).
70 71 72 |
# File 'lib/html2rss/request_service/response.rb', line 70 def captured_responses @captured_responses end |
#headers ⇒ Hash{String => Object} (readonly)
Returns the headers of the response.
58 59 60 |
# File 'lib/html2rss/request_service/response.rb', line 58 def headers @headers end |
#status ⇒ Integer? (readonly)
Returns the HTTP status code when known.
61 62 63 |
# File 'lib/html2rss/request_service/response.rb', line 61 def status @status end |
#transport_meta ⇒ Hash (readonly)
Returns allowlisted upstream transport telemetry.
67 68 69 |
# File 'lib/html2rss/request_service/response.rb', line 67 def @transport_meta end |
#url ⇒ Html2rss::Url (readonly)
Returns the URL of the response.
64 65 66 |
# File 'lib/html2rss/request_service/response.rb', line 64 def url @url end |
Instance Method Details
#content_type ⇒ String
Returns normalized content type header value.
73 |
# File 'lib/html2rss/request_service/response.rb', line 73 def content_type = header('content-type').to_s |
#feed_response? ⇒ Boolean
Returns whether response content is RSS/Atom (header or sniffed body).
84 85 86 87 88 |
# File 'lib/html2rss/request_service/response.rb', line 84 def feed_response? return @feed_response if defined?(@feed_response) @feed_response = feed_content_type? || (!json_response? && !html_looking_body? && feed_looking_body?) end |
#html_response? ⇒ Boolean
Returns whether response content is HTML (header or sniffed body, never JSON).
79 80 81 |
# File 'lib/html2rss/request_service/response.rb', line 79 def html_response? content_type.include?('text/html') || (!json_response? && !feed_response? && html_looking_body?) end |
#json_response? ⇒ Boolean
Returns whether response content is JSON.
76 |
# File 'lib/html2rss/request_service/response.rb', line 76 def json_response? = content_type.include?('application/json') |
#parsed_body ⇒ Nokogiri::HTML::Document, Hash
Returns the parsed body of the response, frozen object.
93 94 95 96 97 98 99 100 101 |
# File 'lib/html2rss/request_service/response.rb', line 93 def parsed_body @parsed_body ||= if html_response? parse_html_document elsif json_response? JSON.parse(body, symbolize_names: true).freeze else raise UnsupportedResponseContentType, "Unsupported content type: #{content_type}" end end |