From 3f93bf1fa260d204225f38adde6f114122ad358b Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Wed, 19 Dec 2012 09:31:17 +0100 Subject: [PATCH] Add configuration class and modify lookup to use this configuration To configure the module, you can use : ProjectHoneypot.configure do @api_key = 'API_KEY' end To access, the variable, use : ProjectHoneypot.api_key The lookup method has been modified to be backward compatible and use the defined api_key if available. --- lib/project-honeypot.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/project-honeypot.rb b/lib/project-honeypot.rb index 7f516f1..7cf376b 100644 --- a/lib/project-honeypot.rb +++ b/lib/project-honeypot.rb @@ -3,8 +3,25 @@ require File.dirname(__FILE__) + "/project_honeypot/url.rb" require File.dirname(__FILE__) + "/project_honeypot/base.rb" module ProjectHoneypot - def self.lookup(api_key, url) - searcher = Base.new(api_key) + class << self + attr_accessor :api_key + + def api_key + raise "ProjectHoneypot really needs its api_key set to work" unless @api_key + @api_key + end + + def configure(&block) + class_eval(&block) + end + end + + def self.lookup(api_key_or_url, url=nil) + if url.nil? + url = api_key_or_url + api_key_or_url = ProjectHoneypot.api_key + end + searcher = Base.new(api_key_or_url) searcher.lookup(url) end end