Class: Digger::Pattern

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

Constant Summary collapse

MATCH_MAX =
3
TYPES =
0.upto(MATCH_MAX).map{|i| "match_#{i}"} + %w{match_many css_one css_many}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Pattern

Returns a new instance of Pattern.



7
8
9
# File 'lib/digger/pattern.rb', line 7

def initialize(hash = {})
  hash.each_pair{|key, value| send("#{key}=", value) if %w{type value block}.include?(key.to_s)}
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



5
6
7
# File 'lib/digger/pattern.rb', line 5

def block
  @block
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/digger/pattern.rb', line 5

def type
  @type
end

#valueObject

Returns the value of attribute value.



5
6
7
# File 'lib/digger/pattern.rb', line 5

def value
  @value
end

Class Method Details

.wrap(hash) ⇒ Object



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

def self.wrap(hash)
  Hash[hash.map{|key, value| [key, value.is_a?(Pattern) ? value : Pattern.new(value)]}]
end

Instance Method Details

#match_page(page, &callback) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/digger/pattern.rb', line 37

def match_page(page, &callback)
  blk = callback || safe_block
  if regexp? # regular expression
    index = TYPES.index(type)
    blk ||= ->(text){text.strip}
    # content is String
    if type == 'match_many'
      match = page.body.gsub(value).to_a
    else
      matches = page.body.match(value)
      match = matches.nil? ? nil : matches[index]
    end
  else # css expression
    blk ||= ->(node){node.content.strip}
    # content is Nokogiri::HTML::Document
    if type == 'css_one'
      match = page.doc.css(value).first
    elsif type == 'css_many' # css_many
      match = page.doc.css(value)
    end
  end
  if match.nil?
    nil
  elsif %w{css_many match_many}.include? type
    match.map{|node| blk.call(node) }.uniq
  else
    blk.call(match)
  end
rescue
  nil
end

#regexp?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/digger/pattern.rb', line 33

def regexp?
  TYPES.index(type) <= MATCH_MAX + 1 # match_many in addition
end

#safe_blockObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/digger/pattern.rb', line 11

def safe_block
  block && begin
    if block.respond_to?(:call)
      block
    elsif block.strip == '' # 
      nil
    else
      proc{ $SAFE = 2; eval block }.call
    end
  rescue StandardError
    nil
  end
end