Class: NoBrainer::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/no_brainer/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
# File 'lib/no_brainer/connection.rb', line 7

def initialize(uri)
  @orig_uri = uri
  parse_uri(uri)
end

Instance Attribute Details

#orig_uriObject

Returns the value of attribute orig_uri.



5
6
7
# File 'lib/no_brainer/connection.rb', line 5

def orig_uri
  @orig_uri
end

#parsed_uriObject

Returns the value of attribute parsed_uri.



5
6
7
# File 'lib/no_brainer/connection.rb', line 5

def parsed_uri
  @parsed_uri
end

Instance Method Details

#current_dbObject



49
50
51
# File 'lib/no_brainer/connection.rb', line 49

def current_db
  NoBrainer.current_run_options[:db] || default_db
end

#default_dbObject



45
46
47
# File 'lib/no_brainer/connection.rb', line 45

def default_db
  parsed_uri[:db]
end

#parse_uri(uri) ⇒ Object



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

def parse_uri(uri)
  @parsed_uri = begin
    uri = URI.parse(uri)

    if uri.scheme != 'rethinkdb'
      raise NoBrainer::Error::Connection,
        "Invalid URI. Expecting something like rethinkdb://host:port/database. Got #{uri}"
    end

    {
      :user     => uri.user && CGI.unescape(uri.user),
      :password => uri.password && CGI.unescape(uri.password),
      :host     => uri.host,
      :port     => uri.port || 28015,
      :db       => uri.path.gsub(/^\//, ''),
    }.tap { |result| raise "No database specified in #{uri}" unless result[:db].present? }
  end
end

#rawObject Also known as: connect



35
36
37
38
39
# File 'lib/no_brainer/connection.rb', line 35

def raw
  options = parsed_uri
  options = options.merge(:ssl => NoBrainer::Config.ssl_options) if NoBrainer::Config.ssl_options
  @raw ||= RethinkDB::Connection.new(options).tap { NoBrainer.logger.info("Connected to #{uri}") }
end

#uriObject



31
32
33
# File 'lib/no_brainer/connection.rb', line 31

def uri
  "rethinkdb://#{'****@' if parsed_uri[:password]}#{parsed_uri[:host]}:#{parsed_uri[:port]}/#{parsed_uri[:db]}"
end