Class: Rextract::Parser
- Inherits:
-
Object
- Object
- Rextract::Parser
- Defined in:
- lib/rextract/parser.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #extract(regex) ⇒ Object
-
#initialize(content, *opts) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
- #parsing_methods ⇒ Object
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
#content ⇒ Object (readonly)
Returns the value of attribute content.
5 6 7 |
# File 'lib/rextract/parser.rb', line 5 def content @content end |
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
5 6 7 |
# File 'lib/rextract/parser.rb', line 5 def doc @doc end |
#opts ⇒ Object (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 |
#parse ⇒ Object
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_methods ⇒ Object
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 |