schema_registry 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0ae27f7cfce0bff061ce4ac247a11bae184d2bbd
4
+ data.tar.gz: aa3fbeeeb5a04273d70588ee319d730d0644fe2a
5
+ SHA512:
6
+ metadata.gz: eba53efa53ede87f404ed70cf7405f5af0ccc10650796dd19038b8100bab76d16927b7e1bf0cfd2b6fe24b9780683b5aea38618b1e4ec71a640ae2231a8e6c91
7
+ data.tar.gz: 572a7592030e7e576da373f112fa9961603dad0037072410e100bc5488899adb0d925215a85bbf5d0a636952a07d5b8660cb280674af17c36ded03226ca5be3c
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in schema_registry.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Willem van Bergen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,32 @@
1
+ # SchemaRegistry
2
+
3
+ Ruby client for Confluent Inc.'s schema-registry. The schema-registry holds AVRO schemas for different
4
+ subjects, and can ensure backward and/or forward compatiblity between different schema versions.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'schema_registry'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install schema_registry
21
+
22
+ ## Usage
23
+
24
+ TODO: Write usage instructions here
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it ( https://github.com/[my-github-username]/schema_registry/fork )
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,18 @@
1
+ module SchemaRegistry
2
+
3
+ module Compatibility
4
+ FORWARD = "FORWARD".freeze
5
+ BACKWARD = "BACKWARD".freeze
6
+ FULL = "FULL".freeze
7
+ NONE = "NONE".freeze
8
+
9
+ LEVELS = [NONE, FORWARD, BACKWARD, FULL].freeze
10
+ end
11
+
12
+ class Error < ::StandardError
13
+ end
14
+ end
15
+
16
+ require 'schema_registry/client'
17
+ require 'schema_registry/subject'
18
+ require 'schema_registry/version'
@@ -0,0 +1,71 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module SchemaRegistry
5
+ class ResponseError < Error
6
+ attr_reader :code
7
+
8
+ def initialize(code, message)
9
+ @code = code
10
+ super(message)
11
+ end
12
+ end
13
+
14
+ class Client
15
+
16
+ attr_reader :endpoint, :username, :password
17
+
18
+ def initialize(endpoint, username = nil, password = nil)
19
+ @endpoint = URI(endpoint)
20
+ @username, @password = username, password
21
+ end
22
+
23
+ def schema(id)
24
+ request(:get, "/schemas/ids/#{id}")['schema']
25
+ end
26
+
27
+ def subjects
28
+ data = request(:get, "/subjects")
29
+ data.map { |subject| SchemaRegistry::Subject.new(self, subject) }
30
+ end
31
+
32
+ def subject(name)
33
+ SchemaRegistry::Subject.new(self, name)
34
+ end
35
+
36
+ def default_compatibility_level
37
+ request(:get, "/config")["compatibilityLevel"]
38
+ end
39
+
40
+ def default_compatibility_level=(level)
41
+ request(:put, "/config", compatibility: level)
42
+ end
43
+
44
+ def request(method, path, body = nil)
45
+ Net::HTTP.start(endpoint.host, endpoint.port, use_ssl: endpoint.scheme == 'https') do |http|
46
+ request_class = case method
47
+ when :get; Net::HTTP::Get
48
+ when :post; Net::HTTP::Post
49
+ when :put; Net::HTTP::Put
50
+ when :delete; Net::HTTP::Delete
51
+ else raise ArgumentError, "Unsupported request method"
52
+ end
53
+
54
+ request = request_class.new(path)
55
+ request.basic_auth(username, password) if username && password
56
+ request['Accept'] = "application/vnd.schemaregistry.v1+json"
57
+ if body
58
+ request['Content-Type'] = "application/json"
59
+ request.body = JSON.dump(body)
60
+ end
61
+
62
+ response = http.request(request)
63
+ response_data = JSON.parse(response.body)
64
+ case response
65
+ when Net::HTTPOK; response_data
66
+ else raise SchemaRegistry::ResponseError.new(response_data['error_code'], response_data['message'])
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,40 @@
1
+ module SchemaRegistry
2
+
3
+ class Subject
4
+ attr_reader :client, :name
5
+
6
+ def initialize(client, name)
7
+ @client, @name = client, name
8
+ end
9
+
10
+ def versions
11
+ client.request(:get, "/subjects/#{name}/versions")
12
+ end
13
+
14
+ def version(version)
15
+ client.request(:get, "/subjects/#{name}/versions/#{version}")
16
+ end
17
+
18
+ def verify_schema(schema_json)
19
+ client.request(:post, "/subjects/#{name}", schema: schema_json)
20
+ end
21
+
22
+ def update_schema(schema_json)
23
+ client.request(:post, "/subjects/#{name}/versions", schema: schema_json)["id"]
24
+ end
25
+
26
+ def compatibility_level
27
+ response = client.request(:get, "/config/#{name}")
28
+ response["compatibilityLevel"]
29
+ end
30
+
31
+ def compatibility_level=(level)
32
+ response = client.request(:put, "/config/#{name}", compatibility: level)
33
+ end
34
+
35
+ def compatible?(schema, version = "latest")
36
+ response = client.request(:post, "/compatibility/subjects/#{name}/versions/#{version}", schema: schema)
37
+ response["is_compatible"]
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module SchemaRegistry
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'schema_registry/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "schema_registry"
8
+ spec.version = SchemaRegistry::VERSION
9
+ spec.authors = ["Willem van Bergen"]
10
+ spec.email = ["willem@railsdoctors.com"]
11
+ spec.summary = %q{Ruby client for Confluent Inc.'s schema-registry}
12
+ spec.description = %q{Ruby client for Confluent Inc.'s schema-registry. The schema-registry holds AVRO schemas for different subjects, and can ensure backward and/or forward compatiblity between different schema versions.}
13
+ spec.homepage = "http://confluent.io/docs/current/schema-registry/docs/index.html"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ spec.add_development_dependency "avro", "~> 1.7"
25
+ end
@@ -0,0 +1,13 @@
1
+ {
2
+ "namespace": "org.vanbergen.test",
3
+ "name": "Test",
4
+ "doc": "Only used for testing",
5
+ "type": "record",
6
+ "fields": [
7
+ {
8
+ "type": "string",
9
+ "name": "data"
10
+ }
11
+ ],
12
+ "metafata_field": "present"
13
+ }
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ class SchemaRegistryTest < Minitest::Test
4
+
5
+ def setup
6
+ raise "The SCHEMA_REGISTRY_URI environment variable must be set" if ENV['SCHEMA_REGISTRY_URI'].nil?
7
+ @client = SchemaRegistry::Client.new(ENV['SCHEMA_REGISTRY_URI'])
8
+ end
9
+
10
+ def test_global_compatibility_level
11
+ old_level = @client.default_compatibility_level
12
+ assert_equal SchemaRegistry::Compatibility::BACKWARD, old_level
13
+
14
+ @client.default_compatibility_level = SchemaRegistry::Compatibility::FULL
15
+
16
+ current_level = @client.default_compatibility_level
17
+ assert_equal current_level, SchemaRegistry::Compatibility::FULL
18
+ ensure
19
+ @client.default_compatibility_level = old_level
20
+ end
21
+
22
+ def test_register_schema_for_subject
23
+ schema = schema_fixture('test', 1)
24
+
25
+ subject = @client.subject('test.schema_registry')
26
+ schema_id = subject.update_schema(schema)
27
+ assert schema_id > 0
28
+
29
+ assert_equal ['test.schema_registry'], @client.subjects.map(&:name)
30
+
31
+ registered_schema = @client.schema(schema_id)
32
+ schema_info = subject.verify_schema(schema)
33
+ assert_equal schema_id, schema_info['id']
34
+
35
+ assert_equal Avro::Schema.parse(schema), Avro::Schema.parse(registered_schema)
36
+
37
+ parsed_schema = JSON.parse(registered_schema)
38
+ assert_equal 'Only used for testing', parsed_schema['doc']
39
+ assert_equal 'present', parsed_schema['metafata_field']
40
+ end
41
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+ require 'minitest/autorun'
3
+ require 'minitest/pride'
4
+
5
+ require 'pp'
6
+ require 'avro'
7
+ require 'schema_registry'
8
+
9
+ def schema_fixture(name, version = 1, suffix = nil)
10
+ schema_dirname = File.expand_path("../fixtures/schemas", __FILE__)
11
+ schema_filename = "#{name}-v#{version}"
12
+ schema_filename += suffix ? "-#{suffix}.avsc" : ".avsc"
13
+ File.read(File.join(schema_dirname, schema_filename))
14
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: schema_registry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Willem van Bergen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-13 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: avro
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ description: Ruby client for Confluent Inc.'s schema-registry. The schema-registry
70
+ holds AVRO schemas for different subjects, and can ensure backward and/or forward
71
+ compatiblity between different schema versions.
72
+ email:
73
+ - willem@railsdoctors.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .gitignore
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/schema_registry.rb
84
+ - lib/schema_registry/client.rb
85
+ - lib/schema_registry/subject.rb
86
+ - lib/schema_registry/version.rb
87
+ - schema_registry.gemspec
88
+ - test/fixtures/schemas/test-v1.avsc
89
+ - test/functional/schema_registry_test.rb
90
+ - test/test_helper.rb
91
+ homepage: http://confluent.io/docs/current/schema-registry/docs/index.html
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.0.14
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Ruby client for Confluent Inc.'s schema-registry
115
+ test_files:
116
+ - test/fixtures/schemas/test-v1.avsc
117
+ - test/functional/schema_registry_test.rb
118
+ - test/test_helper.rb