avrodrome 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3f7de094626cff1e717b4cc1373d35986cbc4cb22c1017a1567cf85396cee212
4
+ data.tar.gz: 4e061d33e75aa5d03bc69c60904239d1693cd357ab611daf6c087a3a1e713b16
5
+ SHA512:
6
+ metadata.gz: b0ad39cac524ccd311a1dbcf6684b056e7b2804bfffc8920247cc9881318274a8391ed8648a49c6ce6866991522ceb0b9d558b70c0b5e7de4b33852013381d2b
7
+ data.tar.gz: 72c2a01daa9f6214be11df8f62e3dd074d8e0168b9c1b27ab5230f4cfe8bf95cb4d700ae8760812dd8d82d08eaf17b2ad14c43c3acc4ac2d444f56a0c66d8940
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.0
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in avrodrome.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ avrodrome (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ concurrent-ruby (1.1.4)
11
+ diff-lcs (1.3)
12
+ faker (1.9.1)
13
+ i18n (>= 0.7)
14
+ i18n (1.5.1)
15
+ concurrent-ruby (~> 1.0)
16
+ method_source (0.9.2)
17
+ pry (0.12.2)
18
+ coderay (~> 1.1.0)
19
+ method_source (~> 0.9.0)
20
+ rake (10.5.0)
21
+ rspec (3.8.0)
22
+ rspec-core (~> 3.8.0)
23
+ rspec-expectations (~> 3.8.0)
24
+ rspec-mocks (~> 3.8.0)
25
+ rspec-core (3.8.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-expectations (3.8.2)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-mocks (3.8.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.8.0)
33
+ rspec-support (3.8.0)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ avrodrome!
40
+ bundler (~> 1.17)
41
+ faker (~> 1.9)
42
+ pry (~> 0.12)
43
+ rake (~> 10.0)
44
+ rspec (~> 3.0)
45
+
46
+ BUNDLED WITH
47
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Adam Carlile
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Avrodrome
2
+
3
+ Avrodrome, just like an aerodrome is a place to store aircraft, avrodrome is a place to store Avro schemas. Designed to be use as an in memory drop in replacement for `AvroTurf/Avromatic` registry, instead of using `Webmock` and stubbing calls for testing. It can also be used in development, as it will behave exactly like talking to an Avro schema registry
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'avrodrome'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install avrodrome
20
+
21
+ ## Configuration
22
+
23
+ ```ruby
24
+ Avrodrome.configure do |config|
25
+ config.logger = Logger.new(STDOUT) #Default
26
+ end
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ Designed to be used as a replacement for the registry class in `AvroTurf/Avromatic`
32
+
33
+ ```ruby
34
+ require 'avrodrome'
35
+
36
+ Avromatic.configure do |config|
37
+ config.schema_store = Avro::Builder::SchemaStore.new(path: 'path/to/store')
38
+ config.schema_registry = Avrodrome.build_adaptor
39
+ config.build_messaging!
40
+ end
41
+ ```
42
+
43
+ or
44
+
45
+ ```ruby
46
+ avro = AvroTurf::Messaging.new(registry: Avrodrome.build_adaptor)
47
+ avro.encode({...})
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/adamcarlile/avrodrome.
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/avrodrome.gemspec ADDED
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "avrodrome/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "avrodrome"
8
+ spec.version = Avrodrome::VERSION
9
+ spec.authors = ["Adam Carlile"]
10
+ spec.email = ["hello@adamcarlile.com"]
11
+
12
+ spec.summary = "An in memory drop in replacement for an Avro Schema Registry"
13
+ spec.homepage = "https://github.com/adamcarlile/avrodrome"
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.17"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "pry", "~> 0.12"
29
+ spec.add_development_dependency "faker", "~> 1.9"
30
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "avrodrome"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/avrodrome.rb ADDED
@@ -0,0 +1,37 @@
1
+ require "ostruct"
2
+ require "logger"
3
+
4
+ require "avrodrome/version"
5
+
6
+ require "avrodrome/schema"
7
+ require "avrodrome/subject"
8
+ require "avrodrome/registry"
9
+ require "avrodrome/adaptor"
10
+
11
+
12
+ module Avrodrome
13
+ module_function
14
+
15
+ def build_adaptor
16
+ Avrodrom::Adaptor.new
17
+ end
18
+
19
+ def version
20
+ Avrodrome::VERSION
21
+ end
22
+
23
+ def config
24
+ @config ||= OpenStruct.new.tap do |c|
25
+ c.logger = Logger.new(STDOUT)
26
+ end
27
+ end
28
+
29
+ def configure(&block)
30
+ yield(config)
31
+ end
32
+
33
+ def logger
34
+ config.logger
35
+ end
36
+
37
+ end
@@ -0,0 +1,98 @@
1
+ module Avrodrome
2
+ class Adaptor
3
+ attr_reader :registry, :logger
4
+
5
+ def initialize(logger: Avrodrome.logger, registry: Avrodrome::Registry.new)
6
+ @logger = logger
7
+ @registry = registry
8
+ end
9
+
10
+ def fetch(id)
11
+ logger.info "Fetching schema with id #{id}"
12
+ registry.find(id)&.body&.to_s || errors[:not_found]
13
+ end
14
+
15
+ def register(subject, schema)
16
+ registered_schema = registry.register!(subject, schema)
17
+ logger.info "Registered schema for subject `#{subject}`; id = #{registered_schema.id}"
18
+ registered_schema.id
19
+ end
20
+
21
+ # List all subjects
22
+ def subjects
23
+ registry.subjects.map(&:name)
24
+ end
25
+
26
+ # List all versions for a subject
27
+ def subject_versions(subject_name)
28
+ subject = registry.subject(subject_name)
29
+ return errors[:subject_not_found] unless subject
30
+ subject.schemas.map(&:version)
31
+ end
32
+
33
+ # Get a specific version for a subject
34
+ def subject_version(subject_name, version = 'latest')
35
+ subject = registry.subject(subject_name)
36
+ return errors[:subject_not_found] unless subject
37
+ version = nil if version == 'latest'
38
+ subject.version(version) || errors[:version_not_found]
39
+ end
40
+
41
+ # Check if a schema exists. Returns nil if not found.
42
+ def check(subject_name, schema)
43
+ subject = registry.subject(subject_name)
44
+ return errors[:subject_not_found] unless subject
45
+ subject.check(schema)&.to_hash
46
+ end
47
+
48
+ # AC: Assume short lived in memory store, no historic schemas for now
49
+ #
50
+ # Check if a schema is compatible with the stored version.
51
+ # Returns:
52
+ # - true if compatible
53
+ # - nil if the subject or version does not exist
54
+ # - false if incompatible
55
+ # http://docs.confluent.io/3.1.2/schema-registry/docs/api.html#compatibility
56
+ def compatible?(subject, schema, version = 'latest')
57
+ true
58
+ # data = post("/compatibility/subjects/#{subject}/versions/#{version}",
59
+ # expects: [200, 404],
60
+ # body: { schema: schema.to_s }.to_json)
61
+ # data.fetch('is_compatible', false) unless data.has_key?('error_code')
62
+ end
63
+
64
+ # Get global config
65
+ def global_config
66
+ @gloabl_config ||= { "compatibilityLevel" => "BACKWARD" }
67
+ end
68
+
69
+ # Update global config
70
+ def update_global_config(config)
71
+ global_config.merge!(config)
72
+ end
73
+
74
+ # Get config for subject
75
+ def subject_config(subject)
76
+ subject = registry.subject(subject)
77
+ return errors[:subject_not_found] unless subject
78
+ subject.config
79
+ end
80
+
81
+ # Update config for subject
82
+ def update_subject_config(subject, config)
83
+ subject = registry.subject(subject)
84
+ return errors[:subject_not_found] unless subject
85
+ subject.config.merge!(config)
86
+ end
87
+
88
+ private
89
+
90
+ def errors
91
+ @errors ||= {
92
+ not_found: { error_code: 404, message: "HTTP 404 Not Found" },
93
+ version_not_found: { error_code: 40402, message: "Version not found." },
94
+ subject_not_found: { error_code: 40401, message: "Subject not found." }
95
+ }
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,30 @@
1
+ module Avrodrome
2
+ class Registry
3
+
4
+ def subjects
5
+ @subjects ||= []
6
+ end
7
+
8
+ def schemas
9
+ subjects.map(&:schemas).flatten
10
+ end
11
+
12
+ def register!(subject_name, schema)
13
+ subject = subjects.detect {|x| x.name == subject_name } || (Avrodrome::Subject.new(name: subject_name).tap { |x| subjects << x } )
14
+ subject.register!(schema: schema, id: next_id)
15
+ end
16
+
17
+ def find(id)
18
+ schemas.detect {|x| x.id == id }
19
+ end
20
+
21
+ def subject(name)
22
+ subjects.detect {|x| x.name == name }
23
+ end
24
+
25
+ def next_id
26
+ (subjects.sum(&:total_schemas) || 0) + 1
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ module Avrodrome
2
+ class Schema
3
+ attr_reader :body, :id, :version, :name
4
+
5
+ def initialize(body:, id:, version:, name:)
6
+ @body = body
7
+ @id = id
8
+ @version = version
9
+ @name = name
10
+ end
11
+
12
+ def to_hash
13
+ {
14
+ subject: name,
15
+ version: version,
16
+ id: id,
17
+ schema: body.to_s
18
+ }
19
+ end
20
+
21
+ def to_json
22
+ to_hash.to_json
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,48 @@
1
+ module Avrodrome
2
+ class Subject
3
+ attr_reader :name
4
+
5
+ def initialize(name:)
6
+ @name = name
7
+ end
8
+
9
+ def config
10
+ @config ||= {}
11
+ end
12
+
13
+ def find(id)
14
+ schemas.detect {|x| x.id == id}
15
+ end
16
+
17
+ def version(version=nil)
18
+ if version
19
+ schemas.detect {|x| x.version == version }
20
+ else
21
+ schemas.last
22
+ end
23
+ end
24
+
25
+ def check(schema)
26
+ schemas.detect {|x| x.body == schema }
27
+ end
28
+
29
+ def schemas
30
+ @schemas ||= []
31
+ end
32
+
33
+ def register!(schema:, id:, version: next_version)
34
+ Avrodrome::Schema.new(body: schema, id: id, version: version, name: name).tap { |x| schemas << x }
35
+ end
36
+
37
+ def total_schemas
38
+ schemas.count
39
+ end
40
+
41
+ private
42
+
43
+ def next_version
44
+ schemas.count + 1
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module Avrodrome
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: avrodrome
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Carlile
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-17 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faker
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.9'
83
+ description:
84
+ email:
85
+ - hello@adamcarlile.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - avrodrome.gemspec
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/avrodrome.rb
102
+ - lib/avrodrome/adaptor.rb
103
+ - lib/avrodrome/registry.rb
104
+ - lib/avrodrome/schema.rb
105
+ - lib/avrodrome/subject.rb
106
+ - lib/avrodrome/version.rb
107
+ homepage: https://github.com/adamcarlile/avrodrome
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubygems_version: 3.0.0.beta3
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: An in memory drop in replacement for an Avro Schema Registry
130
+ test_files: []