tableschema 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +14 -0
  5. data/CHANGELOG.md +31 -0
  6. data/CODE_OF_CONDUCT.md +49 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +274 -0
  10. data/Rakefile +6 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/etc/schemas/geojson.json +209 -0
  14. data/etc/schemas/json-table-schema.json +102 -0
  15. data/lib/tableschema.rb +42 -0
  16. data/lib/tableschema/constraints/constraints.rb +76 -0
  17. data/lib/tableschema/constraints/enum.rb +14 -0
  18. data/lib/tableschema/constraints/max_length.rb +15 -0
  19. data/lib/tableschema/constraints/maximum.rb +14 -0
  20. data/lib/tableschema/constraints/min_length.rb +15 -0
  21. data/lib/tableschema/constraints/minimum.rb +14 -0
  22. data/lib/tableschema/constraints/pattern.rb +14 -0
  23. data/lib/tableschema/constraints/required.rb +32 -0
  24. data/lib/tableschema/data.rb +60 -0
  25. data/lib/tableschema/exceptions.rb +28 -0
  26. data/lib/tableschema/field.rb +41 -0
  27. data/lib/tableschema/helpers.rb +48 -0
  28. data/lib/tableschema/infer.rb +143 -0
  29. data/lib/tableschema/model.rb +73 -0
  30. data/lib/tableschema/schema.rb +36 -0
  31. data/lib/tableschema/table.rb +51 -0
  32. data/lib/tableschema/types/any.rb +23 -0
  33. data/lib/tableschema/types/array.rb +37 -0
  34. data/lib/tableschema/types/base.rb +54 -0
  35. data/lib/tableschema/types/boolean.rb +35 -0
  36. data/lib/tableschema/types/date.rb +56 -0
  37. data/lib/tableschema/types/datetime.rb +63 -0
  38. data/lib/tableschema/types/geojson.rb +38 -0
  39. data/lib/tableschema/types/geopoint.rb +56 -0
  40. data/lib/tableschema/types/integer.rb +35 -0
  41. data/lib/tableschema/types/null.rb +37 -0
  42. data/lib/tableschema/types/number.rb +60 -0
  43. data/lib/tableschema/types/object.rb +37 -0
  44. data/lib/tableschema/types/string.rb +64 -0
  45. data/lib/tableschema/types/time.rb +55 -0
  46. data/lib/tableschema/validate.rb +54 -0
  47. data/lib/tableschema/version.rb +3 -0
  48. data/tableschema.gemspec +32 -0
  49. metadata +231 -0
@@ -0,0 +1,35 @@
1
+ module TableSchema
2
+ module Types
3
+ class Integer < Base
4
+
5
+ def name
6
+ 'integer'
7
+ end
8
+
9
+ def self.supported_constraints
10
+ [
11
+ 'required',
12
+ 'pattern',
13
+ 'enum',
14
+ 'minimum',
15
+ 'maximum',
16
+ ]
17
+ end
18
+
19
+ def type
20
+ ::Integer
21
+ end
22
+
23
+ def cast_default(value)
24
+ if value.is_a?(type)
25
+ value
26
+ else
27
+ Integer(value)
28
+ end
29
+ rescue ArgumentError
30
+ raise TableSchema::InvalidCast.new("#{value} is not a #{name}")
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ module TableSchema
2
+ module Types
3
+ class Null < Base
4
+
5
+ def name
6
+ 'null'
7
+ end
8
+
9
+ def self.supported_constraints
10
+ [
11
+ 'required',
12
+ 'pattern',
13
+ 'enum',
14
+ ]
15
+ end
16
+
17
+ def type
18
+ ::NilClass
19
+ end
20
+
21
+ def null_values
22
+ ['null', 'none', 'nil', 'nan', '-', '']
23
+ end
24
+
25
+ def cast_default(value)
26
+ if value.is_a?(type)
27
+ return value
28
+ elsif null_values.include?(value.to_s.downcase)
29
+ nil
30
+ else
31
+ raise TableSchema::InvalidCast.new("#{value} is not a #{name}")
32
+ end
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,60 @@
1
+ module TableSchema
2
+ module Types
3
+ class Number < Base
4
+
5
+ def name
6
+ 'number'
7
+ end
8
+
9
+ def self.supported_constraints
10
+ [
11
+ 'required',
12
+ 'pattern',
13
+ 'enum',
14
+ 'minimum',
15
+ 'maximum',
16
+ ]
17
+ end
18
+
19
+ def type
20
+ ::Float
21
+ end
22
+
23
+ def currency_symbols
24
+ ISO4217::Currency.currencies.to_a.map { |c| Regexp.escape(c.last.symbol) rescue nil }.delete_if { |s| s.nil? }
25
+ end
26
+
27
+ def cast_default(value)
28
+ return value if value.class == type
29
+ return Float(value) if value.class == ::Fixnum
30
+
31
+ value = preprocess_value(value)
32
+ return Float(value)
33
+ rescue ArgumentError
34
+ raise TableSchema::InvalidCast.new("#{value} is not a #{name}")
35
+ end
36
+
37
+ def cast_currency(value)
38
+ cast_default(value)
39
+ rescue TableSchema::InvalidCast
40
+ value = preprocess_value(value)
41
+ re = Regexp.new currency_symbols.join('|')
42
+ value.gsub!(re, '')
43
+ cast_default(value)
44
+ end
45
+
46
+ private
47
+
48
+ def preprocess_value(value)
49
+ group_char = @field.fetch('groupChar', ',')
50
+ decimal_char = @field.fetch('decimalChar', '.')
51
+ percent_char = /%|‰|‱|%|﹪|٪/
52
+ value.gsub(group_char, '')
53
+ .gsub(decimal_char, '.')
54
+ .gsub(percent_char, '')
55
+ .gsub(Regexp.new(currency_symbols.join '|'), '')
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,37 @@
1
+ module TableSchema
2
+ module Types
3
+ class Object < Base
4
+
5
+ def name
6
+ 'object'
7
+ end
8
+
9
+ def self.supported_constraints
10
+ [
11
+ 'required',
12
+ 'pattern',
13
+ 'enum',
14
+ 'minLength',
15
+ 'maxLength',
16
+ ]
17
+ end
18
+
19
+ def type
20
+ ::Hash
21
+ end
22
+
23
+ def cast_default(value)
24
+ return value if value.is_a?(type)
25
+ parsed = JSON.parse(value)
26
+ if parsed.is_a?(Hash)
27
+ return parsed
28
+ else
29
+ raise TableSchema::InvalidObjectType.new("#{value} is not a valid object")
30
+ end
31
+ rescue
32
+ raise TableSchema::InvalidObjectType.new("#{value} is not a valid object")
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,64 @@
1
+ module TableSchema
2
+ module Types
3
+ class String < Base
4
+
5
+ def name
6
+ 'string'
7
+ end
8
+
9
+ def self.supported_constraints
10
+ [
11
+ 'required',
12
+ 'pattern',
13
+ 'enum',
14
+ 'minLength',
15
+ 'maxLength',
16
+ ]
17
+ end
18
+
19
+ def type
20
+ ::String
21
+ end
22
+
23
+ def email_pattern
24
+ /[^@]+@[^@]+\.[^@]+/
25
+ end
26
+
27
+ def cast_default(value)
28
+ if value.is_a?(type)
29
+ return value
30
+ else
31
+ raise TableSchema::InvalidCast.new("#{value} is not a #{name}")
32
+ end
33
+ end
34
+
35
+ def cast_email(value)
36
+ value = cast_default(value)
37
+ if (value =~ email_pattern) != nil
38
+ value
39
+ else
40
+ raise TableSchema::InvalidEmail.new("#{value} is not a valid email")
41
+ end
42
+ end
43
+
44
+ def cast_uri(value)
45
+ value = cast_default(value)
46
+ if (value =~ URI::regexp) != nil
47
+ value
48
+ else
49
+ raise TableSchema::InvalidURI.new("#{value} is not a valid uri")
50
+ end
51
+ end
52
+
53
+ def cast_uuid(value)
54
+ value = cast_default(value)
55
+ if UUID.validate(value)
56
+ value
57
+ else
58
+ raise TableSchema::InvalidUUID.new("#{value} is not a valid UUID")
59
+ end
60
+ end
61
+
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,55 @@
1
+ module TableSchema
2
+ module Types
3
+ class Time < Base
4
+
5
+ def name
6
+ 'time'
7
+ end
8
+
9
+ def self.supported_constraints
10
+ [
11
+ 'required',
12
+ 'pattern',
13
+ 'enum',
14
+ 'minimum',
15
+ 'maximum',
16
+ ]
17
+ end
18
+
19
+ def type
20
+ ::Tod::TimeOfDay
21
+ end
22
+
23
+ def iso8601
24
+ '%H:%M:%S'
25
+ end
26
+
27
+ def cast_default(value)
28
+ @format_string = iso8601
29
+ cast_fmt(value)
30
+ end
31
+
32
+ def cast_any(value)
33
+ return value if value.is_a?(type)
34
+
35
+ begin
36
+ return ::Tod::TimeOfDay.parse(value)
37
+ rescue ArgumentError
38
+ raise TableSchema::InvalidTimeType.new("#{value} is not a valid time")
39
+ end
40
+ end
41
+
42
+ def cast_fmt(value)
43
+ return value if value.is_a?(type)
44
+
45
+ begin
46
+ time = ::Time.strptime(value, @format_string)
47
+ return time.to_time_of_day
48
+ rescue ArgumentError, TypeError
49
+ raise TableSchema::InvalidTimeType.new("#{value} is not a valid time")
50
+ end
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,54 @@
1
+ module TableSchema
2
+ module Validate
3
+
4
+ attr_reader :messages
5
+
6
+ def load_validator!
7
+ filepath = File.join(File.dirname(__FILE__), '..', '..', 'etc', 'schemas', 'json-table-schema.json')
8
+ @validator ||= JSON.parse(File.read filepath)
9
+ end
10
+
11
+ def valid?
12
+ validate
13
+ @messages.count == 0
14
+ end
15
+
16
+ def validate
17
+ @messages = JSON::Validator.fully_validate(@validator, self)
18
+ check_primary_keys
19
+ check_foreign_keys
20
+ end
21
+
22
+ private
23
+
24
+ def check_primary_keys
25
+ return if self['primaryKey'].nil?
26
+ primary_keys.each { |pk| check_field_value(pk, 'primaryKey') }
27
+ end
28
+
29
+ def check_foreign_keys
30
+ return if self['foreignKeys'].nil?
31
+ self['foreignKeys'].each do |keys|
32
+ foreign_key_fields(keys).each { |fk| check_field_value(fk, 'foreignKey.fields') }
33
+ add_error("A JSON Table Schema foreignKey.fields must contain the same number entries as foreignKey.reference.fields.") if field_count_mismatch?(keys)
34
+ end
35
+ end
36
+
37
+ def check_field_value(key, type)
38
+ add_error("The JSON Table Schema #{type} value `#{key}` is not found in any of the schema's field names") if headers.select { |f| key == f }.count == 0
39
+ end
40
+
41
+ def foreign_key_fields(keys)
42
+ [keys['fields']].flatten
43
+ end
44
+
45
+ def field_count_mismatch?(keys)
46
+ keys['reference'] &&([keys['fields']].flatten.count != [keys['reference']['fields']].flatten.count)
47
+ end
48
+
49
+ def add_error(error)
50
+ @messages << error
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module TableSchema
2
+ VERSION = "0.3.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tableschema/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tableschema"
8
+ spec.version = TableSchema::VERSION
9
+ spec.authors = ["Open Knowledge Foundation"]
10
+ spec.email = ["info@okfn.org"]
11
+
12
+ spec.summary = "A Ruby library for working with Table Schema. Formerly known as 'jsontableschema'"
13
+ spec.homepage = "https://github.com/frictionlessdata/tableschema-rb"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "pry", "~> 0.10.0"
25
+ spec.add_development_dependency "webmock", "~> 2.3.0"
26
+ spec.add_development_dependency "coveralls", "~> 0.8.13"
27
+
28
+ spec.add_dependency "json-schema", "~> 2.6.0"
29
+ spec.add_dependency "uuid", "~> 2.3.8"
30
+ spec.add_dependency "currencies", "~> 0.4.2"
31
+ spec.add_dependency "tod", "~> 2.1.0"
32
+ end
metadata ADDED
@@ -0,0 +1,231 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tableschema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Open Knowledge Foundation
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.3.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.3.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.8.13
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.8.13
97
+ - !ruby/object:Gem::Dependency
98
+ name: json-schema
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.6.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.6.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: uuid
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 2.3.8
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 2.3.8
125
+ - !ruby/object:Gem::Dependency
126
+ name: currencies
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.4.2
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.4.2
139
+ - !ruby/object:Gem::Dependency
140
+ name: tod
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 2.1.0
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 2.1.0
153
+ description:
154
+ email:
155
+ - info@okfn.org
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".rspec"
162
+ - ".travis.yml"
163
+ - CHANGELOG.md
164
+ - CODE_OF_CONDUCT.md
165
+ - Gemfile
166
+ - LICENSE.txt
167
+ - README.md
168
+ - Rakefile
169
+ - bin/console
170
+ - bin/setup
171
+ - etc/schemas/geojson.json
172
+ - etc/schemas/json-table-schema.json
173
+ - lib/tableschema.rb
174
+ - lib/tableschema/constraints/constraints.rb
175
+ - lib/tableschema/constraints/enum.rb
176
+ - lib/tableschema/constraints/max_length.rb
177
+ - lib/tableschema/constraints/maximum.rb
178
+ - lib/tableschema/constraints/min_length.rb
179
+ - lib/tableschema/constraints/minimum.rb
180
+ - lib/tableschema/constraints/pattern.rb
181
+ - lib/tableschema/constraints/required.rb
182
+ - lib/tableschema/data.rb
183
+ - lib/tableschema/exceptions.rb
184
+ - lib/tableschema/field.rb
185
+ - lib/tableschema/helpers.rb
186
+ - lib/tableschema/infer.rb
187
+ - lib/tableschema/model.rb
188
+ - lib/tableschema/schema.rb
189
+ - lib/tableschema/table.rb
190
+ - lib/tableschema/types/any.rb
191
+ - lib/tableschema/types/array.rb
192
+ - lib/tableschema/types/base.rb
193
+ - lib/tableschema/types/boolean.rb
194
+ - lib/tableschema/types/date.rb
195
+ - lib/tableschema/types/datetime.rb
196
+ - lib/tableschema/types/geojson.rb
197
+ - lib/tableschema/types/geopoint.rb
198
+ - lib/tableschema/types/integer.rb
199
+ - lib/tableschema/types/null.rb
200
+ - lib/tableschema/types/number.rb
201
+ - lib/tableschema/types/object.rb
202
+ - lib/tableschema/types/string.rb
203
+ - lib/tableschema/types/time.rb
204
+ - lib/tableschema/validate.rb
205
+ - lib/tableschema/version.rb
206
+ - tableschema.gemspec
207
+ homepage: https://github.com/frictionlessdata/tableschema-rb
208
+ licenses:
209
+ - MIT
210
+ metadata: {}
211
+ post_install_message:
212
+ rdoc_options: []
213
+ require_paths:
214
+ - lib
215
+ required_ruby_version: !ruby/object:Gem::Requirement
216
+ requirements:
217
+ - - ">="
218
+ - !ruby/object:Gem::Version
219
+ version: '0'
220
+ required_rubygems_version: !ruby/object:Gem::Requirement
221
+ requirements:
222
+ - - ">="
223
+ - !ruby/object:Gem::Version
224
+ version: '0'
225
+ requirements: []
226
+ rubyforge_project:
227
+ rubygems_version: 2.6.12
228
+ signing_key:
229
+ specification_version: 4
230
+ summary: A Ruby library for working with Table Schema. Formerly known as 'jsontableschema'
231
+ test_files: []