Module: Scrapify::Base::ClassMethods

Defined in:
lib/scrapify/base.rb

Instance Method Summary collapse

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
# File 'lib/scrapify/base.rb', line 28

def attribute(name, options={}, &block)
  add_attribute(name)
  parser = options[:xpath] ? :xpath : :css
  selector = options[parser]
  matcher = /#{options[:regex]}/ if options[:regex]
  to_array = options[:array]
  meta_define "#{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
          options[: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_headerObject



57
58
59
60
61
# File 'lib/scrapify/base.rb', line 57

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



52
53
54
55
# File 'lib/scrapify/base.rb', line 52

def key(attribute)
  define_find_by_id attribute
  define_count attribute
end