Class: Ogo::PageSource

Inherits:
Object
  • Object
show all
Defined in:
lib/ogo/page_source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, options = {}) ⇒ PageSource

Returns a new instance of PageSource.



6
7
8
9
10
# File 'lib/ogo/page_source.rb', line 6

def initialize(src, options={})
  @src = src
  @url = options[:url]
  @charset = options[:charset]
end

Instance Attribute Details

#charsetObject (readonly)

Returns the value of attribute charset.



4
5
6
# File 'lib/ogo/page_source.rb', line 4

def charset
  @charset
end

#docObject (readonly)

Returns the value of attribute doc.



4
5
6
# File 'lib/ogo/page_source.rb', line 4

def doc
  @doc
end

#srcObject (readonly)

Returns the value of attribute src.



4
5
6
# File 'lib/ogo/page_source.rb', line 4

def src
  @src
end

#urlObject (readonly)

Returns the value of attribute url.



4
5
6
# File 'lib/ogo/page_source.rb', line 4

def url
  @url
end

Instance Method Details

#inspectObject



25
26
27
28
29
30
31
32
# File 'lib/ogo/page_source.rb', line 25

def inspect
  str = "<Ogo::PageSource:0x00#{'%x' % (self.object_id << 1)}\n"
  str << "url=\"#{url}\",\n"
  str << "charset=\"#{charset}\",\n"
  str << "src=\"#{src.to_s.truncate(100, omission: '...')}\",\n"
  str << "doc=#{doc.to_s.truncate(100, omission: '...')}\" >"
  str
end

#parseObject



12
13
14
15
16
17
18
# File 'lib/ogo/page_source.rb', line 12

def parse
  unless charset
    _doc = Nokogiri.parse(src.scrub)
    @charset = guess_encoding(_doc)
  end
  Nokogiri::HTML(src, nil, charset)
end

#parse!Object



20
21
22
23
# File 'lib/ogo/page_source.rb', line 20

def parse!
  @doc = parse
  self
end

#to_sObject



34
35
36
# File 'lib/ogo/page_source.rb', line 34

def to_s
  inspect
end