Created
August 7, 2013 18:45
-
-
Save loganlinn/6177164 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nokogiri' | |
require 'open-uri' | |
require 'colorize' | |
require 'ripl' | |
require 'yaml' | |
class RegexWorkshop | |
def initialize() | |
reset | |
end | |
def reset | |
@ua_dict = {} | |
@ua_test = {} | |
@focus = nil | |
@last_match_regex = nil | |
end | |
def focus() @focus end | |
def focus=(browser) @focus = browser end | |
def verbose() @verbose end | |
def verbose=(v=true) @verbose = v end | |
def ua_dict | |
if focus.nil? | |
@ua_dict | |
else | |
{focus => @ua_dict[focus]} | |
end | |
end | |
def load(file) | |
if /^http:\/\/(www\.)?useragentstring\.com/ =~ file | |
doc = Nokogiri::HTML(open(file)) | |
ua_links = doc.xpath("//div[@id='liste']//*[self::h4 or self::a]") | |
browser = nil | |
ua_links.each do |node| | |
if node.name== 'h4' | |
# start new | |
browser = node.content.strip | |
@ua_dict[browser] = [] | |
elsif browser | |
@ua_dict[browser] << node.content.strip | |
end | |
end | |
elsif File.exists? file | |
data = YAML.load_file(file) | |
data.each do |k, v| | |
@ua_dict[k] = [] if @ua_dict[k].nil? | |
@ua_dict[k].concat v | |
end | |
else | |
$stderr.puts "Cannot load file" | |
end | |
end | |
def dump(file) | |
File.open(file, 'w') do |out| | |
YAML.dump(@ua_dict, out) | |
end | |
end | |
def show(opts={:show_true=>true,:show_false=>true}) | |
browser = opts[:browser] || focus | |
show_true = opts[:show_true] | |
show_false = opts[:show_false] | |
if browser.nil? | |
expand_dict = @ua_test || {} | |
else | |
expand_dict = {browser => @ua_test[browser]} | |
end | |
expand_dict.each do |b, match_data| | |
next if match_data.empty? | |
puts b | |
match_data[true].each do |ua| | |
puts ua.green | |
end if show_true | |
match_data[false].each do |ua| | |
puts ua.red | |
end if show_false | |
puts | |
end | |
nil | |
end | |
def match_last | |
unless @last_match_regex.nil? | |
match @last_match_regex | |
end | |
@last_match_regex | |
end | |
def match(regex, opts={:show_true=>true, :show_false=>true}) | |
@last_match_regex = regex | |
ua_test = {} | |
ua_dict.each do |b, uas| | |
ua_test[b] = {true=>[], false=>[]} | |
uas.each do |ua| | |
ua_test[b][(regex =~ ua).nil?.!] << ua | |
end | |
end | |
if verbose | |
ua_test.each do |b, match_data| | |
match_data[true].each do |ua| | |
puts ua.green | |
end if opts[:show_true] | |
match_data[false].each do |ua| | |
puts ua.red | |
end if opts[:show_false] | |
end | |
else | |
ua_test.each do |b, match_data| | |
num_t = match_data[true].length | |
num_f = match_data[false].length | |
sum = num_t + num_f | |
next if sum.zero? | |
pc = "%d/%d" % [num_t, sum] | |
if num_t.zero? | |
pc = pc.light_blue | |
elsif num_f.zero? | |
pc = pc.green | |
else | |
pc = pc.red | |
end | |
puts "#{b} (#{pc})" | |
end | |
end | |
@ua_test = ua_test | |
nil | |
end | |
end | |
## RUN | |
regex_workshop = RegexWorkshop.new | |
ARGV.each do |url| | |
regex_workshop.load url | |
end | |
Ripl.start :binding => regex_workshop.instance_eval { binding } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment