faraday_middleware-avro 0.0.2

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: f8bad56934fe452ad4852f8cd056f9264664dd3f
4
+ data.tar.gz: 08402b3490fda0864aaf8b2bcee0cebc4d74fafd
5
+ SHA512:
6
+ metadata.gz: efde4336f74534bd98f50a8aa270634c9843d4495dc4bcd5f34b52c891cc4ee0c83365a0671529c1ae9d9d097374a49032d9eb520488b05a7938aeed05e0a10f
7
+ data.tar.gz: 5e4a535d8c14392f7ec6b3c273d11e040db16378b43c777b4c8e2e262a156306bc9d9ee113cc8fc7a3195e9697b98cd1e3922baa6b51716cb1e3476ff6516ab7
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in faraday_middleware-avro.gemspec
4
+ ruby "2.1.2"
5
+ gemspec
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ faraday_middleware-avro (0.0.2)
5
+ avro_turf (>= 0.4.1)
6
+ faraday_middleware (>= 0.8.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ avro (1.7.7)
12
+ multi_json
13
+ avro_turf (0.4.1)
14
+ avro (~> 1.7.7)
15
+ byebug (5.0.0)
16
+ columnize (= 0.9.0)
17
+ coderay (1.1.0)
18
+ columnize (0.9.0)
19
+ diff-lcs (1.2.5)
20
+ faraday (0.9.1)
21
+ multipart-post (>= 1.2, < 3)
22
+ faraday_middleware (0.10.0)
23
+ faraday (>= 0.7.4, < 0.10)
24
+ method_source (0.8.2)
25
+ multi_json (1.11.2)
26
+ multipart-post (2.0.0)
27
+ pry (0.10.1)
28
+ coderay (~> 1.1.0)
29
+ method_source (~> 0.8.1)
30
+ slop (~> 3.4)
31
+ rake (10.4.2)
32
+ rspec (3.3.0)
33
+ rspec-core (~> 3.3.0)
34
+ rspec-expectations (~> 3.3.0)
35
+ rspec-mocks (~> 3.3.0)
36
+ rspec-core (3.3.2)
37
+ rspec-support (~> 3.3.0)
38
+ rspec-expectations (3.3.1)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.3.0)
41
+ rspec-mocks (3.3.2)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.3.0)
44
+ rspec-support (3.3.0)
45
+ slop (3.6.0)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ bundler (~> 1.7)
52
+ byebug
53
+ faraday_middleware-avro!
54
+ pry
55
+ rake (~> 10.0)
56
+ rspec
57
+
58
+ BUNDLED WITH
59
+ 1.10.5
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Guilherme Cavalcanti
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,46 @@
1
+ # FaradayMiddleware::Avro
2
+
3
+ Faraday request middleware.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'faraday_middleware-avro'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ connection = Faraday.new do |conn|
21
+ conn.request :avro, schemas_path: File.expand_path(File.dirname(__FILE__))
22
+ conn.adapter Faraday.default_adapter
23
+ end
24
+
25
+
26
+ offer = { name: "Minha oferta",
27
+ unit: "Kg",
28
+ seller_code: "123",
29
+ seller_categories: "Categoria 1",
30
+ catalogs: ["outono"]
31
+ }
32
+
33
+ connection.post("http://0.0.0.0:3000/endpoint", offer, 'Avro-Schema' => 'offer')
34
+ ```
35
+
36
+ ## Todo
37
+
38
+ - Make it possible to pass the schema
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it ( https://github.com/[my-github-username]/faraday_middleware-avro/fork )
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'faraday_middleware/avro/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "faraday_middleware-avro"
8
+ spec.version = FaradayMiddleware::Avro::VERSION
9
+ spec.authors = ["Guilherme Cavalcanti"]
10
+ spec.email = ["guiocavalcanti@gmail.com"]
11
+ spec.summary = "Faraday middleware to encode and decode HTTP requests using avro"
12
+ spec.description = "Faraday middleware to encode and decode HTTP requests using avro"
13
+ spec.homepage = ""
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 "byebug"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "pry"
26
+
27
+ spec.add_dependency "faraday_middleware", '>= 0.8.0'
28
+ spec.add_dependency "avro_turf", ">= 0.4.1"
29
+ end
@@ -0,0 +1,11 @@
1
+ require 'avro_turf'
2
+ require 'faraday_middleware/response_middleware'
3
+ require 'faraday_middleware/avro/version'
4
+ require 'faraday_middleware/avro/request/encode_avro'
5
+ require 'faraday_middleware/avro/response/parse_avro'
6
+ require 'faraday_middleware/avro/register'
7
+
8
+ module FaradayMiddleware
9
+ module Avro
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ Faraday::Request.register_middleware avro: FaradayMiddleware::Avro::EncodeAvro
2
+ Faraday::Response.register_middleware avro: FaradayMiddleware::Avro::ParseAvro
@@ -0,0 +1,48 @@
1
+ class FaradayMiddleware::Avro::EncodeAvro < Faraday::Middleware
2
+ CONTENT_TYPE = 'Content-Type'.freeze
3
+ MIME_TYPE = 'avro/binary'.freeze
4
+
5
+ def initialize(app, options={})
6
+ super(app)
7
+ @schemas_path = \
8
+ options[:schemas_path] || File.expand_path(File.dirname(__FILE__))
9
+ end
10
+
11
+ def call(env)
12
+ match_content_type(env) do |data|
13
+ schema = env[:request_headers]['Avro-Schema']
14
+ env[:body] = encode(data, schema)
15
+ end
16
+
17
+ @app.call env
18
+ end
19
+
20
+ def encode(data, schema)
21
+ avro = AvroTurf.new(schemas_path: @schemas_path)
22
+ data = JSON.parse data if data.respond_to?(:to_str)
23
+
24
+ avro.encode(data, schema_name: schema)
25
+ end
26
+
27
+ def match_content_type(env)
28
+ if process_request?(env)
29
+ env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
30
+ yield env[:body]
31
+ end
32
+ end
33
+
34
+ def process_request?(env)
35
+ type = request_type(env)
36
+ has_body?(env) and (type.empty? or type == MIME_TYPE)
37
+ end
38
+
39
+ def has_body?(env)
40
+ body = env[:body] and !(body.respond_to?(:to_str) and body.empty?)
41
+ end
42
+
43
+ def request_type(env)
44
+ type = env[:request_headers][CONTENT_TYPE].to_s
45
+ type = type.split(';', 2).first if type.index(';')
46
+ type
47
+ end
48
+ end
@@ -0,0 +1,17 @@
1
+ class FaradayMiddleware::Avro::ParseAvro < FaradayMiddleware::ResponseMiddleware
2
+ def process_response(env)
3
+ env[:raw_body] = env[:body] if preserve_raw?(env)
4
+ env[:body] = parse(env[:body], env[:response_headers]['Avro-Schema'])
5
+ end
6
+
7
+ def parse(body, schema_name)
8
+ avro = AvroTurf.new(schemas_path: schemas_path)
9
+ avro.decode(body, schema_name: schema_name) unless body.empty?
10
+ end
11
+
12
+ private
13
+
14
+ def schemas_path
15
+ @options[:schemas_path] || File.expand_path(File.dirname(__FILE__))
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module FaradayMiddleware
2
+ module Avro
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,68 @@
1
+ RSpec.describe FaradayMiddleware::Avro::EncodeAvro do
2
+ subject { described_class.new application }
3
+
4
+ let(:application) { double 'Application' }
5
+ let(:environment) do
6
+ {
7
+ request_headers: request_headers,
8
+ body: body,
9
+ }
10
+ end
11
+ let(:request_headers) do
12
+ {
13
+ 'Content-Type' => content_type,
14
+ 'Avro-Schema' => avro_schema,
15
+ }
16
+ end
17
+ let(:content_type) { 'avro/binary' }
18
+ let(:avro_schema) { 'person' }
19
+ let(:body) { { name: 'Buck' } }
20
+ let(:response) { double 'Response', on_complete: nil }
21
+ let(:parsed_body) { double 'Binary', class: :binary }
22
+
23
+ describe '#call' do
24
+ before do
25
+ allow_any_instance_of(AvroTurf).to receive(:encode)
26
+ .and_return(parsed_body)
27
+ expect(application).to receive(:call)
28
+ .with(environment)
29
+ .and_return(response)
30
+ end
31
+
32
+ it 'changes the body type' do
33
+ subject.call(environment)
34
+
35
+ expect(environment[:body].class).to be :binary
36
+ end
37
+
38
+ context 'when request content is not Avro' do
39
+ let(:content_type) { 'text/json' }
40
+
41
+ it 'doest not change the body type' do
42
+ subject.call(environment)
43
+
44
+ expect(environment[:body].class).to be body.class
45
+ end
46
+ end
47
+
48
+ context 'when request has no body' do
49
+ let(:body) { nil }
50
+
51
+ it 'doest not change the body type' do
52
+ subject.call(environment)
53
+
54
+ expect(environment[:body].class).to be body.class
55
+ end
56
+ end
57
+
58
+ context 'when request body responds to #to_str' do
59
+ let(:body) { { name: 'Buck '}.to_json }
60
+
61
+ it 'changes the body type' do
62
+ subject.call(environment)
63
+
64
+ expect(environment[:body].class).to be parsed_body.class
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,51 @@
1
+ RSpec.describe FaradayMiddleware::Avro::ParseAvro do
2
+ subject { described_class.new application }
3
+
4
+ let(:application) { double 'Application' }
5
+ let(:env) { { body: body, response_headers: response_headers } }
6
+ let(:body) { double 'Binary', respond_to?: false, empty?: empty_body }
7
+ let(:response_headers) do
8
+ { 'Content-Type' => content_type, 'Avro-Schema' => schema_name }
9
+ end
10
+ let(:content_type) { 'avro/binary' }
11
+ let(:schema_name) { 'person' }
12
+ let(:empty_body) { false }
13
+
14
+ before do
15
+ allow(subject).to receive(:preserve_raw?).and_return false
16
+ end
17
+
18
+ describe '#process_response' do
19
+ let(:parsed_body) { { person: { name: 'Buck' } } }
20
+
21
+ before do
22
+ expect(subject).to receive(:parse).with(body, schema_name)
23
+ .and_return(parsed_body)
24
+ end
25
+
26
+ it 'rewrites body with parsed params' do
27
+ subject.process_response(env)
28
+
29
+ expect(env[:body]).to eq parsed_body
30
+ end
31
+ end
32
+
33
+ describe '#parse' do
34
+ it 'decodes response body with AvroTurf' do
35
+ expect_any_instance_of(AvroTurf).to receive(:decode)
36
+ .with(body, schema_name: schema_name)
37
+
38
+ subject.parse(body, schema_name)
39
+ end
40
+
41
+ context 'when body is empty' do
42
+ let(:empty_body) { true }
43
+
44
+ it 'does not decode response body' do
45
+ expect_any_instance_of(AvroTurf).to_not receive(:decode)
46
+
47
+ subject.parse(body, schema_name)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,2 @@
1
+ require 'faraday_middleware/avro'
2
+ require 'pry'
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday_middleware-avro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Guilherme Cavalcanti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-11 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: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faraday_middleware
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 0.8.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 0.8.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: avro_turf
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 0.4.1
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 0.4.1
111
+ description: Faraday middleware to encode and decode HTTP requests using avro
112
+ email:
113
+ - guiocavalcanti@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".rspec"
119
+ - Gemfile
120
+ - Gemfile.lock
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - faraday_middleware-avro.gemspec
125
+ - lib/faraday_middleware/avro.rb
126
+ - lib/faraday_middleware/avro/register.rb
127
+ - lib/faraday_middleware/avro/request/encode_avro.rb
128
+ - lib/faraday_middleware/avro/response/parse_avro.rb
129
+ - lib/faraday_middleware/avro/version.rb
130
+ - spec/faraday_middleware/avro/request/encode_avro_spec.rb
131
+ - spec/faraday_middleware/avro/response/parse_avro_spec.rb
132
+ - spec/spec_helper.rb
133
+ homepage: ''
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.4.8
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Faraday middleware to encode and decode HTTP requests using avro
157
+ test_files:
158
+ - spec/faraday_middleware/avro/request/encode_avro_spec.rb
159
+ - spec/faraday_middleware/avro/response/parse_avro_spec.rb
160
+ - spec/spec_helper.rb