Class: Connectors::Mongodb

Inherits:
Object
  • Object
show all
Defined in:
lib/connectors/mongodb.rb

Overview

Mongodb health checker

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configured?Boolean

Returns:

  • (Boolean)

Raises:

  • (StandardError)


19
20
21
22
23
# File 'lib/connectors/mongodb.rb', line 19

def self.configured?
  raise StandardError, 'Please make sure you have MongoDB installed' unless defined?(::Mongo::Client)

  'configured'
end

Instance Method Details

#connectObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/connectors/mongodb.rb', line 6

def connect
  @client = Mongo::Client.new(
    ENV.fetch('MONGO_DB_URI', nil) || ['localhost:27017']
  )
  @client.command(ping: 1)
  true
rescue Mongo::Error::SocketError, Mongo::Error::NoServerAvailable
  puts "Mongodb Connection Error: #{e.message}"
  false
ensure
  @client&.close
end