Class: Wayfarer::Page

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, status_code:, body:, headers:) ⇒ Page

Returns a new instance of Page.



10
11
12
13
14
15
# File 'lib/wayfarer/page.rb', line 10

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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/wayfarer/page.rb', line 5

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/wayfarer/page.rb', line 5

def headers
  @headers
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



5
6
7
# File 'lib/wayfarer/page.rb', line 5

def status_code
  @status_code
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/wayfarer/page.rb', line 5

def url
  @url
end

Instance Method Details

#docObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wayfarer/page.rb', line 17

def doc
  return @doc if @doc

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

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

  @doc = case sub_type
         when "json" then Wayfarer::Parsing::JSON.parse(body)
         when "xml" then Wayfarer::Parsing::XML.parse_xml(body)
         else Wayfarer::Parsing::XML.parse_html(body)
         end
end

#metaObject



33
34
35
# File 'lib/wayfarer/page.rb', line 33

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