Sha256: 14543845714137fb675187831a3405e4f9f96a119c118bb65f0f62ec791edd67

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'mongo'

module YogiBerra
  class Catcher
    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
          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}."
        nil
      end

      def quick_connection
        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}."
          end
        else
          Rails.logger.error "[YogiBerra Error] Couldn't load the yogi.yml file."
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yogi_berra-0.0.1 lib/yogi_berra/catcher.rb