class Wayfarer::Page

The representation of fetched pages

Attributes

body[RW]

@!attribute [r] body @return [String] the response body.

headers[R]

@!attribute [r] headers @return [Hash] the response headers.

status_code[R]

@!attribute [r] status_code @return [Fixnum] the response status code.

uri[R]

@!attribute [r] uri @return [URI] the URI of the page.

Public Class Methods

new(attrs = {}) click to toggle source
# File lib/wayfarer/page.rb, line 33
def initialize(attrs = {})
  @uri         = attrs[:uri]
  @status_code = attrs[:status_code]
  @body        = attrs[:body]
  @headers     = attrs[:headers]
end

Public Instance Methods

doc() click to toggle source

Returns a parsed representation of the fetched document depending on the Content-Type field. @return [OpenStruct] if the Content-Type field's sub-type is “json”. @return [Nokogiri::XML::Document] if the Content-Type field's sub-type is “xml”. @return [Nokogiri::HTML::Document] otherwise.

# File lib/wayfarer/page.rb, line 45
def doc
  return @doc if @doc

  # If no Content-Type field is present, assume HTML/XML
  # TODO: Test
  unless @headers["content-type"]
    return @doc = Parsers::XMLParser.parse_html(@body)
  end

  content_type = @headers["content-type"].first
  sub_type = MIME::Types[content_type].first.sub_type

  # TODO: Tests
  @doc = case sub_type
         when "json"
           Parsers::JSONParser.parse(@body)
         when "xml"
           Parsers::XMLParser.parse_xml(@body)
         else
           Parsers::XMLParser.parse_html(@body)
         end
end

Private Instance Methods

instantiate_pismo_document() click to toggle source
# File lib/wayfarer/page.rb, line 85
def instantiate_pismo_document
  doc = Pismo::Document.allocate
  doc.instance_variable_set(:@options, {})
  doc.instance_variable_set(:@url, uri)
  doc.instance_variable_set(:@html, body)
  doc.instance_variable_set(:@doc, self.doc)
  doc
end
pismo() click to toggle source

Returns a Pismo document. @note Not available on JRuby. @note Only succeeds when {#doc} returns a `Nokogiri::HTML::Document`. @return [Pismo::Document]

# File lib/wayfarer/page.rb, line 81
def pismo
  @pismo_doc ||= instantiate_pismo_document
end