Module: Scrapify::Base::ClassMethods
- Defined in:
- lib/scrapify/base.rb
Instance Method Summary collapse
- #attribute(name, options = {}, &block) ⇒ Object
- #html(url) ⇒ Object
- #http_cache_header ⇒ Object
- #key(attribute) ⇒ Object
Instance Method Details
#attribute(name, options = {}, &block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/scrapify/base.rb', line 28 def attribute(name, ={}, &block) add_attribute(name) = .symbolize_keys parser = [:xpath] ? :xpath : :css selector = [parser] matcher = /#{[:regex]}/ if [:regex] to_array = [:array] define_singleton_method "#{name}_values" do self.doc ||= parse_html self.doc.send(parser, selector).map do |element| if block yield element else content = element.content if matcher match_data = content.scan(matcher).map &:first [:array] ? match_data : match_data.first else content.strip end end end end end |
#html(url) ⇒ Object
23 24 25 26 |
# File 'lib/scrapify/base.rb', line 23 def html(url) self.url = url define_finders end |
#http_cache_header ⇒ Object
58 59 60 61 62 |
# File 'lib/scrapify/base.rb', line 58 def http_cache_header http_header.select do |(k, v)| HTTP_CACHE_HEADERS_TO_RETURN.map(&:upcase).include?(k.upcase) end end |
#key(attribute) ⇒ Object
53 54 55 56 |
# File 'lib/scrapify/base.rb', line 53 def key(attribute) define_find_by_id attribute define_count attribute end |