enrichment_db 0.1.10 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ced6e9cc62c6dd68ec83db0abe2684b6c3c4535a
4
- data.tar.gz: 0f1fcffaef04c8d30c74f73892b0893379ada295
3
+ metadata.gz: 4df9713d1120be2a7dbe8af63a95a05d21912d05
4
+ data.tar.gz: 7cd8617b5f062ab74b72a6c15856a93637ad678a
5
5
  SHA512:
6
- metadata.gz: aac6db9cd446762db07c085673c20d3953995c09c749dfaa8829061440b5ac6a1ed30dd25379f719712e80b0bb6c70dec5822d28128f6aeca9cd46e61948b490
7
- data.tar.gz: 07e674766a38fb0f94344f0e8400e745a79a0823fe2b198a8c9ec20a2f764eb94856c6a3495d726010bc64ebe1104fa496ef6ba7bed4499a6c4ac7dd0eef89a0
6
+ metadata.gz: b6c80bd4ffca67734aadcbd11bc46b9ada5411d62800900ecec2205eff23ca3f87fd36c696ba27292611a147d896d1f4014777de6c5c0d3a1d1148ea4b940f33
7
+ data.tar.gz: b9685ff855a8305f7b1f0b294e7b2cd4296e0fe6e019df91b0cf0f0b6352c83497d4ba4ff9d0f57c014f26e4e128537f319f32d437784aa02739790c280191bb
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enrichment_db (0.1.7)
4
+ enrichment_db (0.1.11)
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.3)
36
+ pg (0.18.4)
37
37
  pr_geohash (1.0.0)
38
38
  rake (10.4.2)
39
39
  ruby-progressbar (1.7.5)
@@ -0,0 +1,17 @@
1
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}")
2
+
3
+ module EnrichmentDb::Cache
4
+ def self.get(query, values = nil)
5
+ query_cache(query, values)
6
+ end
7
+
8
+ def self.set(query, values = nil, result = nil)
9
+ query_cache(query, values, result)
10
+ end
11
+
12
+ def self.query_cache(query, values, result = nil)
13
+ key = query + values.to_s
14
+ @query_cache ||= {}
15
+ @query_cache[key] ||= result
16
+ end
17
+ end
@@ -1,4 +1,5 @@
1
1
  require 'pg'
2
+ require 'cache'
2
3
 
3
4
  module EnrichmentDb
4
5
 
@@ -40,11 +41,7 @@ module EnrichmentDb
40
41
 
41
42
  case type
42
43
  when :select, 'select', '\d'
43
- result = if values
44
- EnrichmentDb::db_connection.exec(query, values)
45
- else
46
- EnrichmentDb::db_connection.exec(query)
47
- end
44
+ get_or_request(query, values)
48
45
  when :insert, :drop, :delete, 'insert', 'drop', 'delete'
49
46
  fail EnrichmentDb::RequestError, "Postgres method '#{type}' is unsupported"
50
47
  else
@@ -52,6 +49,18 @@ module EnrichmentDb
52
49
  end
53
50
  end
54
51
 
52
+ def self.get_or_request(query, values = nil)
53
+ result = EnrichmentDb::Cache.get(query, values)
54
+ return result unless result.nil?
55
+
56
+ result = if values
57
+ EnrichmentDb::db_connection.exec(query, values)
58
+ else
59
+ EnrichmentDb::db_connection.exec(query)
60
+ end
61
+ EnrichmentDb::Cache.set(query, values, result)
62
+ end
63
+
55
64
  def self.request_type(query)
56
65
  result = query.downcase.match(/(^(?:drop|select|insert|delete|\\d))/)
57
66
  return unless result
@@ -32,13 +32,21 @@ class EnrichmentDb::Geo::Region < EnrichmentDb::DatumModel
32
32
  table = make_table_name
33
33
  query = "SELECT #{field_names} FROM #{DATABASE_NAME}.#{table} where ST_Within(ST_SetSRID(ST_GeomFromGeoHash($1), 4283), boundary)"
34
34
 
35
- result = EnrichmentDb.request(DATABASE_NAME, query, values)
36
- if result.ntuples == 1
37
- puts "Found #{object_type}"
38
- result[0]
39
- else
40
- puts "Nothing found"
41
- nil
35
+ begin
36
+ result = EnrichmentDb.request(DATABASE_NAME, query, values)
37
+
38
+ if result.ntuples == 1
39
+ puts "Found #{object_type}"
40
+ result[0]
41
+ else
42
+ puts "Nothing found"
43
+ nil
44
+ end
45
+ rescue PG::InternalError => e
46
+ # This usually is GEOSContains: TopologyException: side location conflict at #{lat} #{long}
47
+ LexerAPI.error e.class
48
+ LexerAPI.error e.message
49
+ return nil
42
50
  end
43
51
  end
44
52
 
@@ -2,6 +2,7 @@ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}")
2
2
 
3
3
  module EnrichmentDb::Helper
4
4
  def self.float_str_to_float(str)
5
+ return if str.nil?
5
6
  if str.match(/^[0-9\.-]*$/)
6
7
  str.to_f
7
8
  else
@@ -10,7 +10,10 @@ class EnrichmentDb::Language::Datum < EnrichmentDb::DatumModel
10
10
  end.sort_by do |h|
11
11
  h['value']
12
12
  end.each_with_object({}) do |record, h|
13
- h[record.fetch('value')] = record.fetch('language')
13
+ h[record.fetch('value')] ||= []
14
+ h[record.fetch('value')] << record.fetch('language')
15
+ end.each_with_object({}) do |(key, values), h|
16
+ h[key] = values.join('|')
14
17
  end
15
18
  else
16
19
  puts "Nothing found"
@@ -1,3 +1,3 @@
1
1
  module EnrichmentDb
2
- VERSION = '0.1.10'
2
+ VERSION = '0.1.11'
3
3
  end
@@ -0,0 +1,31 @@
1
+ require "#{File.dirname(__FILE__)}/test_helper"
2
+
3
+ describe EnrichmentDb::Cache do
4
+ before do
5
+ EnrichmentDb::Cache.instance_variable_set(:@query_cache, nil)
6
+ end
7
+
8
+ it 'should return nil as value hasn\'t been set when no values' do
9
+ query = 'do stuff'
10
+ assert_nil EnrichmentDb::Cache.get(query)
11
+ end
12
+
13
+ it 'should return nil as value hasn\'t been set when non-empty values' do
14
+ query = 'do stuff'
15
+ values = ['er']
16
+ assert_nil EnrichmentDb::Cache.get(query, values)
17
+ end
18
+
19
+ it 'should set value on cache and get it' do
20
+ query = 'do stuff'
21
+ values = ['er']
22
+ record = 'This here is a gut dang it record'
23
+ set_result = EnrichmentDb::Cache.set(query, values, record)
24
+
25
+ get_result = EnrichmentDb::Cache.set(query, values)
26
+
27
+ refute_nil set_result
28
+ refute_nil get_result
29
+ assert_equal set_result, get_result
30
+ end
31
+ end
data/test/datum_test.rb CHANGED
@@ -3,7 +3,7 @@ require "#{File.dirname(__FILE__)}/test_helper"
3
3
  describe EnrichmentDb::Datum do
4
4
  it 'should get correct ato record' do
5
5
  condition = "region_id = '3000'"
6
- result = EnrichmentDb::Datum.by_id('ato', 'taxable_incomes', condition)
6
+ result = EnrichmentDb::Datum.by_lambda('ato', 'taxable_incomes', condition)
7
7
  region = result.first
8
8
  assert_equal '3000', region['region_id']
9
9
  assert_equal 'Postcodes', region['region_type']
@@ -9,4 +9,15 @@ describe EnrichmentDb::Language::Datum do
9
9
  expecting = 'Acer'
10
10
  assert_equal expecting, test_result
11
11
  end
12
+
13
+ it 'should retrieve correct record via a lambda' do
14
+ table_name = 'location_countries'
15
+ schema_name = 'bio'
16
+ value = 'Australia'
17
+ lambda = "value = '#{value}'"
18
+ result = EnrichmentDb::Language::Datum.by_lambda(schema_name, table_name, lambda)
19
+ test_result = result.keys.first
20
+ expecting = 'Australia'
21
+ assert_equal expecting, test_result
22
+ end
12
23
  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.10
4
+ version: 0.1.11
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-12-02 00:00:00.000000000 Z
11
+ date: 2016-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -69,6 +69,7 @@ files:
69
69
  - lib/enrichment_db.rb
70
70
  - lib/enrichment_db/.DS_Store
71
71
  - lib/enrichment_db/ato/datum.rb
72
+ - lib/enrichment_db/cache.rb
72
73
  - lib/enrichment_db/census.rb
73
74
  - lib/enrichment_db/census/datum.rb
74
75
  - lib/enrichment_db/datum.rb
@@ -88,6 +89,7 @@ files:
88
89
  - lib/enrichment_db/language.rb
89
90
  - lib/enrichment_db/language/datum.rb
90
91
  - lib/enrichment_db/version.rb
92
+ - test/cache_test.rb
91
93
  - test/census/datum_test.rb
92
94
  - test/datum_test.rb
93
95
  - test/db_test.rb
@@ -138,6 +140,7 @@ test_files:
138
140
  - lib/enrichment_db.rb
139
141
  - lib/enrichment_db/.DS_Store
140
142
  - lib/enrichment_db/ato/datum.rb
143
+ - lib/enrichment_db/cache.rb
141
144
  - lib/enrichment_db/census.rb
142
145
  - lib/enrichment_db/census/datum.rb
143
146
  - lib/enrichment_db/datum.rb
@@ -157,6 +160,7 @@ test_files:
157
160
  - lib/enrichment_db/language.rb
158
161
  - lib/enrichment_db/language/datum.rb
159
162
  - lib/enrichment_db/version.rb
163
+ - test/cache_test.rb
160
164
  - test/census/datum_test.rb
161
165
  - test/datum_test.rb
162
166
  - test/db_test.rb