Class: Rextract::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rextract/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, *opts) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rextract/parser.rb', line 7

def initialize(content, *opts)
  @opts = opts
  case content
  when content.respond_to?(:parser) && content.respond_to?(:html_body)
    @doc     = content.parser
    @content = content.html_body
  when content.respond_to?(:body)
    @doc     = content
    @content = content.body
  else
    @doc     = Nokogiri::HTML(content.to_s)
    @content = content.to_s
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



5
6
7
# File 'lib/rextract/parser.rb', line 5

def content
  @content
end

#docObject (readonly)

Returns the value of attribute doc.



5
6
7
# File 'lib/rextract/parser.rb', line 5

def doc
  @doc
end

#optsObject (readonly)

Returns the value of attribute opts.



5
6
7
# File 'lib/rextract/parser.rb', line 5

def opts
  @opts
end

Instance Method Details

#extract(regex) ⇒ Object



22
23
24
# File 'lib/rextract/parser.rb', line 22

def extract(regex)
  @content.scan(regex).flatten.first
end

#parseObject



30
31
32
33
34
35
36
37
# File 'lib/rextract/parser.rb', line 30

def parse
  result = {}
  parsing_methods.each do |method|
    key = method.to_s.scan(/parse_(\w*)/).flatten.first.to_sym
    result[key] = self.send(method.to_sym)
  end
  result
end

#parsing_methodsObject



26
27
28
# File 'lib/rextract/parser.rb', line 26

def parsing_methods
  (self.methods - self.class.superclass.methods).collect{|m| m if m =~ /parse_/}.compact
end