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 +4 -4
- data/README.md +4 -2
- data/circle.yml +1 -0
- data/lib/avro_turf.rb +21 -5
- data/lib/avro_turf/schema_store.rb +1 -1
- data/lib/avro_turf/version.rb +1 -1
- data/spec/avro_turf_spec.rb +21 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d083d0ed365ddbd16e9a5e35acf698d50a7bd7d5
|
4
|
+
data.tar.gz: a297b67a288046a64932ad1e388cd39e4e7a106d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de5d8c415fcf16b731aa9cd4b69c5e8fc4e2243b092744a637a68a36494083330b861731f7567a4677ba0d149c60a8da6cbce38e40ee32771a84cdb9c687e843
|
7
|
+
data.tar.gz: 0e14a355dc76805b335d2fda8871671714a3843abe60886ef9d25ec5a4bad3270127d46386c2713e968a1eba47131753389b3cbb55531e8cafd0535ac14b2f39
|
data/README.md
CHANGED
data/circle.yml
CHANGED
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
|
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
|
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
|
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!
|
data/lib/avro_turf/version.rb
CHANGED
data/spec/avro_turf_spec.rb
CHANGED
@@ -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
|
+
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-
|
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.
|
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
|