Sha256: 219ff637a8daf601f91228330f6c3dd0cf5e0c2f8d5aeb3902e396acfbdd9092

Contents?: true

Size: 761 Bytes

Versions: 3

Compression:

Stored size: 761 Bytes

Contents

require 'nokogiri'
require 'open-uri'

module WebGrep
  class Grep
    def initialize(word:,url:,file:)
      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 = word, url, file
    end

    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.map do |l|
        "#{"\033[32;1m"}XPath: #{l.path}#{"\033[0m"}\n" \
        "\tMatched content: #{l.content}"
      end
    rescue SocketError
      raise 'Bad url or connection!'
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
web_grep-1.0.2 lib/web_grep/grep.rb
web_grep-1.0.1 lib/web_grep/grep.rb
web_grep-1.0.0 lib/web_grep/grep.rb