Class: Earl::Scraper

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

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.



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

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

Class Attribute Details

.attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

.regexpObject (readonly)

Returns the value of attribute regexp.



5
6
7
# File 'lib/earl/scraper.rb', line 5

def regexp
  @regexp
end

Instance Attribute Details

#earl_sourceObject (readonly)

Returns the value of attribute earl_source.



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

def earl_source
  @earl_source
end

Class Method Details

.define_attribute(name, &block) ⇒ Object



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

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

.for(url, earl_source) ⇒ Object



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

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

.match(regexp) ⇒ Object



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

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

Instance Method Details

#attribute(name) ⇒ Object



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

def attribute(name)
  return unless has_attribute?(name)
  self.attributes[name].call(response)
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

#has_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_attribute?(name)
  return false unless self.class.attributes
  self.attributes.has_key?(name)
end

#responseObject



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

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