Class: Alacrity::Source
- Inherits:
-
Object
- Object
- Alacrity::Source
- Defined in:
- lib/alacrity.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
Returns the value of attribute document.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#structured_data ⇒ Object
Returns the value of attribute structured_data.
Instance Method Summary collapse
- #fetch(key_name, arguments) ⇒ Object
-
#initialize(source_url, &block) ⇒ Source
constructor
A new instance of Source.
Constructor Details
#initialize(source_url, &block) ⇒ Source
Returns a new instance of Source.
7 8 9 10 11 12 |
# File 'lib/alacrity.rb', line 7 def initialize source_url, &block @structured_data = {} @errors=[] @document = Nokogiri::HTML(open(source_url)) (block.arity < 1 ? (instance_eval &block) : block.call(self)) if block_given? end |
Instance Attribute Details
#document ⇒ Object
Returns the value of attribute document.
6 7 8 |
# File 'lib/alacrity.rb', line 6 def document @document end |
#errors ⇒ Object
Returns the value of attribute errors.
6 7 8 |
# File 'lib/alacrity.rb', line 6 def errors @errors end |
#structured_data ⇒ Object
Returns the value of attribute structured_data.
6 7 8 |
# File 'lib/alacrity.rb', line 6 def structured_data @structured_data end |
Instance Method Details
#fetch(key_name, arguments) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/alacrity.rb', line 14 def fetch key_name, arguments begin page_elements = @document.css arguments[:lookup] raise HMTLDOMElementsNotFound, "While parsing the page to find ('#{key_name}') we couldn't find any elements" if page_elements.empty? @structured_data[key_name] = {} if arguments[:post_fetch] page_elements.each_with_index do |elem,index| @structured_data[key_name][index] = arguments[:post_fetch].call(elem) end else page_elements.each_with_index do |elem,index| @structured_data[key_name][index] = elem.text end end rescue HMTLDOMElementsNotFound => e @structured_data[key_name] = {} @errors << "#{e.to_s}" end end |