Sha256: 0530b35dbd4df57441fcf3758077d04c8ab6d1d22123d49ed01b3910133f77c9

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

class EnrichmentDb::Geo::Region < EnrichmentDb::DatumModel
  attr_reader :id
  attr_reader :name

  DATABASE_NAME = 'geo'

  def initialize(data)
    @id = data['id']
    @name = data['name']
  end

  def self.by_id(id)
    puts "Finding #{object_type} with id = '#{id}'."
    field_names = fields
    table = make_table_name
    query = "SELECT #{field_names} FROM #{DATABASE_NAME}.#{table} where id = $1"
    values = [id]
    
    result = EnrichmentDb.request(DATABASE_NAME, query, values)
    if result.ntuples == 1 
      puts "Found #{object_type}"
      result[0]
    else 
      puts "Nothing found"
      nil
    end
  end

  def self.by_boundary(values)
    puts "Finding #{object_type} intersecting with some geohash"
    field_names = fields
    table = make_table_name
    query = "SELECT #{field_names} FROM #{DATABASE_NAME}.#{table} where ST_Within(ST_SetSRID(ST_GeomFromGeoHash($1), 4283), boundary)"

    result = EnrichmentDb.request(DATABASE_NAME, query, values)
    if result.ntuples == 1 
      puts "Found #{object_type}"
      result[0]
    else 
      puts "Nothing found"
      nil
    end
  end
  
  private

  def self.object_type
    to_s.split(':').last.downcase.to_sym
  end

  def self.make_table_name
    "#{object_type}s"
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
enrichment_db-0.1.10 lib/enrichment_db/geo/region.rb
enrichment_db-0.1.9 lib/enrichment_db/geo/region.rb
enrichment_db-0.1.8 lib/enrichment_db/geo/region.rb
enrichment_db-0.1.7 lib/enrichment_db/geo/region.rb
enrichment_db-0.1.6 lib/enrichment_db/geo/region.rb
enrichment_db-0.1.5 lib/enrichment_db/geo/region.rb