Class: Digger::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/digger/model.rb

Constant Summary collapse

@@digger_config =
{'pattern'=>{}, 'index'=>{}}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.index_configObject

index page



21
22
23
# File 'lib/digger/model.rb', line 21

def index_config
  @@digger_config['index'][self.name]
end

.index_page(pattern, *args) ⇒ Object



25
26
27
# File 'lib/digger/model.rb', line 25

def index_page(pattern, *args)
   @@digger_config['index'][self.name] = Index.new(pattern, args)
end

.index_page?Boolean

Returns:



29
30
31
# File 'lib/digger/model.rb', line 29

def index_page?
  !index_config.nil?
end

.pattern_configObject

patterns



8
9
10
# File 'lib/digger/model.rb', line 8

def pattern_config
  @@digger_config['pattern'][self.name] ||= {}
end

Instance Method Details

#dig(cocurrence = 1) ⇒ Object



52
53
54
55
56
# File 'lib/digger/model.rb', line 52

def dig(cocurrence = 1)
  if self.class.index_page?
    self.class.index_config.process(cocurrence){|url| dig_url(url) }
  end
end

#dig_url(url, opts = {}) ⇒ Object



42
43
44
45
46
# File 'lib/digger/model.rb', line 42

def dig_url(url, opts = {})
  client = Digger::HTTP.new(opts)
  page = client.fetch_page(url)
  match_page(page)
end

#dig_urls(urls, cocurrence = 1, opts = {}) ⇒ Object



48
49
50
# File 'lib/digger/model.rb', line 48

def dig_urls(urls, cocurrence = 1, opts = {})
  Index.batch(urls, cocurrence){|url| dig_url(url, opts) }
end

#match_page(page) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/digger/model.rb', line 34

def match_page(page)
  result = {}
  self.class.pattern_config.each_pair do |key, pattern|
    result[key] = pattern.match_page(page)
  end
  result
end