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:, headers:, body:) ⇒ Page

Returns a new instance of Page.



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

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

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



9
10
11
# File 'lib/wayfarer/page.rb', line 9

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



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

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"].first
  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



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

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