lib/yogi_berra/catcher.rb in yogi_berra-0.0.1 vs lib/yogi_berra/catcher.rb in yogi_berra-0.0.2
- old
+ new
@@ -1,44 +1,59 @@
require 'mongo'
+require 'facets'
module YogiBerra
class Catcher
+ extend Facets
cattr_accessor :settings, :mongo_client, :connection
class << self
- def load_db_settings
- begin
- File.open("#{Rails.root}/config/yogi.yml", 'r') do |f|
- yaml_file = YAML.load(f)
- @@settings = yaml_file["#{Rails.env}"] if yaml_file
+ def load_db_settings(config_file = nil)
+ if config_file
+ database_config = config_file
+ elsif defined?(Rails)
+ database_config = "#{Rails.root}/config/yogi.yml"
+ else
+ YogiBerra::Logger.log("No config file specified!", :error)
+ end
+ if database_config
+ begin
+ File.open(database_config, 'r') do |f|
+ yaml_file = YAML.load(f)
+ environment = ENV["RAILS_ENV"] ? ENV["RAILS_ENV"] : ENV["YOGI_ENV"]
+ YogiBerra::Logger.log("I get here! #{environment.inspect}", :info)
+ @@settings = yaml_file["#{environment}"] if yaml_file
+ end
+ rescue
+ YogiBerra::Logger.log("No such file: #{database_config}", :error)
end
- rescue
- $stderr.puts "[YogiBerra Error] No such file: #{Rails.root}/config/yogi.yml"
end
end
def db_client(host, port)
# :w => 0 set the default write concern to 0, this allows writes to be non-blocking
# by not waiting for a response from mongodb
@@mongo_client = Mongo::MongoClient.new(host, port, :w => 0)
rescue
- Rails.logger.error "[YogiBerra Error] Couldn't connect to the mongo database on host: #{host} port: #{port}."
+ YogiBerra::Logger.log("Couldn't connect to the mongo database on host: #{host} port: #{port}.", :error)
nil
end
def quick_connection
+ YogiBerra::Logger.log("settings::::::::::: #{settings.inspect}", :info)
settings = @@settings || load_db_settings
+
if settings
host = settings["host"]
port = settings["port"]
client = db_client(host, port)
if client
@@connection = client[settings["database"]]
else
- Rails.logger.error "[YogiBerra Error] Couldn't connect to the mongo database on host: #{host} port: #{port}."
+ YogiBerra::Logger.log("Couldn't connect to the mongo database on host: #{host} port: #{port}.", :error)
end
else
- Rails.logger.error "[YogiBerra Error] Couldn't load the yogi.yml file."
+ YogiBerra::Logger.log("Couldn't load the yogi.yml file.", :error)
end
end
end
end
end
\ No newline at end of file