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.
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
The raw body of the response.
-
#headers ⇒ Hash<String, Object>
readonly
The headers of the response.
-
#status ⇒ Integer?
readonly
The HTTP status code when known.
-
#url ⇒ Html2rss::Url
readonly
The URL of the response.
Instance Method Summary collapse
- #content_type ⇒ Object
- #html_response? ⇒ Boolean
-
#initialize(body:, url:, headers: {}, status: nil) ⇒ Response
constructor
A new instance of Response.
- #json_response? ⇒ Boolean
-
#parsed_body ⇒ Nokogiri::HTML::Document, Hash
The parsed body of the response, frozen object.
Constructor Details
#initialize(body:, url:, headers: {}, status: nil) ⇒ Response
Returns a new instance of Response.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/html2rss/request_service/response.rb', line 15 def initialize(body:, url:, headers: {}, status: nil) @body = body headers = headers.dup headers.transform_keys!(&:to_s) @headers = headers @status = status @url = url end |
Instance Attribute Details
#body ⇒ String (readonly)
Returns the raw body of the response.
27 28 29 |
# File 'lib/html2rss/request_service/response.rb', line 27 def body @body end |
#headers ⇒ Hash<String, Object> (readonly)
Returns the headers of the response.
30 31 32 |
# File 'lib/html2rss/request_service/response.rb', line 30 def headers @headers end |
#status ⇒ Integer? (readonly)
Returns the HTTP status code when known.
33 34 35 |
# File 'lib/html2rss/request_service/response.rb', line 33 def status @status end |
#url ⇒ Html2rss::Url (readonly)
Returns the URL of the response.
36 37 38 |
# File 'lib/html2rss/request_service/response.rb', line 36 def url @url end |
Instance Method Details
#content_type ⇒ Object
38 |
# File 'lib/html2rss/request_service/response.rb', line 38 def content_type = header('content-type').to_s |
#html_response? ⇒ Boolean
40 |
# File 'lib/html2rss/request_service/response.rb', line 40 def html_response? = content_type.include?('text/html') |
#json_response? ⇒ Boolean
39 |
# File 'lib/html2rss/request_service/response.rb', line 39 def json_response? = content_type.include?('application/json') |
#parsed_body ⇒ Nokogiri::HTML::Document, Hash
Returns the parsed body of the response, frozen object.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/html2rss/request_service/response.rb', line 45 def parsed_body @parsed_body ||= if html_response? Nokogiri::HTML(body).tap do |doc| # Remove comments from the document to avoid processing irrelevant content doc.xpath('//comment()').each(&:remove) end.freeze elsif json_response? JSON.parse(body, symbolize_names: true).freeze else raise UnsupportedResponseContentType, "Unsupported content type: #{content_type}" end end |