Class: Earl::Scraper
- Inherits:
-
Object
- Object
- Earl::Scraper
- Defined in:
- lib/earl/scraper.rb
Overview
Base class for nokogiri page scraping
Constant Summary collapse
- @@registry =
[]
Class Attribute Summary collapse
-
.attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
.regexp ⇒ Object
readonly
Returns the value of attribute regexp.
Instance Attribute Summary collapse
-
#earl_source ⇒ Object
readonly
Returns the value of attribute earl_source.
Class Method Summary collapse
- .define_attribute(name, &block) ⇒ Object
- .for(url, earl_source) ⇒ Object
- .match(regexp) ⇒ Object
- .register(scraper_klass) ⇒ Object
Instance Method Summary collapse
- #attribute(name) ⇒ Object
- #attribute?(name) ⇒ Boolean
- #attributes ⇒ Object
-
#initialize(url, earl_source = nil) ⇒ Scraper
constructor
A new instance of Scraper.
- #response ⇒ Object
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
.attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
7 8 9 |
# File 'lib/earl/scraper.rb', line 7 def attributes @attributes end |
.regexp ⇒ Object (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_source ⇒ Object (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
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 |
#attributes ⇒ Object
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 |
#response ⇒ Object
39 40 41 |
# File 'lib/earl/scraper.rb', line 39 def response @response ||= earl_source && Nokogiri::HTML(earl_source.uri_response) end |