enrichment_db 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/enrichment_db.rb +37 -0
- data/lib/enrichment_db/census/datum.rb +3 -25
- data/lib/enrichment_db/datum.rb +4 -10
- data/lib/enrichment_db/helper.rb +19 -0
- data/lib/enrichment_db/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e761011e4c6ae8f32a932c1c349730ce112ef66
|
4
|
+
data.tar.gz: 39cd9b7f3bd62161f202e13fe3e33e576292d0a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d67d7ba3d10a5bac786ae0d3353fc8e32a3850284eac00306ff14bdfbbd026e6fbd7edba7445e6ffa015ff2157f838640e0ec1ee93beb6025a1bd1df3877cea
|
7
|
+
data.tar.gz: cbced204ad097551a74f375c5b0a597fc5f063a902ecc83392ae136891b2e6cc86a219cba0ee45824d614ba684f2259be354c836f0ce30c8e9024caa8d69646a
|
data/lib/enrichment_db.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'enrichment_db/error'
|
2
2
|
require 'enrichment_db/version'
|
3
|
+
require 'enrichment_db/helper'
|
3
4
|
|
4
5
|
module EnrichmentDb
|
5
6
|
module_function
|
@@ -24,6 +25,41 @@ module EnrichmentDb
|
|
24
25
|
end
|
25
26
|
end
|
26
27
|
|
28
|
+
def self.by_id(table_name, id)
|
29
|
+
schema_name = self::DATABASE_NAME
|
30
|
+
uid_name = self::UID_NAME
|
31
|
+
puts "Finding object from #{table_name} with #{uid_name} = '#{id}'."
|
32
|
+
query = "SELECT * FROM #{schema_name}.#{table_name} where #{uid_name} = $1"
|
33
|
+
values = [id]
|
34
|
+
|
35
|
+
result = EnrichmentDb.request(schema_name, query, values).collect do |record|
|
36
|
+
record
|
37
|
+
end
|
38
|
+
|
39
|
+
format_result(result)
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.all(table_name, schema_name = nil)
|
43
|
+
schema_name = schema_name || self::DATABASE_NAME
|
44
|
+
puts "Finding all objects from #{table_name}"
|
45
|
+
query = "SELECT * FROM #{schema_name}.#{table_name}"
|
46
|
+
|
47
|
+
result = EnrichmentDb.request(schema_name, query).collect do |record|
|
48
|
+
record
|
49
|
+
end
|
50
|
+
|
51
|
+
format_result(result)
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.format_result(result)
|
55
|
+
if result.size > 0
|
56
|
+
puts "Found #{result.size} object/s"
|
57
|
+
result
|
58
|
+
else
|
59
|
+
puts "Nothing found"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
27
63
|
# Initializes a new Base object
|
28
64
|
#
|
29
65
|
# @param attrs [Hash]
|
@@ -35,6 +71,7 @@ module EnrichmentDb
|
|
35
71
|
end
|
36
72
|
|
37
73
|
require 'enrichment_db/geo'
|
74
|
+
require 'enrichment_db/language'
|
38
75
|
require 'enrichment_db/census'
|
39
76
|
require 'datum'
|
40
77
|
require 'db'
|
@@ -4,31 +4,9 @@ class EnrichmentDb::Census::Datum < EnrichmentDb::DatumModel
|
|
4
4
|
attr_reader :id
|
5
5
|
|
6
6
|
DATABASE_NAME = 'censis'
|
7
|
+
UID_NAME = 'region_id'
|
7
8
|
|
8
|
-
def self.
|
9
|
-
|
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
|
9
|
+
def self.format_result(result)
|
10
|
+
EnrichmentDb::Helper.format_geo_result(result)
|
33
11
|
end
|
34
12
|
end
|
data/lib/enrichment_db/datum.rb
CHANGED
@@ -5,16 +5,10 @@ class EnrichmentDb::Datum < EnrichmentDb::DatumModel
|
|
5
5
|
puts "Finding object from #{table_name} with condition #{condition}."
|
6
6
|
query = "SELECT * FROM #{schema_name}.#{table_name} where #{condition}"
|
7
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
|
8
|
+
result = EnrichmentDb.request(schema_name, query).collect do |record|
|
9
|
+
record
|
18
10
|
end
|
11
|
+
|
12
|
+
format_result(result)
|
19
13
|
end
|
20
14
|
end
|
data/lib/enrichment_db/helper.rb
CHANGED
@@ -14,4 +14,23 @@ module EnrichmentDb::Helper
|
|
14
14
|
h[k] = float_str_to_float(v)
|
15
15
|
end
|
16
16
|
end
|
17
|
+
|
18
|
+
def self.format_geo_result(result)
|
19
|
+
if result.size == 1
|
20
|
+
puts "Found object"
|
21
|
+
result = result.first
|
22
|
+
id = result['region_id']
|
23
|
+
result = EnrichmentDb::Helper.hash_float_str_to_float(result)
|
24
|
+
region_type = result['region_type']
|
25
|
+
region_type = region_type[0..-2]
|
26
|
+
region = EnrichmentDb::Geo.const_get(region_type).by_id(id.to_i)
|
27
|
+
region = EnrichmentDb::Helper.hash_float_str_to_float(region)
|
28
|
+
result['region'] = region
|
29
|
+
result
|
30
|
+
elsif result.size > 1
|
31
|
+
puts "More than 1 object found. Only wanted 1 object."
|
32
|
+
else
|
33
|
+
puts "Nothing found"
|
34
|
+
end
|
35
|
+
end
|
17
36
|
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.6
|
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-10-
|
11
|
+
date: 2015-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_json
|