Class: YogiBerra::Catcher

Inherits:
Object
  • Object
show all
Extended by:
Facets
Defined in:
lib/yogi_berra/catcher.rb

Class Method Summary collapse

Methods included from Facets

cattr, cattr_accessor, cattr_reader, cattr_writer, mattr, mattr_accessor, mattr_reader, mattr_writer

Class Method Details

.db_client(host, port, replica_set = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/yogi_berra/catcher.rb', line 44

def db_client(host, port, replica_set = nil)
  # :w => 0 set the default write concern to 0, this allows writes to be non-blocking
  # by not waiting for a response from mongodb
  if replica_set
    @@mongo_client = Mongo::MongoReplicaSetClient.new(replica_set, :w => 0)
  else
    @@mongo_client = Mongo::MongoClient.new(host, port, :w => 0)
  end
rescue
  YogiBerra::Logger.log("Couldn't connect to the mongo database on host: #{host} port: #{port}.", :error)
  nil
end

.environmentObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/yogi_berra/catcher.rb', line 32

def environment
  if ENV["YOGI_ENV"]
    ENV["YOGI_ENV"]
  elsif defined?(Rails)
    Rails.env
  elsif ENV["RAILS_ENV"]
    ENV["RAILS_ENV"]
  else
    "test"
  end
end

.load_db_settings(config_file = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/yogi_berra/catcher.rb', line 11

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)
        @@settings = yaml_file["#{environment}"] if yaml_file
      end
    rescue
      YogiBerra::Logger.log("No such file: #{database_config}", :error)
    end
    @@settings
  end
end

.quick_connection(load_settings = false) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/yogi_berra/catcher.rb', line 57

def quick_connection(load_settings = false)
  load_db_settings if load_settings

  if @@settings
    host = @@settings["host"]
    port = @@settings["port"]
    database = @@settings["database"]
    username = @@settings["username"]
    password = @@settings["password"]
    replica_set = @@settings["replica_set"]
    client = db_client(host, port, replica_set)
    if client
      @@connection = client[database]
      if @@connection && username && password
        begin
          @@connection.authenticate(username, password)
        rescue
          YogiBerra::Logger.log("Couldn't authenticate with user #{username} to mongo database on host: #{host} port: #{port} database: #{database}.", :warn)
        end
      end
    else
      YogiBerra::Logger.log("Couldn't connect to the mongo database on host: #{host} port: #{port}.", :error)
    end
  else
    YogiBerra::Logger.log("Couldn't load the yogi.yml file.", :error) if load_settings
  end
  @@connection
end