Class: Html2rss::RequestService::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/request_service/response.rb

Overview

To be used by strategies to provide their response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body:, url:, headers: {}, status: nil) ⇒ Response

Returns a new instance of Response.

Parameters:

  • body (String)

    the body of the response

  • url (Html2rss::Url)

    the final request URL

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

    the headers of the response

  • status (Integer, nil) (defaults to: nil)

    the HTTP status code when available



15
16
17
18
19
20
21
22
23
24
25
# 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)
  HashUtil.assert_string_keys!(headers, context: 'response headers', deep: false)

  @headers = headers
  @status = status
  @url = url
end

Instance Attribute Details

#bodyString (readonly)

Returns the raw body of the response.

Returns:

  • (String)

    the raw body of the response



28
29
30
# File 'lib/html2rss/request_service/response.rb', line 28

def body
  @body
end

#headersHash{String => Object} (readonly)

Returns the headers of the response.

Returns:

  • (Hash{String => Object})

    the headers of the response



31
32
33
# File 'lib/html2rss/request_service/response.rb', line 31

def headers
  @headers
end

#statusInteger? (readonly)

Returns the HTTP status code when known.

Returns:

  • (Integer, nil)

    the HTTP status code when known



34
35
36
# File 'lib/html2rss/request_service/response.rb', line 34

def status
  @status
end

#urlHtml2rss::Url (readonly)

Returns the URL of the response.

Returns:



37
38
39
# File 'lib/html2rss/request_service/response.rb', line 37

def url
  @url
end

Instance Method Details

#content_typeString

Returns normalized content type header value.

Returns:

  • (String)

    normalized content type header value



40
# File 'lib/html2rss/request_service/response.rb', line 40

def content_type = header('content-type').to_s

#html_response?Boolean

Returns whether response content is HTML.

Returns:

  • (Boolean)

    whether response content is HTML



46
# File 'lib/html2rss/request_service/response.rb', line 46

def html_response? = content_type.include?('text/html')

#json_response?Boolean

Returns whether response content is JSON.

Returns:

  • (Boolean)

    whether response content is JSON



43
# File 'lib/html2rss/request_service/response.rb', line 43

def json_response? = content_type.include?('application/json')

#parsed_bodyNokogiri::HTML::Document, Hash

Returns the parsed body of the response, frozen object.

Returns:

  • (Nokogiri::HTML::Document, Hash)

    the parsed body of the response, frozen object

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/html2rss/request_service/response.rb', line 51

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