avro_turf 0.4.1 → 0.5.0

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: cfae5e5b40637dc42042fd526172b9265d7e6ffe
4
- data.tar.gz: a8df9a341f18727f0617eb82b0ab4831a51c0ac8
3
+ metadata.gz: d083d0ed365ddbd16e9a5e35acf698d50a7bd7d5
4
+ data.tar.gz: a297b67a288046a64932ad1e388cd39e4e7a106d
5
5
  SHA512:
6
- metadata.gz: abdaddbf56e6b7c34bce4f10118838847a09ec6ffdb850187e78efd2319b251db4016d6773ac08686ab86f7deba9070a3553f7ddd26f76c41a2a6dec8f4e0639
7
- data.tar.gz: ad8734818133df66c3717a92143e3dae81eba436a1bb3095df5b46fef9f3053f79419471488707e3c4f7fe3ec6257eabf2a0aa0724de4d83970b40c3376af81c
6
+ metadata.gz: de5d8c415fcf16b731aa9cd4b69c5e8fc4e2243b092744a637a68a36494083330b861731f7567a4677ba0d149c60a8da6cbce38e40ee32771a84cdb9c687e843
7
+ data.tar.gz: 0e14a355dc76805b335d2fda8871671714a3843abe60886ef9d25ec5a4bad3270127d46386c2713e968a1eba47131753389b3cbb55531e8cafd0535ac14b2f39
data/README.md CHANGED
@@ -81,8 +81,10 @@ In the example above, the `person` schema references the `address` schema, even
81
81
  // person_list.avsc
82
82
  {
83
83
  "name": "person_list",
84
- "type": "array",
85
- "items": "person"
84
+ "type": {
85
+ "type": "array",
86
+ "items": "person"
87
+ }
86
88
  }
87
89
  ```
88
90
 
data/circle.yml CHANGED
@@ -1,3 +1,4 @@
1
1
  machine:
2
2
  ruby:
3
3
  version: 2.2.0
4
+ version: 2.0.0
data/lib/avro_turf.rb CHANGED
@@ -9,16 +9,18 @@ class AvroTurf
9
9
  class SchemaError < Error; end
10
10
  class SchemaNotFoundError < Error; end
11
11
 
12
+ DEFAULT_SCHEMAS_PATH = "./schemas"
13
+
12
14
  # Create a new AvroTurf instance with the specified configuration.
13
15
  #
14
- # schemas_path - The String path to the root directory containing Avro schemas.
16
+ # schemas_path - The String path to the root directory containing Avro schemas (default: "./schemas").
15
17
  # namespace - The String namespace that should be used to qualify schema names (optional).
16
18
  # codec - The String name of a codec that should be used to compress messages (optional).
17
19
  #
18
20
  # Currently, the only valid codec name is `deflate`.
19
- def initialize(schemas_path:, namespace: nil, codec: nil)
21
+ def initialize(schemas_path: nil, namespace: nil, codec: nil)
20
22
  @namespace = namespace
21
- @schema_store = SchemaStore.new(path: schemas_path)
23
+ @schema_store = SchemaStore.new(path: schemas_path || DEFAULT_SCHEMAS_PATH)
22
24
  @codec = codec
23
25
  end
24
26
 
@@ -28,7 +30,7 @@ class AvroTurf
28
30
  # schema_name - The name of a schema in the `schemas_path`.
29
31
  #
30
32
  # Returns a String containing the encoded data.
31
- def encode(data, schema_name:, namespace: @namespace)
33
+ def encode(data, schema_name: nil, namespace: @namespace)
32
34
  stream = StringIO.new
33
35
 
34
36
  encode_to_stream(data, stream: stream, schema_name: schema_name, namespace: namespace)
@@ -44,7 +46,7 @@ class AvroTurf
44
46
  # stream - An IO object that the encoded data should be written to (optional).
45
47
  #
46
48
  # Returns nothing.
47
- def encode_to_stream(data, schema_name:, stream:, namespace: @namespace)
49
+ def encode_to_stream(data, schema_name: nil, stream: nil, namespace: @namespace)
48
50
  schema = @schema_store.find(schema_name, namespace)
49
51
  writer = Avro::IO::DatumWriter.new(schema)
50
52
 
@@ -81,6 +83,20 @@ class AvroTurf
81
83
  dr.first
82
84
  end
83
85
 
86
+ # Validates data against an Avro schema.
87
+ #
88
+ # data - The data that should be validated.
89
+ # schema - The String name of the schema that should be used to validate
90
+ # the data.
91
+ # namespace - The namespace of the Avro schema (optional).
92
+ #
93
+ # Returns true if the data is valid, false otherwise.
94
+ def valid?(data, schema_name: nil, namespace: @namespace)
95
+ schema = schema_name && @schema_store.find(schema_name, namespace)
96
+
97
+ Avro::Schema.validate(schema, data)
98
+ end
99
+
84
100
  # Loads all schema definition files in the `schemas_dir`.
85
101
  def load_schemas!
86
102
  @schema_store.load_schemas!
@@ -1,5 +1,5 @@
1
1
  class AvroTurf::SchemaStore
2
- def initialize(path:)
2
+ def initialize(path: nil)
3
3
  @path = path or raise "Please specify a schema path"
4
4
  @schemas = Hash.new
5
5
  end
@@ -1,3 +1,3 @@
1
1
  class AvroTurf
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -122,4 +122,25 @@ describe AvroTurf do
122
122
  expect(avro.decode_stream(stream)).to eq "hello"
123
123
  end
124
124
  end
125
+
126
+ describe "#valid?" do
127
+ before do
128
+ define_schema "message.avsc", <<-AVSC
129
+ {
130
+ "name": "message",
131
+ "type": "string"
132
+ }
133
+ AVSC
134
+ end
135
+
136
+ it "returns true if the datum matches the schema" do
137
+ datum = "hello"
138
+ expect(avro.valid?(datum, schema_name: "message")).to eq true
139
+ end
140
+
141
+ it "returns false if the datum does not match the schema" do
142
+ datum = 42
143
+ expect(avro.valid?(datum, schema_name: "message")).to eq false
144
+ end
145
+ end
125
146
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avro_turf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Schierbeck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-16 00:00:00.000000000 Z
11
+ date: 2015-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro
@@ -146,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  version: '0'
147
147
  requirements: []
148
148
  rubyforge_project:
149
- rubygems_version: 2.2.2
149
+ rubygems_version: 2.4.5.1
150
150
  signing_key:
151
151
  specification_version: 4
152
152
  summary: A library that makes it easier to use the Avro serialization format from