Sha256: 79a51f3c419af4354a63c80330d684b561af1d0912dfb53e230e75002fb7eeaa

Contents?: true

Size: 1.87 KB

Versions: 5

Compression:

Stored size: 1.87 KB

Contents

module FlareUp

  class HostUnknownOrInaccessibleError < StandardError
  end
  class TimeoutError < StandardError
  end
  class NoDatabaseError < StandardError
  end
  class AuthenticationError < StandardError
  end
  class UnknownError < StandardError
  end

  class Connection

    attr_accessor :host
    attr_accessor :port
    attr_accessor :dbname
    attr_accessor :user
    attr_accessor :password
    attr_accessor :connect_timeout

    def initialize(host, dbname, user, password)
      @host = host
      @dbname = dbname
      @user = user
      @password = password

      @port = 5439
      @connect_timeout = 5
    end

    # TODO - Not quite sure how to test this; perhaps fold connect/execute into
    # TODO   one method so we can close connections in case of failure, etc.
    def execute(statement)
      @pg_conn ||= connect
      @pg_conn.exec(statement)
    end

    private

    def connect
      begin
        PG.connect(connection_parameters)
      rescue PG::ConnectionBad => e
        case e.message
          when /nodename nor servname provided, or not known/
            raise HostUnknownOrInaccessibleError, "Host unknown or unreachable: #{@host}"
          when /timeout expired/
            raise TimeoutError, 'Timeout connecting to the database (have you checked your Redshift security groups?)'
          when /database ".+" does not exist/
            raise NoDatabaseError, "Database #{@dbname} does not exist"
          when /password authentication failed for user/
            raise AuthenticationError, "Either username '#{@user}' or password invalid"
          else
            raise UnknownError
        end
      end
    end

    def connection_parameters
      {
        :host => @host,
        :port => @port,
        :dbname => @dbname,
        :user => @user,
        :password => @password,
        :connect_timeout => @connect_timeout
      }
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
flare-up-0.6 lib/flare_up/connection.rb
flare-up-0.5 lib/flare_up/connection.rb
flare-up-0.4 lib/flare_up/connection.rb
flare-up-0.3 lib/flare_up/connection.rb
flare-up-0.2 lib/flare_up/connection.rb