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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ceeab3783a59c54f74ed47b5068319a203955e2c
4
- data.tar.gz: b0fffe8b8f5856086423784b8fac993e4803517c
3
+ metadata.gz: 8e761011e4c6ae8f32a932c1c349730ce112ef66
4
+ data.tar.gz: 39cd9b7f3bd62161f202e13fe3e33e576292d0a7
5
5
  SHA512:
6
- metadata.gz: 454bc5d8e9e609175f6a9292d7b3268a21dcc3c5715617e08bb12c2a9ff6ef9e88c64e98880bd02d69d57b6fd7ef7c9aea08f6ae91d0740e50005dcbbc82b4b3
7
- data.tar.gz: 5f16865505cfb6c740654684d4ccf4905a3d936049e1f73146d8ee710fd9282573405e15beadea9f6eee8a107e14e627bb0c5874829a14d7ef6bc4b09408c7f6
6
+ metadata.gz: 2d67d7ba3d10a5bac786ae0d3353fc8e32a3850284eac00306ff14bdfbbd026e6fbd7edba7445e6ffa015ff2157f838640e0ec1ee93beb6025a1bd1df3877cea
7
+ data.tar.gz: cbced204ad097551a74f375c5b0a597fc5f063a902ecc83392ae136891b2e6cc86a219cba0ee45824d614ba684f2259be354c836f0ce30c8e9024caa8d69646a
@@ -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.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
9
+ def self.format_result(result)
10
+ EnrichmentDb::Helper.format_geo_result(result)
33
11
  end
34
12
  end
@@ -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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module EnrichmentDb
2
- VERSION = '0.1.5'
2
+ VERSION = '0.1.6'
3
3
  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.5
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-06 00:00:00.000000000 Z
11
+ date: 2015-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json