sinatra-avro 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 74b847c334a0acd04d84f5fbfc19277f53181316
4
+ data.tar.gz: a79f2ef7e84dd9a5b8a13f1b4c66c2f367ce938f
5
+ SHA512:
6
+ metadata.gz: 7301c468c09573e7facef164a6afa85b2d57f3b8e9b5f8597025ea97964b990d2e5ae32900b232e1b3122def74ce2f98d9161418a08df92b84fb9f65890c4fb9
7
+ data.tar.gz: 5b22aa2c57860ad511c8af6da1d113666729ab9b9755a34352edcd1ee6e9eff7e852c815ff8bff939af243b041c1e65dd1fb6cdeaf578bd1141ec62ba55fcb0a
data/.gitignore ADDED
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sinatra-avro.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Daniel Schierbeck
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.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # Sinatra::Avro
2
+
3
+ A Sinatra plugin that allows encoding requests and responses using Apache Avro.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sinatra-avro'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sinatra-avro
20
+
21
+ ## Usage
22
+
23
+ In your Sinatra app:
24
+
25
+ ```ruby
26
+ require 'sinatra/avro'
27
+
28
+ get '/' do
29
+ schema = <<-SCHEMA
30
+ {
31
+ "name": "person",
32
+ "type": "record",
33
+ "fields": [
34
+ { "name": "name", "type": "string" },
35
+ { "name": "age", "type": "long" }
36
+ ]
37
+ }
38
+ SCHEMA
39
+
40
+ avro({ "name" => "Jane", "age" => 42 }, schema: schema)
41
+ end
42
+ ```
43
+
44
+ Alternatively, store your schemas in `schemas/<schema-name>.avsc` and refer to them by name:
45
+
46
+ ```ruby
47
+ get '/' do
48
+ # Will use the schema defined by `./schemas/person.avsc`.
49
+ avro({ "name" => "Jane", "age" => 42 }, schema_name: "person")
50
+ end
51
+ ```
52
+
53
+ You can configure which directory to look up schema files from using
54
+
55
+ ```ruby
56
+ set :avro_schema_dir, "some/other/dir"
57
+ ```
58
+
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it ( https://github.com/[my-github-username]/sinatra-avro/fork )
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,5 @@
1
+ module Sinatra
2
+ module Avro
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,43 @@
1
+ require 'sinatra/avro/version'
2
+ require 'sinatra/base'
3
+ require 'avro'
4
+
5
+ module Sinatra
6
+ module Avro
7
+ MIME_TYPE = "avro/binary".freeze
8
+
9
+ def avro(object, options = {})
10
+ schema_json = fetch_avro_schema(options)
11
+
12
+ # Set the Content-Type response header.
13
+ content_type MIME_TYPE
14
+
15
+ avro_encode(object, schema_json)
16
+ end
17
+
18
+ def avro_encode(object, schema_json)
19
+ schema = ::Avro::Schema.parse(schema_json)
20
+ writer = ::Avro::IO::DatumWriter.new(schema)
21
+ io = StringIO.new
22
+
23
+ dw = ::Avro::DataFile::Writer.new(io, writer, schema)
24
+ dw << object.to_h
25
+ dw.close
26
+
27
+ io.string
28
+ end
29
+
30
+ def fetch_avro_schema(options = {})
31
+ options[:schema] || load_avro_schema(options.fetch(:schema_name))
32
+ end
33
+
34
+ def load_avro_schema(schema_name)
35
+ schema_path = File.join(settings.avro_schema_dir, schema_name + ".avsc")
36
+
37
+ File.read(schema_path)
38
+ end
39
+ end
40
+
41
+ Base.set :avro_schema_dir, "schemas"
42
+ Base.helpers Avro
43
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sinatra/avro/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sinatra-avro"
8
+ spec.version = Sinatra::Avro::VERSION
9
+ spec.authors = ["Daniel Schierbeck"]
10
+ spec.email = ["dasch@zendesk.com"]
11
+ spec.summary = %q{A Sinatra plugin that allows encoding requests and responses using Apache Avro}
12
+ spec.homepage = "https://github.com/dasch/sinatra-avro"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "avro"
21
+ spec.add_dependency "sinatra"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.2.0"
26
+ spec.add_development_dependency "sinatra-contrib", "~> 1.4.2"
27
+ end
data/spec/avro_spec.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'sinatra/test_helpers'
2
+ require 'json'
3
+
4
+ describe Sinatra::Avro do
5
+ include Sinatra::TestHelpers
6
+
7
+ SCHEMA = {
8
+ type: "record",
9
+ name: "person",
10
+ fields: [
11
+ { name: "name", type: "string" },
12
+ { name: "age", type: "long" },
13
+ ]
14
+ }.to_json
15
+
16
+
17
+ it "allows serializing responses with Avro" do
18
+ mock_app do
19
+ get '/' do
20
+ data = { "name" => "Jane", "age" => 42 }
21
+ avro data, schema: SCHEMA
22
+ end
23
+ end
24
+
25
+ expect(decoded_data).to eq("name" => "Jane", "age" => 42)
26
+ end
27
+
28
+ it "allows loading the schema from disk automatically" do
29
+ mock_app do
30
+ set :avro_schema_dir, "spec/"
31
+ get '/' do
32
+ data = { "name" => "Jane", "age" => 42 }
33
+ avro data, schema_name: "person"
34
+ end
35
+ end
36
+
37
+ expect(decoded_data).to eq("name" => "Jane", "age" => 42)
38
+ end
39
+
40
+ it "sets the Content-Type header to avro/binary" do
41
+ mock_app do
42
+ get '/' do
43
+ data = { "name" => "Jane", "age" => 42 }
44
+ avro data, schema: SCHEMA
45
+ end
46
+ end
47
+
48
+ response = get('/')
49
+ expect(response["Content-Type"]).to eq "avro/binary"
50
+ end
51
+
52
+ def decoded_data
53
+ avro = get('/').body
54
+ reader = Avro::DataFile::Reader.new(StringIO.new(avro), Avro::IO::DatumReader.new)
55
+ data = reader.first
56
+ end
57
+ end
data/spec/person.avsc ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "type": "record",
3
+ "name": "person",
4
+ "fields": [
5
+ { "name": "name", "type": "string" },
6
+ { "name": "age", "type": "long" }
7
+ ]
8
+ }
@@ -0,0 +1 @@
1
+ require 'sinatra/avro'
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-avro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Schierbeck
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: avro
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sinatra
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '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: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.2.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.2.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: sinatra-contrib
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.4.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.4.2
97
+ description:
98
+ email:
99
+ - dasch@zendesk.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/sinatra/avro.rb
111
+ - lib/sinatra/avro/version.rb
112
+ - sinatra-avro.gemspec
113
+ - spec/avro_spec.rb
114
+ - spec/person.avsc
115
+ - spec/spec_helper.rb
116
+ homepage: https://github.com/dasch/sinatra-avro
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.2.2
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: A Sinatra plugin that allows encoding requests and responses using Apache
140
+ Avro
141
+ test_files:
142
+ - spec/avro_spec.rb
143
+ - spec/person.avsc
144
+ - spec/spec_helper.rb
145
+ has_rdoc: