Class: WebGrep::Grep

Inherits:
Object
  • Object
show all
Defined in:
lib/web_grep/grep.rb

Instance Method Summary collapse

Constructor Details

#initialize(word:, url:, file:, quite:) ⇒ Grep

Returns a new instance of Grep.



6
7
8
9
10
11
# File 'lib/web_grep/grep.rb', line 6

def initialize(word:,url:,file:,quite:)
  raise 'Should set one of params, url or file!' if file && url
  url = "http://#{url}" if url && !url.match('http[s]{0,1}://')

  @word, @url, @file, @quite = word, url, file, quite
end

Instance Method Details

#grep!Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/web_grep/grep.rb', line 13

def grep!
  Nokogiri::XML(open(@url || @file)).
    xpath ".//text()[regex(., '#{@word}')]", Class.new {
      def regex(node_set, regex)
        node_set.find_all { |node| node.content.match regex }
      end
    }.new
rescue SocketError
  raise 'Bad url or connection!'
end