lib/scrapify/base.rb in scrapify-0.0.7 vs lib/scrapify/base.rb in scrapify-0.0.8
- old
+ new
@@ -67,11 +67,13 @@
self.attribute_names ||= []
self.attribute_names << name
end
def parse_html
- Nokogiri::HTML(html_content)
+ doc = Nokogiri::HTML(html_content)
+ doc.css('br').each {|br| br.replace("\n")}
+ doc
end
def html_content
http_response.body
end
@@ -103,9 +105,18 @@
define_singleton_method :find_by_index do |index|
return if index.nil? or index < 0
attributes = Hash[attribute_names.map {|attribute| [attribute, send("#{attribute}_values")[index]]}]
self.new(attributes)
+ end
+
+ define_singleton_method :where do |conditions = {}|
+ raise Scrapify::AttributeDoesNotExist.new(conditions.keys - attribute_names) unless conditions.keys.all?{|key| attribute_names.include?(key) }
+ indices = conditions.collect do |attribute, value|
+ send("#{attribute}_values").each_with_index.find_all{|attr_val, index| attr_val == value}.collect(&:last)
+ end
+ common_indices = indices.reduce {|a, b| a & b}
+ common_indices.collect{|index| find_by_index(index)}
end
end
def define_count(key_attribute)
define_singleton_method :count do