enrichment_db 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/Gemfile.lock +2 -2
- data/enrichment_db-0.1.4.gem +0 -0
- data/lib/enrichment_db/census/datum.rb +34 -0
- data/lib/enrichment_db/census.rb +6 -0
- data/lib/enrichment_db/datum.rb +20 -0
- data/lib/enrichment_db/db.rb +26 -5
- data/lib/enrichment_db/geo/region.rb +6 -4
- data/lib/enrichment_db/helper.rb +17 -0
- data/lib/enrichment_db/version.rb +1 -1
- data/lib/enrichment_db.rb +2 -0
- data/test/census/datum_test.rb +10 -0
- data/test/datum_test.rb +12 -0
- data/test/db_test.rb +27 -0
- data/test/geo/locator_test.rb +13 -15
- data/test/helper_test.rb +28 -0
- metadata +23 -8
- data/.DS_Store +0 -0
- data/test/.DS_Store +0 -0
- data/test/reports/TEST-EnrichmentDb-Geo-Locator.xml +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ceeab3783a59c54f74ed47b5068319a203955e2c
|
4
|
+
data.tar.gz: b0fffe8b8f5856086423784b8fac993e4803517c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 454bc5d8e9e609175f6a9292d7b3268a21dcc3c5715617e08bb12c2a9ff6ef9e88c64e98880bd02d69d57b6fd7ef7c9aea08f6ae91d0740e50005dcbbc82b4b3
|
7
|
+
data.tar.gz: 5f16865505cfb6c740654684d4ccf4905a3d936049e1f73146d8ee710fd9282573405e15beadea9f6eee8a107e14e627bb0c5874829a14d7ef6bc4b09408c7f6
|
data/.gitignore
ADDED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
enrichment_db (0.
|
4
|
+
enrichment_db (0.1.5)
|
5
5
|
multi_json (~> 1.3)
|
6
6
|
pg
|
7
7
|
pr_geohash
|
@@ -33,7 +33,7 @@ GEM
|
|
33
33
|
minitest (>= 5.0)
|
34
34
|
ruby-progressbar
|
35
35
|
multi_json (1.11.2)
|
36
|
-
pg (0.18.
|
36
|
+
pg (0.18.3)
|
37
37
|
pr_geohash (1.0.0)
|
38
38
|
rake (10.4.2)
|
39
39
|
ruby-progressbar (1.7.5)
|
Binary file
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class EnrichmentDb::Census::Datum < EnrichmentDb::DatumModel
|
4
|
+
attr_reader :id
|
5
|
+
|
6
|
+
DATABASE_NAME = 'censis'
|
7
|
+
|
8
|
+
def self.by_id(table_name, id)
|
9
|
+
puts "Finding object from #{table_name} with region_id = '#{id}'."
|
10
|
+
query = "SELECT * FROM #{DATABASE_NAME}.#{table_name} where region_id = $1"
|
11
|
+
values = [id]
|
12
|
+
|
13
|
+
result = EnrichmentDb.request(DATABASE_NAME, query, values)
|
14
|
+
|
15
|
+
if result.ntuples == 1
|
16
|
+
puts "Found object with region_id = '#{id}'"
|
17
|
+
format_result(result, id)
|
18
|
+
else
|
19
|
+
puts "Nothing found"
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.format_result(result, id)
|
25
|
+
result = result[0]
|
26
|
+
result = EnrichmentDb::Helper.hash_float_str_to_float(result)
|
27
|
+
region_type = result['region_type']
|
28
|
+
region_type = region_type[0..-2]
|
29
|
+
region = EnrichmentDb::Geo.const_get(region_type).by_id(id.to_i)
|
30
|
+
region = EnrichmentDb::Helper.hash_float_str_to_float(region)
|
31
|
+
result['region'] = region
|
32
|
+
result
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class EnrichmentDb::Datum < EnrichmentDb::DatumModel
|
4
|
+
def self.by_id(schema_name, table_name, condition)
|
5
|
+
puts "Finding object from #{table_name} with condition #{condition}."
|
6
|
+
query = "SELECT * FROM #{schema_name}.#{table_name} where #{condition}"
|
7
|
+
|
8
|
+
result = EnrichmentDb.request(schema_name, query)
|
9
|
+
|
10
|
+
if result.ntuples > 0
|
11
|
+
puts "Found #{result.ntuples} object/s"
|
12
|
+
result.collect do |v|
|
13
|
+
v
|
14
|
+
end
|
15
|
+
else
|
16
|
+
puts "Nothing found"
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/enrichment_db/db.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require 'pg'
|
2
2
|
|
3
3
|
module EnrichmentDb
|
4
|
+
|
4
5
|
def self.db_connection
|
5
6
|
@__db_connection ||= PGconn.connect(
|
6
|
-
host:'geo-mappings.cfmmyp7uix0j.ap-southeast-2.rds.amazonaws.com',
|
7
|
-
dbname:'geo',
|
7
|
+
host: 'geo-mappings.cfmmyp7uix0j.ap-southeast-2.rds.amazonaws.com',
|
8
|
+
dbname: 'geo',
|
8
9
|
user: 'lexer',
|
9
10
|
password: 'campl3x3r'
|
10
11
|
)
|
@@ -14,11 +15,31 @@ module EnrichmentDb
|
|
14
15
|
@__api_connection = nil
|
15
16
|
end
|
16
17
|
|
17
|
-
def self.
|
18
|
+
def self.table_names(schema = 'public')
|
19
|
+
query = "SELECT table_name
|
20
|
+
FROM information_schema.tables
|
21
|
+
WHERE table_schema='#{schema}'
|
22
|
+
AND table_type='BASE TABLE'"
|
23
|
+
request(schema, query).collect do |h|
|
24
|
+
h['table_name']
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.table_columns(table, schema = 'public')
|
29
|
+
query = "SELECT *
|
30
|
+
FROM information_schema.columns
|
31
|
+
WHERE table_schema = '#{schema}'
|
32
|
+
AND table_name = '#{table}'"
|
33
|
+
request(schema, query).collect do |h|
|
34
|
+
h['column_name']
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.request(db, query, values = nil)
|
18
39
|
type = request_type(query)
|
19
40
|
|
20
41
|
case type
|
21
|
-
when :select, 'select'
|
42
|
+
when :select, 'select', '\d'
|
22
43
|
result = if values
|
23
44
|
EnrichmentDb::db_connection.exec(query, values)
|
24
45
|
else
|
@@ -32,7 +53,7 @@ module EnrichmentDb
|
|
32
53
|
end
|
33
54
|
|
34
55
|
def self.request_type(query)
|
35
|
-
result = query.downcase.match(/(^(?:drop|select|insert|delete))/)
|
56
|
+
result = query.downcase.match(/(^(?:drop|select|insert|delete|\\d))/)
|
36
57
|
return unless result
|
37
58
|
|
38
59
|
result[0]
|
@@ -2,6 +2,8 @@ class EnrichmentDb::Geo::Region < EnrichmentDb::DatumModel
|
|
2
2
|
attr_reader :id
|
3
3
|
attr_reader :name
|
4
4
|
|
5
|
+
DATABASE_NAME = 'geo'
|
6
|
+
|
5
7
|
def initialize(data)
|
6
8
|
@id = data['id']
|
7
9
|
@name = data['name']
|
@@ -11,10 +13,10 @@ class EnrichmentDb::Geo::Region < EnrichmentDb::DatumModel
|
|
11
13
|
puts "Finding #{object_type} with id = '#{id}'."
|
12
14
|
field_names = fields
|
13
15
|
table = make_table_name
|
14
|
-
query = "SELECT #{field_names} FROM #{table} where id = $1"
|
16
|
+
query = "SELECT #{field_names} FROM #{DATABASE_NAME}.#{table} where id = $1"
|
15
17
|
values = [id]
|
16
18
|
|
17
|
-
result = EnrichmentDb.request(query, values)
|
19
|
+
result = EnrichmentDb.request(DATABASE_NAME, query, values)
|
18
20
|
if result.ntuples == 1
|
19
21
|
puts "Found #{object_type}"
|
20
22
|
result[0]
|
@@ -28,9 +30,9 @@ class EnrichmentDb::Geo::Region < EnrichmentDb::DatumModel
|
|
28
30
|
puts "Finding #{object_type} intersecting with some geohash"
|
29
31
|
field_names = fields
|
30
32
|
table = make_table_name
|
31
|
-
query = "SELECT #{field_names} FROM #{table} where ST_Within(ST_SetSRID(ST_GeomFromGeoHash($1), 4283), boundary)"
|
33
|
+
query = "SELECT #{field_names} FROM #{DATABASE_NAME}.#{table} where ST_Within(ST_SetSRID(ST_GeomFromGeoHash($1), 4283), boundary)"
|
32
34
|
|
33
|
-
result = EnrichmentDb.request(query, values)
|
35
|
+
result = EnrichmentDb.request(DATABASE_NAME, query, values)
|
34
36
|
if result.ntuples == 1
|
35
37
|
puts "Found #{object_type}"
|
36
38
|
result[0]
|
@@ -0,0 +1,17 @@
|
|
1
|
+
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}")
|
2
|
+
|
3
|
+
module EnrichmentDb::Helper
|
4
|
+
def self.float_str_to_float(str)
|
5
|
+
if str.match(/^[0-9\.-]*$/)
|
6
|
+
str.to_f
|
7
|
+
else
|
8
|
+
str
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.hash_float_str_to_float(hash)
|
13
|
+
hash.each_with_object({}) do |(k, v), h|
|
14
|
+
h[k] = float_str_to_float(v)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/enrichment_db.rb
CHANGED
@@ -0,0 +1,10 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/../test_helper"
|
2
|
+
|
3
|
+
describe EnrichmentDb::Census::Datum do
|
4
|
+
it 'should retrieve correct record' do
|
5
|
+
result = EnrichmentDb::Census::Datum.by_id('selected_medians_and_averages', '3000')
|
6
|
+
assert_equal 3000, result['region_id']
|
7
|
+
assert_equal 'Postcodes', result['region_type']
|
8
|
+
assert_equal 26, result['median_age_of_persons']
|
9
|
+
end
|
10
|
+
end
|
data/test/datum_test.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/test_helper"
|
2
|
+
|
3
|
+
describe EnrichmentDb::Datum do
|
4
|
+
it 'should get correct ato record' do
|
5
|
+
condition = "region_id = '3000'"
|
6
|
+
result = EnrichmentDb::Datum.by_id('ato', 'taxable_incomes', condition)
|
7
|
+
region = result.first
|
8
|
+
assert_equal '3000', region['region_id']
|
9
|
+
assert_equal 'Postcodes', region['region_type']
|
10
|
+
assert_equal '51948.229', region['taxable_income']
|
11
|
+
end
|
12
|
+
end
|
data/test/db_test.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/test_helper"
|
2
|
+
|
3
|
+
describe EnrichmentDb do
|
4
|
+
it 'should list all table names for geo schema' do
|
5
|
+
result = EnrichmentDb.table_names('geo')
|
6
|
+
geo_table_count = 7
|
7
|
+
assert_equal geo_table_count, result.size
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should list all table names for census schema' do
|
11
|
+
result = EnrichmentDb.table_names('censis')
|
12
|
+
census_table_count = 33
|
13
|
+
assert_equal census_table_count, result.size
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should list table columns from geo.sa1 table' do
|
17
|
+
result = EnrichmentDb.table_columns('sa1s', 'geo')
|
18
|
+
sa1s_column_count = 8
|
19
|
+
assert_equal sa1s_column_count, result.size
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should list table columns from censis.selected_medians_and_averages table' do
|
23
|
+
result = EnrichmentDb.table_columns('selected_medians_and_averages', 'censis')
|
24
|
+
sa1s_column_count = 8
|
25
|
+
assert_equal sa1s_column_count, result.size
|
26
|
+
end
|
27
|
+
end
|
data/test/geo/locator_test.rb
CHANGED
@@ -9,33 +9,31 @@ describe EnrichmentDb::Geo::Locator do
|
|
9
9
|
assert_equal id, result
|
10
10
|
end
|
11
11
|
|
12
|
-
it 'should correctly format a
|
12
|
+
it 'should correctly format a hash to a latlong' do
|
13
13
|
lat = 10
|
14
14
|
long = 180
|
15
15
|
lat_long = { 'lat' => lat, 'long' => long }
|
16
|
-
|
17
|
-
|
16
|
+
begin
|
17
|
+
geohash = EnrichmentDb::Geo::Locator.format_geo(lat_long)
|
18
|
+
rescue EnrichmentDb::InvalidGeoPointFormat => e
|
19
|
+
# Should get here
|
20
|
+
else
|
21
|
+
flunk('This should have errored out.')
|
22
|
+
end
|
18
23
|
end
|
19
24
|
|
20
25
|
it 'should break as the lat long format is incorrect' do
|
21
26
|
lat = 10
|
22
27
|
long = 180
|
23
28
|
lat_long = { 'lat' => lat, 'lon' => long }
|
24
|
-
|
25
|
-
|
26
|
-
rescue EnrichmentDb::InvalidGeoPointFormat => e
|
27
|
-
|
28
|
-
else
|
29
|
-
flunk('This should have errored out.')
|
30
|
-
end
|
29
|
+
geohash = EnrichmentDb::Geo::Locator.format_geo(lat_long)
|
30
|
+
assert_equal 'xczbzurypzpg', geohash
|
31
31
|
end
|
32
32
|
|
33
|
-
it 'should ' do
|
33
|
+
it 'should find region from geohash' do
|
34
34
|
geo = 'r1r0g2e'
|
35
35
|
result = EnrichmentDb::Geo::Locator.geo_locate(geo)
|
36
|
-
|
37
|
-
|
38
|
-
puts result.inspect
|
36
|
+
assert_equal ':sa4', result.smallest_region.inspect
|
37
|
+
assert_equal 'Melbourne - Inner', result.name
|
39
38
|
end
|
40
|
-
|
41
39
|
end
|
data/test/helper_test.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/test_helper"
|
2
|
+
|
3
|
+
describe EnrichmentDb::Helper do
|
4
|
+
it 'should convert float str to float' do
|
5
|
+
result = EnrichmentDb::Helper.float_str_to_float('26')
|
6
|
+
assert_equal 26, result
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should not convert float str to float' do
|
10
|
+
input = 's26'
|
11
|
+
result = EnrichmentDb::Helper.float_str_to_float(input)
|
12
|
+
assert_equal input, result
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should convert hash with float str values to float values' do
|
16
|
+
input = { 'er' => '-26', 'rt' => '234567.43' }
|
17
|
+
result = EnrichmentDb::Helper.hash_float_str_to_float(input)
|
18
|
+
expecting = { 'er' => -26, 'rt' => 234567.43 }
|
19
|
+
assert_equal expecting, result
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should partially convert hash with float str values to float values' do
|
23
|
+
input = { 'er' => '-26', 'rt' => '23s4567.43' }
|
24
|
+
result = EnrichmentDb::Helper.hash_float_str_to_float(input)
|
25
|
+
expecting = { 'er' => -26, 'rt' => '23s4567.43' }
|
26
|
+
assert_equal expecting, result
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enrichment_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Wallis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_json
|
@@ -58,15 +58,19 @@ executables: []
|
|
58
58
|
extensions: []
|
59
59
|
extra_rdoc_files: []
|
60
60
|
files:
|
61
|
-
- ".
|
61
|
+
- ".gitignore"
|
62
62
|
- Gemfile
|
63
63
|
- Gemfile.lock
|
64
64
|
- LICENSE
|
65
65
|
- README.md
|
66
66
|
- Rakefile
|
67
|
+
- enrichment_db-0.1.4.gem
|
67
68
|
- enrichment_db.gemspec
|
68
69
|
- lib/enrichment_db.rb
|
69
70
|
- lib/enrichment_db/.DS_Store
|
71
|
+
- lib/enrichment_db/census.rb
|
72
|
+
- lib/enrichment_db/census/datum.rb
|
73
|
+
- lib/enrichment_db/datum.rb
|
70
74
|
- lib/enrichment_db/db.rb
|
71
75
|
- lib/enrichment_db/error.rb
|
72
76
|
- lib/enrichment_db/geo.rb
|
@@ -79,8 +83,11 @@ files:
|
|
79
83
|
- lib/enrichment_db/geo/sa4.rb
|
80
84
|
- lib/enrichment_db/geo/state.rb
|
81
85
|
- lib/enrichment_db/geo/suburb.rb
|
86
|
+
- lib/enrichment_db/helper.rb
|
82
87
|
- lib/enrichment_db/version.rb
|
83
|
-
- test
|
88
|
+
- test/census/datum_test.rb
|
89
|
+
- test/datum_test.rb
|
90
|
+
- test/db_test.rb
|
84
91
|
- test/factories/postcode.rb
|
85
92
|
- test/factories/sa1.rb
|
86
93
|
- test/factories/sa2.rb
|
@@ -89,7 +96,7 @@ files:
|
|
89
96
|
- test/factories/state.rb
|
90
97
|
- test/factories/suburb.rb
|
91
98
|
- test/geo/locator_test.rb
|
92
|
-
- test/
|
99
|
+
- test/helper_test.rb
|
93
100
|
- test/test_helper.rb
|
94
101
|
homepage: https://github.com/lexerdev/enrichment-db.gem
|
95
102
|
licenses:
|
@@ -116,15 +123,19 @@ signing_key:
|
|
116
123
|
specification_version: 4
|
117
124
|
summary: Lexer Enrichment Database Gem
|
118
125
|
test_files:
|
119
|
-
- ".
|
126
|
+
- ".gitignore"
|
120
127
|
- Gemfile
|
121
128
|
- Gemfile.lock
|
122
129
|
- LICENSE
|
123
130
|
- README.md
|
124
131
|
- Rakefile
|
132
|
+
- enrichment_db-0.1.4.gem
|
125
133
|
- enrichment_db.gemspec
|
126
134
|
- lib/enrichment_db.rb
|
127
135
|
- lib/enrichment_db/.DS_Store
|
136
|
+
- lib/enrichment_db/census.rb
|
137
|
+
- lib/enrichment_db/census/datum.rb
|
138
|
+
- lib/enrichment_db/datum.rb
|
128
139
|
- lib/enrichment_db/db.rb
|
129
140
|
- lib/enrichment_db/error.rb
|
130
141
|
- lib/enrichment_db/geo.rb
|
@@ -137,8 +148,11 @@ test_files:
|
|
137
148
|
- lib/enrichment_db/geo/sa4.rb
|
138
149
|
- lib/enrichment_db/geo/state.rb
|
139
150
|
- lib/enrichment_db/geo/suburb.rb
|
151
|
+
- lib/enrichment_db/helper.rb
|
140
152
|
- lib/enrichment_db/version.rb
|
141
|
-
- test
|
153
|
+
- test/census/datum_test.rb
|
154
|
+
- test/datum_test.rb
|
155
|
+
- test/db_test.rb
|
142
156
|
- test/factories/postcode.rb
|
143
157
|
- test/factories/sa1.rb
|
144
158
|
- test/factories/sa2.rb
|
@@ -147,5 +161,6 @@ test_files:
|
|
147
161
|
- test/factories/state.rb
|
148
162
|
- test/factories/suburb.rb
|
149
163
|
- test/geo/locator_test.rb
|
150
|
-
- test/
|
164
|
+
- test/helper_test.rb
|
151
165
|
- test/test_helper.rb
|
166
|
+
has_rdoc:
|
data/.DS_Store
DELETED
Binary file
|
data/test/.DS_Store
DELETED
Binary file
|
@@ -1,11 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
-
<testsuite name="EnrichmentDb::Geo::Locator" skipped="0" failures="0" errors="0" tests="4" assertions="2" time="6.202437521191314">
|
3
|
-
<testcase name="test_0004_should " classname="EnrichmentDb::Geo::Locator" assertions="0" time="6.202266245032661">
|
4
|
-
</testcase>
|
5
|
-
<testcase name="test_0003_should break as the lat long format is incorrect" classname="EnrichmentDb::Geo::Locator" assertions="0" time="6.976805161684752e-05">
|
6
|
-
</testcase>
|
7
|
-
<testcase name="test_0002_should correctly format a has to a latlong" classname="EnrichmentDb::Geo::Locator" assertions="1" time="8.18880507722497e-05">
|
8
|
-
</testcase>
|
9
|
-
<testcase name="test_0001_should find id from a hash of hashes" classname="EnrichmentDb::Geo::Locator" assertions="1" time="1.962005626410246e-05">
|
10
|
-
</testcase>
|
11
|
-
</testsuite>
|