rack-parser 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Rack::Parser #
2
2
 
3
- Rack::Parser is a Rack Middleware for parsing post body data for JSON/XML, and custom
3
+ Rack::Parser is a Rack Middleware for parsing post body data for JSON, XML, and custom
4
4
  content types using MultiJson, MultiXML, or any thing that you want to
5
5
  use.
6
6
 
@@ -48,13 +48,9 @@ engine of your choice by setting the engine like so:
48
48
  # app.rb
49
49
 
50
50
  MultiJson.engine = :yajl # Yajl-ruby for json decoding
51
- MultiXml.parser = :libxml # libxml for XML parsing
51
+ MultiXml.parser = :libxml # libxml for XML parsing
52
52
 
53
- use Rack::Parser, :content_types => {
54
- 'application/json' => Proc.new { |body| MultiJson.decode body },
55
- 'application/xml' => Proc.new { |body| MultiXml.decode body },
56
- 'application/roll' => Proc.new { |body| 'never gonna give you up' }
57
- }
53
+ use Rack::Parser
58
54
  ```
59
55
 
60
56
  To set your own custom engine that perhaps neither MultiJson or MultiXml
@@ -63,7 +59,9 @@ support, just make it a Proc:
63
59
 
64
60
  ```ruby
65
61
  use Rack::Parser, :content_types => {
66
- 'application/json' => Proc.new { |body| MyCustomJsonEngine.do_it body }
62
+ 'application/json' => Proc.new { |body| MyCustomJsonEngine.do_it body },
63
+ 'application/xml' => Proc.new { |body| MyCustomXmlEngine.decode body },
64
+ 'application/roll' => Proc.new { |body| 'never gonna give you up' }
67
65
  }
68
66
  ```
69
67
 
@@ -98,9 +96,15 @@ This project came to being because of:
98
96
  * Rack::PostBodyContentTypeParser from rack-contrib which proved to be an inspiration for both libraries.
99
97
 
100
98
 
99
+ ## External Sources/Documentations
100
+
101
+ * [Sinatra book contrib](https://github.com/sinatra/sinatra-book-contrib/blob/master/middleware/rack_parser.md) - mini tutorial on using rack-parser (thanks to [Eric Gjertsen](https://github.com/ericgj))
102
+
103
+
101
104
  ## Contributors ##
102
105
 
103
106
  * [Stephen Becker IV](https://github.com/sbeckeriv) - For initial custom error response handling work.
107
+ * [Tom May](https://github.com/tommay) - skip loading post body unless content type is set.
104
108
 
105
109
  ## Copyright
106
110
 
data/Rakefile CHANGED
@@ -4,6 +4,6 @@ require 'rake/testtask'
4
4
  Rake::TestTask.new(:test) do |test|
5
5
  test.libs << 'lib' << 'test'
6
6
  test.pattern = 'test/**/*_test.rb'
7
+ test.warning = true
7
8
  test.verbose = true
8
- test.ruby_opts << '-rubygems'
9
9
  end
data/lib/rack/parser.rb CHANGED
@@ -62,9 +62,9 @@ module Rack
62
62
  end
63
63
 
64
64
  def _call(env)
65
- body = env[POST_BODY].read
66
- return @app.call(env) if (body.respond_to?(:empty?) ? body.empty? : !body) # Send it down the stack immediately
67
65
  content_type = Rack::Request.new(env).media_type
66
+ body = env[POST_BODY].read if content_type
67
+ return @app.call(env) if (body.respond_to?(:empty?) ? body.empty? : !body) # Send it down the stack immediately
68
68
  begin
69
69
  result = @content_types[content_type].call(body)
70
70
  env.update FORM_HASH => result, FORM_INPUT => env[POST_BODY]
data/rack-parser.gemspec CHANGED
@@ -3,12 +3,12 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "rack-parser"
6
- s.version = "0.1.0"
6
+ s.version = "0.1.1"
7
7
  s.authors = ["Arthur Chiu"]
8
8
  s.email = ["mr.arthur.chiu@gmail.com"]
9
9
  s.homepage = "https://www.github.com/achiu/rack-parser"
10
10
  s.summary = %q{Rack Middleware for parsing post body data}
11
- s.description = %q{Rack Middleware for parsing post body data for json/xml and various content types}
11
+ s.description = %q{Rack Middleware for parsing post body data for json, xml and various content types}
12
12
 
13
13
  s.rubyforge_project = "rack-parser"
14
14
 
data/test/parser_test.rb CHANGED
@@ -75,4 +75,11 @@ context "Rack::Parser" do
75
75
  end
76
76
  end
77
77
 
78
+ context "for get with no content_type" do
79
+ setup { get '/' }
80
+
81
+ asserts(:status).equals 200
82
+ asserts(:body).matches %r{Hello world}
83
+ end
84
+
78
85
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-19 00:00:00.000000000Z
12
+ date: 2011-09-08 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
16
- requirement: &2152833180 !ruby/object:Gem::Requirement
16
+ requirement: &2173199560 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152833180
24
+ version_requirements: *2173199560
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: multi_json
27
- requirement: &2152832740 !ruby/object:Gem::Requirement
27
+ requirement: &2173199120 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2152832740
35
+ version_requirements: *2173199120
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: multi_xml
38
- requirement: &2152832320 !ruby/object:Gem::Requirement
38
+ requirement: &2173198700 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2152832320
46
+ version_requirements: *2173198700
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: riot
49
- requirement: &2152831900 !ruby/object:Gem::Requirement
49
+ requirement: &2173198280 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2152831900
57
+ version_requirements: *2173198280
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rack-test
60
- requirement: &2152831480 !ruby/object:Gem::Requirement
60
+ requirement: &2173197860 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2152831480
68
+ version_requirements: *2173197860
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: json
71
- requirement: &2152831060 !ruby/object:Gem::Requirement
71
+ requirement: &2173197440 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,9 +76,9 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2152831060
80
- description: Rack Middleware for parsing post body data for json/xml and various content
81
- types
79
+ version_requirements: *2173197440
80
+ description: Rack Middleware for parsing post body data for json, xml and various
81
+ content types
82
82
  email:
83
83
  - mr.arthur.chiu@gmail.com
84
84
  executables: []