class WebGrep::Grep

Public Class Methods

new(word:,url:,file:,quite:) click to toggle source
# 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

Public Instance Methods

grep!() click to toggle source
# 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
regex(node_set, regex) click to toggle source
# File lib/web_grep/grep.rb, line 16
def regex(node_set, regex)
  node_set.find_all { |node| node.content.match regex }
end