Class: Scrape::Match

Inherits:
Object
  • Object
show all
Defined in:
lib/scrape/match.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher, &proc) ⇒ Match

Returns a new instance of Match.

Raises:

  • (ArgumentError)


4
5
6
7
# File 'lib/scrape/match.rb', line 4

def initialize matcher, &proc
  @matcher, @proc = matcher, proc
  raise ArgumentError, "Not enough arguments in block" if proc.arity == 0
end

Instance Attribute Details

#matcherObject (readonly)

Returns the value of attribute matcher.



2
3
4
# File 'lib/scrape/match.rb', line 2

def matcher
  @matcher
end

Instance Method Details

#=~(url) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/scrape/match.rb', line 14

def =~ url
  case @matcher
  when String
    url.to_s.include? @matcher
  when Regexp
    url.to_s =~ @matcher
  when Proc
    @matcher.call url
  end
end

#invoke(*args) ⇒ Object



9
10
11
12
# File 'lib/scrape/match.rb', line 9

def invoke *args
  args = args[0, @proc.arity] unless @proc.arity == -1
  @proc.call *args
end