Skip to content

Instantly share code, notes, and snippets.

@cheald
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheald/1c2b81bf74a3953580e3 to your computer and use it in GitHub Desktop.
Save cheald/1c2b81bf74a3953580e3 to your computer and use it in GitHub Desktop.
module Kerrigan
module Util
class Blacklist
def initialize(values)
update values
end
def match?(url)
p [url, url.class, String === url]
case url
when Array
p :array
url.any? {|u| match? u }
when String
p :string
url.match(@re).present?
when URI
p :uri
match? url.to_s
else
p [:missing, url.class]
false
end
end
def refresh!(key)
update self.class.matches_for(key)
end
def update(values)
@re = Regexp.union values
end
class << self
def from_key(key)
new matches_for(key)
end
def matches_for(key)
db_matches(key) | config_matches(key)
end
def db_matches(key)
doc = MongoMapper.database["config"].find_one(_id: key)
return [] if doc.nil?
doc.fetch("string", []) + doc.fetch("regex", []).map {|v| Regexp.new v }
end
def config_matches(key)
Kerrigan.config.value(key.to_s, []) + Kerrigan.config.value("#{key}_re", []).map {|v| Regexp.new v }
end
end
end
end
end
> u = Kerrigan::Util::Blacklist.new ["www.amazon.com"]
=> #<Kerrigan::Util::Blacklist:0x5809fa26 @re=/www\.amazon\.com/>
> s = "http://www.amazon.com/kids-hobbies-coins-models/b/ref=sv_t_7/175-7766400-4073802"
=> "http://www.amazon.com/kids-hobbies-coins-models/b/ref=sv_t_7/175-7766400-4073802"
> u.match? s
["http://www.amazon.com/kids-hobbies-coins-models/b/ref=sv_t_7/175-7766400-4073802", String, false]
[:missing, String]
=> false
> String === s
=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment