csvhuman 1.0.0 → 1.0.1

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: 34cc6d7eb412bf02188cde5b26201cc1e5c8a46c
4
- data.tar.gz: 4c04e8bc8678b19923a42f05d11114389ff78ab5
3
+ metadata.gz: d8038f1824b8772b53f57f7dc00c67ca902fd872
4
+ data.tar.gz: a95430def44f77428a32b4ceb80f12c96d078199
5
5
  SHA512:
6
- metadata.gz: 404dc263217830a3dfc68e15114bd3e45a406774638574106708fed616a1fa9245becd6cab9a54889ef8335bffc0be5ca9324bb15779510fd4a5eea365156ecd
7
- data.tar.gz: 8689b1d38c6edade697583786e595332c5a496285c778967ad8a35557290d0b08a62056b75fc194d995f571220e4836eadcbf6d5f9083ec92cc5d97b3877d9cd
6
+ metadata.gz: a2ee778e98288478930b92cb066b8d8ee65da61dca3d911e3f3de12c5ee3a0d0f3451db5ea1426c1c59a5a8cd6fc9ecb2bb509d8982e79b1ff0ce8d8c353765e
7
+ data.tar.gz: e02189132d596e2b5abc654f55c9dcf925ed96d0d5008b8b2eba956cdfc0044827c374409e2e22e45c95791ed5db256c441210a36d81172c53f3178007f8cc40
@@ -9,8 +9,13 @@ lib/csvhuman/converter.rb
9
9
  lib/csvhuman/reader.rb
10
10
  lib/csvhuman/tag.rb
11
11
  lib/csvhuman/version.rb
12
+ test/data/sample1.csv
13
+ test/data/sample2.csv
14
+ test/data/sample3.csv
15
+ test/data/sample4.csv
12
16
  test/data/test.csv
13
17
  test/helper.rb
14
18
  test/test_header_converter.rb
15
19
  test/test_reader.rb
20
+ test/test_samples.rb
16
21
  test/test_tags.rb
@@ -61,16 +61,22 @@ class Tag
61
61
 
62
62
  if name == 'date'
63
63
  Date
64
- elsif ['affected', 'inneed', 'targeted', 'reached'].include?( name )
64
+ ## todo/fix: add more well-known names with num required!!!
65
+ elsif ['affected', 'inneed', 'targeted', 'reached', 'population'].include?( name )
65
66
  Integer
66
67
  else
67
68
  ## check attributes
68
69
  if attributes.nil? || attributes.empty?
69
70
  String ## assume (default to) string
70
- elsif attributes.include?( 'num' )
71
+ elsif attributes.include?( 'num' ) ||
72
+ attributes.include?( 'id') ## assume id is (always) a rowid - why? why not?
71
73
  Integer
72
74
  elsif attributes.include?( 'date' ) ### todo/check: exists +date?
73
75
  Date
76
+ elsif name == 'geo' && (attributes.include?('lat') ||
77
+ attributes.include?('lon') ||
78
+ attributes.include?('elevation'))
79
+ Float
74
80
  elsif attributes.include?( 'killed' ) ||
75
81
  attributes.include?( 'injured' ) ||
76
82
  attributes.include?( 'infected' ) ||
@@ -151,12 +157,17 @@ class Tag
151
157
  def typecast( value ) ## use convert or call - why? why not?
152
158
  if @type == Integer
153
159
  conv_to_i( value )
160
+ elsif @type == Float
161
+ conv_to_f( value )
162
+ elsif @type == Date
163
+ conv_to_date( value )
154
164
  else ## assume String
155
165
  # pass through as is
156
166
  value
157
167
  end
158
168
  end
159
169
 
170
+
160
171
  private
161
172
  def conv_to_i( value )
162
173
  if value.nil? || value.empty?
@@ -166,7 +177,25 @@ private
166
177
  end
167
178
  end
168
179
 
180
+ def conv_to_f( value )
181
+ if value.nil? || value.empty?
182
+ nil ## return nil - why? why not?
183
+ else
184
+ ## todo/fix: add support for NaN, Inf, -Inf etc.
185
+ ## how to deal with conversion errors (throw exception? ignore? NaN? why? why not?)
186
+ Float( value )
187
+ end
188
+ end
169
189
 
190
+ def conv_to_date( value )
191
+ if value.nil? || value.empty?
192
+ nil ## return nil - why? why not?
193
+ else
194
+ ## todo/fix: add support for more formats
195
+ ## how to deal with conversion errors (throw exception? ignore? why? why not?)
196
+ Date.strptime( value, "%Y-%m-%d" )
197
+ end
198
+ end
170
199
  end # class Tag
171
200
 
172
201
 
@@ -5,7 +5,7 @@ class CsvHuman
5
5
 
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- PATCH = 0
8
+ PATCH = 1
9
9
  VERSION = [MAJOR,MINOR,PATCH].join('.')
10
10
 
11
11
 
@@ -0,0 +1,10 @@
1
+ %%%%%%%%%%%%%%%%%%%%%
2
+ % sample from HXL tagging conventions (version 1.1)
3
+ % see http://hxlstandard.org/standard/1_1final/tagging/
4
+
5
+ CAMP INFORMATION, , NEEDS
6
+ LOCATION NAME, LOCATION CODE, NUMBER AFFECTED
7
+ #loc +name, #loc +code, #affected
8
+ Camp A, 01000001, 2000
9
+ Camp B, 01000002, 750
10
+ Camp C, 01000003, 1920
@@ -0,0 +1,7 @@
1
+ %%%%%%%%%%%%%%%%%%%%%
2
+ % sample from HXL tagging conventions (version 1.1)
3
+ % see http://hxlstandard.org/standard/1_1final/tagging/
4
+
5
+ #event+id, #affected+killed, #region, #meta+source+reliability, #date+reported, #geo+lat, #geo+lon
6
+ 1, 1, Mediterranean, Verified, 2015-11-05, 36.891500, 27.287700
7
+ 3, 1, Central America incl. Mexico, Partially Verified, 2015-11-03, 15.956400, -93.663099
@@ -0,0 +1,11 @@
1
+ %%%%%%%%%%%%%%%%%%%%%
2
+ % sample from HXL tagging conventions (version 1.1)
3
+ % see http://hxlstandard.org/standard/1_1final/tagging/
4
+
5
+ P-CODE 1, P-CODE 2, P-CODE 3
6
+ #loc+code, #loc+code, #loc+code
7
+ 020503
8
+ 060107, 060108
9
+ 173219
10
+ 530012
11
+ 530013, 530015, 279333
@@ -0,0 +1,8 @@
1
+ %%%%%%%%%%%%%%%%%%%%%
2
+ % sample from HXL tagging conventions (version 1.1)
3
+ % see http://hxlstandard.org/standard/1_1final/tagging/
4
+
5
+ REGION, 2008, 2009, 2010, 2011
6
+ #adm1 +name, #affected+label, #affected+label, #affected+label, #affected+label
7
+ Coast District, 0, 30, 100, 250
8
+ Mountain District, 15, 75, 30, 45
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # to run use
5
+ # ruby -I ./lib -I ./test test/test_samples.rb
6
+
7
+
8
+ require 'helper'
9
+
10
+ class TestSamples < MiniTest::Test
11
+
12
+
13
+ def test_sample1
14
+ recs = CsvHuman.read( "#{CsvHuman.test_data_dir}/sample1.csv" )
15
+ ## pp recs
16
+ assert_equal [{"loc+name"=>"Camp A", "loc+code"=>"01000001", "affected"=>2000},
17
+ {"loc+name"=>"Camp B", "loc+code"=>"01000002", "affected"=>750},
18
+ {"loc+name"=>"Camp C", "loc+code"=>"01000003", "affected"=>1920}], recs
19
+ end
20
+
21
+ def test_sample2
22
+ recs = CsvHuman.read( "#{CsvHuman.test_data_dir}/sample2.csv" )
23
+ ## pp recs
24
+ assert_equal [{"event+id"=>1,
25
+ "affected+killed"=>1,
26
+ "region"=>"Mediterranean",
27
+ "meta+reliability+source"=>"Verified",
28
+ "date+reported"=>Date.new( 2015, 11, 5 ),
29
+ "geo+lat"=>36.8915,
30
+ "geo+lon"=>27.2877},
31
+ {"event+id"=>3,
32
+ "affected+killed"=>1,
33
+ "region"=>"Central America incl. Mexico",
34
+ "meta+reliability+source"=>"Partially Verified",
35
+ "date+reported"=>Date.new( 2015, 11, 3 ),
36
+ "geo+lat"=>15.9564,
37
+ "geo+lon"=>-93.663099}], recs
38
+ end
39
+
40
+ def test_sample3
41
+ recs = CsvHuman.read( "#{CsvHuman.test_data_dir}/sample3.csv" )
42
+ ## pp recs
43
+ assert_equal [{"loc+code"=>["020503", nil, nil]},
44
+ {"loc+code"=>["060107", "060108", nil]},
45
+ {"loc+code"=>["173219", nil, nil]},
46
+ {"loc+code"=>["530012", nil, nil]},
47
+ {"loc+code"=>["530013", "530015", "279333"]}], recs
48
+ end
49
+
50
+ def test_sample4
51
+ recs = CsvHuman.read( "#{CsvHuman.test_data_dir}/sample4.csv" )
52
+ ## pp recs
53
+ assert_equal [{"adm1+name"=>"Coast District", "affected+label"=>[0, 30, 100, 250]},
54
+ {"adm1+name"=>"Mountain District", "affected+label"=>[15, 75, 30, 45]}], recs
55
+ end
56
+
57
+
58
+ end # class TestSamples
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csvhuman
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-11 00:00:00.000000000 Z
11
+ date: 2018-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: csvreader
@@ -74,10 +74,15 @@ files:
74
74
  - lib/csvhuman/reader.rb
75
75
  - lib/csvhuman/tag.rb
76
76
  - lib/csvhuman/version.rb
77
+ - test/data/sample1.csv
78
+ - test/data/sample2.csv
79
+ - test/data/sample3.csv
80
+ - test/data/sample4.csv
77
81
  - test/data/test.csv
78
82
  - test/helper.rb
79
83
  - test/test_header_converter.rb
80
84
  - test/test_reader.rb
85
+ - test/test_samples.rb
81
86
  - test/test_tags.rb
82
87
  homepage: https://github.com/csvreader/csvhuman
83
88
  licenses: