Class: Wayfarer::Page
- Inherits:
-
Object
- Object
- Wayfarer::Page
- Defined in:
- lib/wayfarer/page.rb
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
The body of the response.
-
#headers ⇒ Hash
readonly
The headers of the response.
-
#status_code ⇒ Fixnum
readonly
HTTP status code.
-
#url ⇒ String
readonly
The URL that was fetched.
Instance Method Summary collapse
-
#doc ⇒ Nokogiri::HTML::Document, ...
Returns a parsed representation of the HTTP response or the browser DOM, depending on the Content-Type.
-
#meta ⇒ MetaInspector::Document
Returns a
MetaInspector::Document
. -
#mime_type ⇒ MIME::Type
Returns the MIME type of the response.
Instance Attribute Details
#body ⇒ String (readonly)
Returns 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 ||= MetaInspector.new(url, document: body, headers: headers, normalize_url: false) end private def content_type @content_type ||= headers["content-type"] end end |
#headers ⇒ Hash (readonly)
Note:
HTTP header keys are downcased, for example: content-type
.
Returns 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 ||= MetaInspector.new(url, document: body, headers: headers, normalize_url: false) end private def content_type @content_type ||= headers["content-type"] end end |
#status_code ⇒ Fixnum (readonly)
Returns 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 ||= MetaInspector.new(url, document: body, headers: headers, normalize_url: false) end private def content_type @content_type ||= headers["content-type"] end end |
#url ⇒ String (readonly)
Returns 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 ||= 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
#doc ⇒ Nokogiri::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.
42 43 44 |
# File 'lib/wayfarer/page.rb', line 42 def doc @doc ||= Wayfarer::Parsing.parse(body, mime_type&.content_type || content_type) end |
#meta ⇒ MetaInspector::Document
Returns a MetaInspector::Document
.
49 50 51 |
# File 'lib/wayfarer/page.rb', line 49 def @meta ||= MetaInspector.new(url, document: body, headers: headers, normalize_url: false) end |
#mime_type ⇒ MIME::Type
Returns the MIME type of the response.
30 31 32 |
# File 'lib/wayfarer/page.rb', line 30 def mime_type @mime_type ||= MIME::Types[content_type]&.first end |