Class: Wayfarer::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/wayfarer/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyString (readonly)

Returns the body of the response.

Returns:

  • (String)

    the body of the response



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wayfarer/page.rb', line 13

class Page
  attr_reader :url,
              :status_code,
              :body,
              :headers

  # @!visibility private
  def initialize(url:, status_code:, body:, headers:)
    @url = url
    @status_code = status_code
    @body = body
    @headers = headers.transform_keys(&:downcase)
  end

  # Returns the MIME type of the response.
  # @return [MIME::Type]
  # @see https://www.rubydoc.info/gems/mime-types/MIME/Type
  def mime_type
    @mime_type ||= MIME::Types[content_type]&.first
  end

  # Returns a parsed representation of the HTTP response or the browser DOM,
  # depending on the Content-Type.
  # @return [Nokogiri::HTML::Document] when Content-Type is `text/html`
  # @see https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/HTML/Document Nokogiri::HTML::Document
  # @return [Nokogiri::XML::Document] when Content-Type is `text/xml`
  # @see https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Document Nokogiri::XML::Document
  # @return [Hash] when Content-Type is `application/json`
  # @note You can register custom parsers with {Wayfarer::Parsing.registry}.
  def doc
    @doc ||= Wayfarer::Parsing.parse(body, mime_type&.content_type || content_type)
  end

  # Returns a `MetaInspector::Document`.
  # @return [MetaInspector::Document]
  # @see https://www.rubydoc.info/gems/metainspector/MetaInspector/Document
  def meta
    @meta ||= MetaInspector.new(url, document: body, headers: headers, normalize_url: false)
  end

private

  def content_type
    @content_type ||= headers["content-type"]
  end
end

#headersHash (readonly)

Note:

HTTP header keys are downcased, for example: content-type.

Returns the headers of the response.

Returns:

  • (Hash)

    the headers of the response



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wayfarer/page.rb', line 13

class Page
  attr_reader :url,
              :status_code,
              :body,
              :headers

  # @!visibility private
  def initialize(url:, status_code:, body:, headers:)
    @url = url
    @status_code = status_code
    @body = body
    @headers = headers.transform_keys(&:downcase)
  end

  # Returns the MIME type of the response.
  # @return [MIME::Type]
  # @see https://www.rubydoc.info/gems/mime-types/MIME/Type
  def mime_type
    @mime_type ||= MIME::Types[content_type]&.first
  end

  # Returns a parsed representation of the HTTP response or the browser DOM,
  # depending on the Content-Type.
  # @return [Nokogiri::HTML::Document] when Content-Type is `text/html`
  # @see https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/HTML/Document Nokogiri::HTML::Document
  # @return [Nokogiri::XML::Document] when Content-Type is `text/xml`
  # @see https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Document Nokogiri::XML::Document
  # @return [Hash] when Content-Type is `application/json`
  # @note You can register custom parsers with {Wayfarer::Parsing.registry}.
  def doc
    @doc ||= Wayfarer::Parsing.parse(body, mime_type&.content_type || content_type)
  end

  # Returns a `MetaInspector::Document`.
  # @return [MetaInspector::Document]
  # @see https://www.rubydoc.info/gems/metainspector/MetaInspector/Document
  def meta
    @meta ||= MetaInspector.new(url, document: body, headers: headers, normalize_url: false)
  end

private

  def content_type
    @content_type ||= headers["content-type"]
  end
end

#status_codeFixnum (readonly)

Returns HTTP status code.

Returns:

  • (Fixnum)

    HTTP status code



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wayfarer/page.rb', line 13

class Page
  attr_reader :url,
              :status_code,
              :body,
              :headers

  # @!visibility private
  def initialize(url:, status_code:, body:, headers:)
    @url = url
    @status_code = status_code
    @body = body
    @headers = headers.transform_keys(&:downcase)
  end

  # Returns the MIME type of the response.
  # @return [MIME::Type]
  # @see https://www.rubydoc.info/gems/mime-types/MIME/Type
  def mime_type
    @mime_type ||= MIME::Types[content_type]&.first
  end

  # Returns a parsed representation of the HTTP response or the browser DOM,
  # depending on the Content-Type.
  # @return [Nokogiri::HTML::Document] when Content-Type is `text/html`
  # @see https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/HTML/Document Nokogiri::HTML::Document
  # @return [Nokogiri::XML::Document] when Content-Type is `text/xml`
  # @see https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Document Nokogiri::XML::Document
  # @return [Hash] when Content-Type is `application/json`
  # @note You can register custom parsers with {Wayfarer::Parsing.registry}.
  def doc
    @doc ||= Wayfarer::Parsing.parse(body, mime_type&.content_type || content_type)
  end

  # Returns a `MetaInspector::Document`.
  # @return [MetaInspector::Document]
  # @see https://www.rubydoc.info/gems/metainspector/MetaInspector/Document
  def meta
    @meta ||= MetaInspector.new(url, document: body, headers: headers, normalize_url: false)
  end

private

  def content_type
    @content_type ||= headers["content-type"]
  end
end

#urlString (readonly)

Returns the URL that was fetched.

Returns:

  • (String)

    the URL that was fetched



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wayfarer/page.rb', line 13

class Page
  attr_reader :url,
              :status_code,
              :body,
              :headers

  # @!visibility private
  def initialize(url:, status_code:, body:, headers:)
    @url = url
    @status_code = status_code
    @body = body
    @headers = headers.transform_keys(&:downcase)
  end

  # Returns the MIME type of the response.
  # @return [MIME::Type]
  # @see https://www.rubydoc.info/gems/mime-types/MIME/Type
  def mime_type
    @mime_type ||= MIME::Types[content_type]&.first
  end

  # Returns a parsed representation of the HTTP response or the browser DOM,
  # depending on the Content-Type.
  # @return [Nokogiri::HTML::Document] when Content-Type is `text/html`
  # @see https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/HTML/Document Nokogiri::HTML::Document
  # @return [Nokogiri::XML::Document] when Content-Type is `text/xml`
  # @see https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Document Nokogiri::XML::Document
  # @return [Hash] when Content-Type is `application/json`
  # @note You can register custom parsers with {Wayfarer::Parsing.registry}.
  def doc
    @doc ||= Wayfarer::Parsing.parse(body, mime_type&.content_type || content_type)
  end

  # Returns a `MetaInspector::Document`.
  # @return [MetaInspector::Document]
  # @see https://www.rubydoc.info/gems/metainspector/MetaInspector/Document
  def meta
    @meta ||= MetaInspector.new(url, document: body, headers: headers, normalize_url: false)
  end

private

  def content_type
    @content_type ||= headers["content-type"]
  end
end

Instance Method Details

#docNokogiri::HTML::Document, ...

Note:

You can register custom parsers with Wayfarer::Parsing.registry.

Returns a parsed representation of the HTTP response or the browser DOM, depending on the Content-Type.

Returns:

  • (Nokogiri::HTML::Document)

    when Content-Type is text/html

  • (Nokogiri::XML::Document)

    when Content-Type is text/xml

  • (Hash)

    when Content-Type is application/json

See Also:



42
43
44
# File 'lib/wayfarer/page.rb', line 42

def doc
  @doc ||= Wayfarer::Parsing.parse(body, mime_type&.content_type || content_type)
end

#metaMetaInspector::Document

Returns a MetaInspector::Document.

Returns:

  • (MetaInspector::Document)

See Also:



49
50
51
# File 'lib/wayfarer/page.rb', line 49

def meta
  @meta ||= MetaInspector.new(url, document: body, headers: headers, normalize_url: false)
end

#mime_typeMIME::Type

Returns the MIME type of the response.

Returns:

  • (MIME::Type)

See Also:



30
31
32
# File 'lib/wayfarer/page.rb', line 30

def mime_type
  @mime_type ||= MIME::Types[content_type]&.first
end