Class: Botz::Definition
- Inherits:
-
Object
- Object
- Botz::Definition
- Defined in:
- lib/botz/definition.rb
Overview
Class representing a website defined by DSL
Class Method Summary collapse
- .scraper(name, encoding: nil, as: :html, &block) ⇒ Object
- .spider(name, start_url = nil, encoding: nil, as: :html, &block) ⇒ Object
Instance Method Summary collapse
Class Method Details
.scraper(name, encoding: nil, as: :html, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/botz/definition.rb', line 18 def scraper(name, encoding: nil, as: :html, &block) class_name = "#{name}_scraper".classify accessor_class = Botz::ResourceAccessor.const_get(as.to_s.classify) accessor = accessor_class.new(encoding: encoding) binder_base = Botz::Scraper.const_get(as.to_s.classify) binder = Class.new(binder_base, &block) binder.define_singleton_method(:name) { class_name } crawler_class = self scraper_class = Class.new do define_singleton_method(:crawler_class) { crawler_class } define_singleton_method(:bind) do |url| accessor.call(url) do |resource| binder.new(scraper_class, resource) end end define_singleton_method(:call) { |url, &output| bind(url).call(&output) } end const_set(class_name, scraper_class) scrapers[name] = scraper_class end |
.spider(name, start_url = nil, encoding: nil, as: :html, &block) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/botz/definition.rb', line 39 def spider(name, start_url = nil, encoding: nil, as: :html, &block) accessor_class = Botz::ResourceAccessor.const_get(as.to_s.classify) accessor = accessor_class.new(start_url: start_url, encoding: encoding) spider = Botz::Spider.new(&block) spider_class = Class.new do define_singleton_method(:accessor) { accessor } define_singleton_method(:call) do |url = start_url, &spider_block| accessor.call(url) do |resource| spider.call(resource, &spider_block) end end end const_set("#{name}_spider".classify, spider_class) spiders[name] = spider_class end |
Instance Method Details
#output(&block) ⇒ Object
12 13 14 |
# File 'lib/botz/definition.rb', line 12 def output(&block) self.output = block end |