faraday_middleware-msgpack 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+
6
+ script: bundle exec rake spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in faraday_middleware-msgpack.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Yuichi Tateno
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,31 @@
1
+ # FaradayMiddleware::Msgpack
2
+
3
+ [![Build Status](https://secure.travis-ci.org/hotchpotch/faraday_middleware-msgpack.png?branch=master)](http://travis-ci.org/hotchpotch/faraday_middleware-msgpack)
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'faraday_middleware-msgpack'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install faraday_middleware-msgpack
19
+
20
+ ## Examples
21
+
22
+ ```
23
+ require 'faraday_middleware/msgpack'
24
+
25
+ connection = Faraday.new do |conn|
26
+ conn.request :msgpack
27
+ conn.response :msgpack, :content_type => %r{\bapplication/x-msgpack$}
28
+ conn.adapter Faraday.default_adapter
29
+ end
30
+ ```
31
+
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.add_dependency 'faraday_middleware', '>= 0.8.0'
7
+ gem.add_dependency 'msgpack', '>= 0.4.7'
8
+ gem.add_development_dependency 'rake', '~> 0.9'
9
+ gem.add_development_dependency 'rspec', '~> 2.6'
10
+
11
+ gem.name = "faraday_middleware-msgpack"
12
+ gem.version = "0.0.1"
13
+ gem.authors = ["Yuichi Tateno"]
14
+ gem.email = ["hotchpotch@gmail.com"]
15
+ gem.description = %q{faraday middleware msgpack}
16
+ gem.summary = %q{faraday middleware msgpack}
17
+ gem.homepage = "https://github.com/hotchpotch/faraday_middleware-msgpack"
18
+
19
+ gem.files = `git ls-files`.split($/)
20
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
21
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
22
+ gem.require_paths = ["lib"]
23
+ end
@@ -0,0 +1 @@
1
+ require "faraday_middleware/msgpack"
@@ -0,0 +1,66 @@
1
+ require 'faraday_middleware/response_middleware'
2
+
3
+ module FaradayMiddleware
4
+ module Msgpack
5
+ # Public: parses response bodies with MessagePack.
6
+ class ParseMsgpack < ResponseMiddleware
7
+ dependency 'msgpack'
8
+
9
+ define_parser do |body|
10
+ if body.empty?
11
+ nil
12
+ else
13
+ ::MessagePack.unpack(body)
14
+ end
15
+ end
16
+ end
17
+
18
+ # base from FaradayMiddeware::EncodeJson
19
+ class EncodeMsgpack < Faraday::Middleware
20
+ CONTENT_TYPE = 'Content-Type'.freeze
21
+ MIME_TYPE = 'application/x-msgpack'.freeze
22
+
23
+ dependency do
24
+ require 'msgpack'
25
+ end
26
+
27
+ def call(env)
28
+ match_content_type(env) do |data|
29
+ env[:body] = encode data
30
+ end
31
+ @app.call env
32
+ end
33
+
34
+ def encode(data)
35
+ data.to_msgpack
36
+ end
37
+
38
+ def match_content_type(env)
39
+ if process_request?(env)
40
+ env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
41
+ yield env[:body] unless env[:body].respond_to?(:to_str)
42
+ end
43
+ end
44
+
45
+ def process_request?(env)
46
+ type = request_type(env)
47
+ has_body?(env) and (type.empty? or type == MIME_TYPE)
48
+ end
49
+
50
+ def has_body?(env)
51
+ body = env[:body] and !(body.respond_to?(:to_str) and body.empty?)
52
+ end
53
+
54
+ def request_type(env)
55
+ type = env[:request_headers][CONTENT_TYPE].to_s
56
+ type = type.split(';', 2).first if type.index(';')
57
+ type
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ Faraday.register_middleware :response, :msgpack => FaradayMiddleware::Msgpack::ParseMsgpack
64
+ Faraday.register_middleware :request, :msgpack => FaradayMiddleware::Msgpack::EncodeMsgpack
65
+
66
+
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+ require 'faraday_middleware/msgpack'
3
+
4
+ describe FaradayMiddleware::Msgpack::EncodeMsgpack do
5
+ let(:middleware) { described_class.new(lambda{|env| env}) }
6
+ let(:msgpack_body) { {:a => 1}.to_msgpack }
7
+
8
+ def process(body, content_type = nil)
9
+ env = {:body => body, :request_headers => Faraday::Utils::Headers.new}
10
+ env[:request_headers]['content-type'] = content_type if content_type
11
+ middleware.call(env)
12
+ end
13
+
14
+ def result_body() result[:body] end
15
+ def result_type() result[:request_headers]['content-type'] end
16
+
17
+ context "no body" do
18
+ let(:result) { process(nil) }
19
+
20
+ it "doesn't change body" do
21
+ expect(result_body).to be_nil
22
+ end
23
+
24
+ it "doesn't add content type" do
25
+ expect(result_type).to be_nil
26
+ end
27
+ end
28
+
29
+ context "empty body" do
30
+ let(:result) { process('') }
31
+
32
+ it "doesn't change body" do
33
+ expect(result_body).to be_empty
34
+ end
35
+
36
+ it "doesn't add content type" do
37
+ expect(result_type).to be_nil
38
+ end
39
+ end
40
+
41
+ context "string body" do
42
+ let(:result) { process(msgpack_body) }
43
+
44
+ it "doesn't change body" do
45
+ expect(result_body).to eq(msgpack_body)
46
+ end
47
+
48
+ it "adds content type" do
49
+ expect(result_type).to eq('application/x-msgpack')
50
+ end
51
+ end
52
+
53
+ context "object body" do
54
+ let(:result) { process({:a => 1}) }
55
+
56
+ it "encodes body" do
57
+ expect(result_body).to eq(msgpack_body)
58
+ end
59
+
60
+ it "adds content type" do
61
+ expect(result_type).to eq('application/x-msgpack')
62
+ end
63
+ end
64
+
65
+ context "empty object body" do
66
+ let(:result) { process({}) }
67
+
68
+ it "encodes body" do
69
+ expect(result_body).to eq({}.to_msgpack)
70
+ end
71
+ end
72
+
73
+ context "object body with msgpack type" do
74
+ let(:result) { process({:a => 1}, 'application/x-msgpack; charset=utf-8') }
75
+
76
+ it "encodes body" do
77
+ expect(result_body).to eq(msgpack_body)
78
+ end
79
+
80
+ it "doesn't change content type" do
81
+ expect(result_type).to eq('application/x-msgpack; charset=utf-8')
82
+ end
83
+ end
84
+
85
+ context "object body with incompatible type" do
86
+ let(:result) { process({:a => 1}, 'application/xml; charset=utf-8') }
87
+
88
+ it "doesn't change body" do
89
+ expect(result_body).to eq({:a => 1})
90
+ end
91
+
92
+ it "doesn't change content type" do
93
+ expect(result_type).to eq('application/xml; charset=utf-8')
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+ require 'faraday_middleware/msgpack'
3
+
4
+ describe FaradayMiddleware::Msgpack::ParseMsgpack, :type => :response do
5
+ let(:msgpack_body) { {:a => 1}.to_msgpack }
6
+
7
+ context "no type matching" do
8
+ it "doesn't change nil body" do
9
+ expect(process(nil).body).to be_nil
10
+ end
11
+
12
+ it "returns false for empty body" do
13
+ expect(process('').body).to be_false
14
+ end
15
+
16
+ it "parses msgpack body" do
17
+ response = process(msgpack_body)
18
+ expect(response.body).to eq('a' => 1)
19
+ expect(response.env[:raw_body]).to be_nil
20
+ end
21
+ end
22
+
23
+ context "with preserving raw" do
24
+ let(:options) { {:preserve_raw => true} }
25
+
26
+ it "parses msgpack body" do
27
+ response = process(msgpack_body)
28
+ expect(response.body).to eq('a' => 1)
29
+ expect(response.env[:raw_body]).to eq(msgpack_body)
30
+ end
31
+
32
+ it "can opt out of preserving raw" do
33
+ response = process(msgpack_body, nil, :preserve_raw => false)
34
+ expect(response.env[:raw_body]).to be_nil
35
+ end
36
+ end
37
+
38
+ context "with regexp type matching" do
39
+ let(:options) { {:content_type => /\bmsgpack$/} }
40
+
41
+ it "parses msgpack body of correct type" do
42
+ response = process(msgpack_body, 'application/x-msgpack')
43
+ expect(response.body).to eq('a' => 1)
44
+ end
45
+
46
+ it "ignores msgpack body of incorrect type" do
47
+ response = process(msgpack_body, 'text/yaml-xml')
48
+ expect(response.body).to eq(msgpack_body)
49
+ end
50
+ end
51
+
52
+ it "chokes on invalid msgpack" do
53
+ expect{ process('hello') }.to raise_error(Faraday::Error::ParsingError)
54
+ end
55
+ end
@@ -0,0 +1,31 @@
1
+
2
+ require 'rspec'
3
+
4
+ # Code from faraday_middleware/spec/spec_helper.rb
5
+ module ResponseMiddlewareExampleGroup
6
+ def self.included(base)
7
+ base.let(:options) { Hash.new }
8
+ base.let(:headers) { Hash.new }
9
+ base.let(:middleware) {
10
+ described_class.new(lambda {|env|
11
+ Faraday::Response.new(env)
12
+ }, options)
13
+ }
14
+ end
15
+
16
+ def process(body, content_type = nil, options = {})
17
+ env = {
18
+ :body => body, :request => options,
19
+ :response_headers => Faraday::Utils::Headers.new(headers)
20
+ }
21
+ env[:response_headers]['content-type'] = content_type if content_type
22
+ middleware.call(env)
23
+ end
24
+ end
25
+
26
+ RSpec.configure do |config|
27
+ config.include ResponseMiddlewareExampleGroup, :type => :response
28
+ config.expect_with :rspec do |c|
29
+ c.syntax = :expect
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday_middleware-msgpack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Yuichi Tateno
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday_middleware
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: msgpack
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.4.7
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.4.7
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '0.9'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '2.6'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '2.6'
78
+ description: faraday middleware msgpack
79
+ email:
80
+ - hotchpotch@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .travis.yml
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - faraday_middleware-msgpack.gemspec
92
+ - lib/faraday_middleware-msgpack.rb
93
+ - lib/faraday_middleware/msgpack.rb
94
+ - spec/encode_msgpack_spec.rb
95
+ - spec/parse_msgpack_spec.rb
96
+ - spec/spec_helper.rb
97
+ homepage: https://github.com/hotchpotch/faraday_middleware-msgpack
98
+ licenses: []
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.23
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: faraday middleware msgpack
121
+ test_files:
122
+ - spec/encode_msgpack_spec.rb
123
+ - spec/parse_msgpack_spec.rb
124
+ - spec/spec_helper.rb