Sha256: bec579c338b2e996dbd756ae21a3e51b4aa82525bcfec3b3acc2a29c79424630

Contents?: true

Size: 979 Bytes

Versions: 5

Compression:

Stored size: 979 Bytes

Contents

require 'pg'

module EnrichmentDb
  def self.db_connection
    @__db_connection ||= PGconn.connect(
      host:'geo-mappings.cfmmyp7uix0j.ap-southeast-2.rds.amazonaws.com', 
      dbname:'geo', 
      user: 'lexer', 
      password: 'campl3x3r'
    )
  end

  def self.reset_connection
    @__api_connection = nil
  end

  def self.request(query, values)
    type = request_type(query)

    case type
    when :select, 'select' 
      result = if values
        EnrichmentDb::db_connection.exec(query, values)
      else
        EnrichmentDb::db_connection.exec(query)
      end
    when :insert, :drop, :delete, 'insert', 'drop', 'delete'
      fail EnrichmentDb::RequestError, "Postgres method '#{type}' is unsupported"
    else
      fail EnrichmentDb::RequestError, "Postgres query '#{query}' is unsupported"
    end
  end

  def self.request_type(query)
    result = query.downcase.match(/(^(?:drop|select|insert|delete))/)
    return unless result

    result[0]
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
enrichment_db-0.1.4 lib/enrichment_db/db.rb
enrichment_db-0.1.3 lib/enrichment_db/db.rb
enrichment_db-0.1.2 lib/enrichment_db/db.rb
enrichment_db-0.1.1 lib/enrichment_db/db.rb
enrichment_db-0.1.0 lib/enrichment_db/db.rb