rack-post_body_msgpack_parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-post_body_msgpack_parser.gemspec
4
+ gemspec
5
+
6
+ gem 'pry-debugger'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Uchio KONDO
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,29 @@
1
+ # Rack::PostBodyMsgpackParser
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-post_body_msgpack_parser'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-post_body_msgpack_parser
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -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
@@ -0,0 +1 @@
1
+ require "rack/post_body_msgpack_parser"
@@ -0,0 +1,40 @@
1
+ # Copied from a great job
2
+ # https://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/post_body_content_type_parser.rb
3
+
4
+ require "rack"
5
+ require "msgpack"
6
+
7
+ module Rack
8
+ class PostBodyMsgpackParser
9
+ CONTENT_TYPE = 'CONTENT_TYPE'.freeze
10
+ POST_BODY = 'rack.input'.freeze
11
+ FORM_INPUT = 'rack.request.form_input'.freeze
12
+ FORM_HASH = 'rack.request.form_hash'.freeze
13
+ FORM_HASH_MSGPACK = 'rack.request.form_hash_msgpack'.freeze
14
+
15
+ APPLICATION_MSGPACK_MIMES = ["application/x-msgpack", "application/x-mpac"]
16
+
17
+ def initialize(app, options={})
18
+ @app = app
19
+ @options = options
20
+ end
21
+
22
+ def override_params?
23
+ !! @options[:override_params]
24
+ end
25
+
26
+ def call(env)
27
+ if APPLICATION_MSGPACK_MIMES.include?(Rack::Request.new(env).media_type) && (body = env[POST_BODY].read).length != 0
28
+ env[POST_BODY].rewind # somebody might try to read this stream
29
+ body = MessagePack.unpack body
30
+ if override_params?
31
+ env.update(FORM_HASH => body, FORM_INPUT => env[POST_BODY])
32
+ end
33
+ env.update(FORM_HASH_MSGPACK => body)
34
+ end
35
+ @app.call(env)
36
+ end
37
+ end
38
+ end
39
+
40
+ require "rack/post_body_msgpack_parser/version"
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class PostBodyMsgpackParser
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/post_body_msgpack_parser/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rack-post_body_msgpack_parser"
8
+ gem.version = Rack::PostBodyMsgpackParser::VERSION
9
+ gem.authors = ["Uchio KONDO"]
10
+ gem.email = ["udzura@udzura.jp"]
11
+ gem.description = %q{Parse post data by MessagePack}
12
+ gem.summary = %q{Parse post data by MessagePack}
13
+ gem.homepage = "https://github.com/udzura/rack-post_body_msgpack_parser"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ # gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency 'rack', '>= 0'
21
+ gem.add_runtime_dependency 'msgpack', '>= 0'
22
+ gem.add_development_dependency 'rake', '>= 0'
23
+ gem.add_development_dependency 'rspec', '>= 0'
24
+ gem.add_development_dependency 'pry', '>= 0'
25
+ gem.add_development_dependency 'rack-test', '>= 0'
26
+ gem.add_development_dependency 'sinatra', '>= 1.3.0'
27
+ gem.add_development_dependency 'sinatra-contrib', '>= 0'
28
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+ require "rack/post_body_msgpack_parser"
4
+
5
+ describe Rack::PostBodyMsgpackParser do
6
+ let(:msgpack_data) { MessagePack.pack({"the_key" => "THE VALUE", "sample" => [1, 2, 3]}) }
7
+ let(:json_data) { JSON.dump({"the_key" => "THE VALUE", "sample" => [1, 2, 3]}) }
8
+
9
+ context 'not override_params' do
10
+ before do
11
+ mock_app do
12
+ use Rack::PostBodyMsgpackParser
13
+
14
+ helpers do
15
+ def msgpack_params
16
+ @_params ||= request.env['rack.request.form_hash_msgpack']
17
+ end
18
+ end
19
+
20
+ post '/test-it' do
21
+ if msgpack_params
22
+ "OK: keys: #{msgpack_params.keys}, values: #{msgpack_params.values}"
23
+ else
24
+ "No Data"
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ it "should ignore post when posted normally" do
31
+ post "/test-it", {foo: "bar"} do |response|
32
+ response.body.should == "No Data"
33
+ end
34
+ last_request.params.should have(1).pair
35
+ end
36
+
37
+ it "should accept post by msgpack data" do
38
+ header 'Content-Type', 'application/x-msgpack'
39
+ post "/test-it", {}, {:input => msgpack_data} do |response|
40
+ response.body.should include "OK"
41
+ response.body.should include %q(keys: ["the_key", "sample"])
42
+ response.body.should include %q(values: ["THE VALUE", [1, 2, 3]])
43
+ end
44
+ last_request.params.should be_empty
45
+ end
46
+
47
+ it "should ignore post by json data" do
48
+ header 'Content-Type', 'application/json'
49
+ post "/test-it", {}, {:input => json_data} do |response|
50
+ response.body.should == "No Data"
51
+ end
52
+ last_request.params.should be_empty
53
+ end
54
+ end
55
+
56
+ context 'not override_params' do
57
+ before do
58
+ mock_app do
59
+ use Rack::PostBodyMsgpackParser, override_params: true
60
+
61
+ post '/test-it' do
62
+ builder = ''
63
+ params.each_pair do |key, value|
64
+ builder << "#{key.to_s}=#{value.inspect} "
65
+ end
66
+ "OK: #{builder}"
67
+ end
68
+ end
69
+ end
70
+
71
+ it "should ignore post when posted normally" do
72
+ post "/test-it", {foo: "bar"} do |response|
73
+ response.body.should include %q(foo="bar")
74
+ end
75
+ last_request.params.should have(1).pair
76
+ end
77
+
78
+ it "should accept post by msgpack data" do
79
+ header 'Content-Type', 'application/x-msgpack'
80
+ post "/test-it", {}, {:input => msgpack_data} do |response|
81
+ response.body.should include %q(the_key="THE VALUE")
82
+ response.body.should include %q(sample=[1, 2, 3])
83
+ end
84
+ last_request.params.should have(2).pair
85
+ end
86
+
87
+ it "should ignore post by json data" do
88
+ header 'Content-Type', 'application/json'
89
+ post "/test-it", {}, {:input => json_data} do |response|
90
+ response.body.length.should == 4
91
+ response.body.should_not include %q(the_key="THE VALUE")
92
+ response.body.should_not include %q(sample=[1, 2, 3])
93
+ end
94
+ last_request.params.should be_empty
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,24 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'rack/test'
9
+ require 'sinatra/test_helpers'
10
+ require 'pry' if ENV['PRY_DEBUG']
11
+
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.run_all_when_everything_filtered = true
15
+ config.filter_run :focus
16
+
17
+ # Run specs in random order to surface order dependencies. If you find an
18
+ # order dependency and want to debug it, you can fix the order by providing
19
+ # the seed, which is printed after each run.
20
+ # --seed 1234
21
+ config.order = 'random'
22
+
23
+ config.include Sinatra::TestHelpers
24
+ end
metadata ADDED
@@ -0,0 +1,193 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-post_body_msgpack_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Uchio KONDO
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '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'
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'
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'
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'
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'
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: '0'
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: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rack-test
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: sinatra
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 1.3.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: 1.3.0
126
+ - !ruby/object:Gem::Dependency
127
+ name: sinatra-contrib
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ description: Parse post data by MessagePack
143
+ email:
144
+ - udzura@udzura.jp
145
+ executables: []
146
+ extensions: []
147
+ extra_rdoc_files: []
148
+ files:
149
+ - .gitignore
150
+ - .rspec
151
+ - Gemfile
152
+ - LICENSE.txt
153
+ - README.md
154
+ - Rakefile
155
+ - lib/rack-post_body_msgpack_parser.rb
156
+ - lib/rack/post_body_msgpack_parser.rb
157
+ - lib/rack/post_body_msgpack_parser/version.rb
158
+ - rack-post_body_msgpack_parser.gemspec
159
+ - spec/rack/post_body_msgpack_parser_spec.rb
160
+ - spec/spec_helper.rb
161
+ homepage: https://github.com/udzura/rack-post_body_msgpack_parser
162
+ licenses: []
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ none: false
169
+ requirements:
170
+ - - ! '>='
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ segments:
174
+ - 0
175
+ hash: 3806413066505115498
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ! '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ segments:
183
+ - 0
184
+ hash: 3806413066505115498
185
+ requirements: []
186
+ rubyforge_project:
187
+ rubygems_version: 1.8.23
188
+ signing_key:
189
+ specification_version: 3
190
+ summary: Parse post data by MessagePack
191
+ test_files:
192
+ - spec/rack/post_body_msgpack_parser_spec.rb
193
+ - spec/spec_helper.rb