Module: NibblerMethods

Included in:
Nibbler, NibblerJSON
Defined in:
lib/nibbler.rb

Overview

DSL for defining data extraction rules from an abstract document object

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



3
4
5
# File 'lib/nibbler.rb', line 3

def self.extended(base)
  base.send(:include, InstanceMethods) if base.is_a? Class
end

Instance Method Details

#element(*args, &block) ⇒ Object

Declare a singular scraping rule



8
9
10
11
12
13
# File 'lib/nibbler.rb', line 8

def element(*args, &block)
  selector, name, delegate = parse_rule_declaration(*args, &block)
  rules[name] = [selector, delegate]
  attr_accessor name
  name
end

#elements(*args, &block) ⇒ Object

Declare a plural scraping rule



16
17
18
19
# File 'lib/nibbler.rb', line 16

def elements(*args, &block)
  name = element(*args, &block)
  rules[name] << true
end

#parse(doc) ⇒ Object

Process data by creating a new instance



27
# File 'lib/nibbler.rb', line 27

def parse(doc) new(doc).parse end

#rulesObject

Parsing rules declared with ‘element` or `elements`



22
23
24
# File 'lib/nibbler.rb', line 22

def rules
  @rules ||= {}
end