Class: Earl::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/earl/scraper.rb

Overview

Base class for nokogiri page scraping

Constant Summary collapse

@@registry =
[]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, earl_source = nil) ⇒ Scraper

Returns a new instance of Scraper.



34
35
36
37
# File 'lib/earl/scraper.rb', line 34

def initialize(url, earl_source = nil)
  @url = url
  @earl_source = earl_source
end

Class Attribute Details

.attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/earl/scraper.rb', line 7

def attributes
  @attributes
end

.regexpObject (readonly)

Returns the value of attribute regexp.



7
8
9
# File 'lib/earl/scraper.rb', line 7

def regexp
  @regexp
end

Instance Attribute Details

#earl_sourceObject (readonly)

Returns the value of attribute earl_source.



32
33
34
# File 'lib/earl/scraper.rb', line 32

def earl_source
  @earl_source
end

Class Method Details

.define_attribute(name, &block) ⇒ Object



14
15
16
17
# File 'lib/earl/scraper.rb', line 14

def define_attribute(name, &block)
  @attributes ||= {}
  @attributes[name] = block
end

.for(url, earl_source) ⇒ Object



19
20
21
22
23
24
# File 'lib/earl/scraper.rb', line 19

def for(url, earl_source)
  @@registry.each do |klass|
    return klass.new(url, earl_source) if klass.regexp.match(url)
  end
  Earl::Scraper.new(url, earl_source)
end

.match(regexp) ⇒ Object



9
10
11
12
# File 'lib/earl/scraper.rb', line 9

def match(regexp)
  @regexp = regexp
  register self
end

.register(scraper_klass) ⇒ Object



26
27
28
# File 'lib/earl/scraper.rb', line 26

def register(scraper_klass)
  @@registry << scraper_klass
end

Instance Method Details

#attribute(name) ⇒ Object



43
44
45
46
47
# File 'lib/earl/scraper.rb', line 43

def attribute(name)
  return unless attribute?(name)

  attributes[name].call(response)
end

#attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/earl/scraper.rb', line 57

def attribute?(name)
  return false unless self.class.attributes

  attributes.key?(name)
end

#attributesObject



49
50
51
52
53
54
55
# File 'lib/earl/scraper.rb', line 49

def attributes
  if self.class.superclass == Earl::Scraper
    self.class.superclass.attributes.merge(self.class.attributes)
  else
    self.class.attributes
  end
end

#responseObject



39
40
41
# File 'lib/earl/scraper.rb', line 39

def response
  @response ||= earl_source && Nokogiri::HTML(earl_source.uri_response)
end