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.



8
9
10
11
12
13
14
15
16
# File 'lib/web_grep/grep.rb', line 8

def initialize(word:,url:,file:,quite:)
  if file && url
    raise 'Should set one of params, url or file!'
  end
  if url && !url.match('http://|https://')
    url = "http://#{url}"
  end
  @word, @url, @file, @quite = word, url, file, quite
end

Instance Method Details

#grep!Object



18
19
20
21
22
23
24
25
26
# File 'lib/web_grep/grep.rb', line 18

def grep!
  Nokogiri::XML(open(@url || @file)).search('[text()*=""]').select do |e|
    e.content
      .encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
      .match /#{@word}/
  end
rescue SocketError
  raise 'Bad url or connection!'
end