rack-parser 0.6.1 → 0.7.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b75afa920d500b395cfeb8e69fcd5b02940abf94
4
- data.tar.gz: b2251334c07c80081ba9172e199787e927ad6c14
3
+ metadata.gz: 74f69a416e4aa2289dcad838a169cfce39ad19cd
4
+ data.tar.gz: a521face89c84772bf670a9a2ce783c2f4001caa
5
5
  SHA512:
6
- metadata.gz: 52f7238b8cb20e48f7d851eabfef29bf976a9a3e712ebe6136ac74f3db73dcde736c7586f7367c87ca1f050f08e606dfa1d002d9b9064bccba56d4fa435be2cc
7
- data.tar.gz: 82d88abe18e5b53880bf605331c84ada934e896e064d42ec64f210bc8a507147ff9b28f14a25b7f9dbf7c6b76558b5609da6bf184713508b98c2cb70fcb70831
6
+ metadata.gz: 564feba9cd26974578068efea5dfb8850cebf497f2e9935509d928dd5d2397c0934d0d98a46ad8339ce5a5291ddb4671f20d2d87e4c45db8bee79b69c1e2890b
7
+ data.tar.gz: 71c633ee487073bb5e3ab2e48a7a149dc266f75e3ef2f36b56215e8237de81e33bb6e8d58dc1770c2eff4219bbf23effeab495cca422de17b2b95839b5017653
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in rack-parser.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -75,6 +75,8 @@ http://www.mikeperham.com/2012/03/03/the-perils-of-rescue-exception/
75
75
  With version `0.4.0`, you can specify regex matches for the content
76
76
  types that you want the `parsers` and `handlers` to match.
77
77
 
78
+ NOTE: you need to explicitly pass a `Regexp` for it to regex match.
79
+
78
80
  ```ruby
79
81
  parser = proc { |data| JSON.parse data }
80
82
  handler = proc { |e, type| [400, {}, 'boop'] }
@@ -4,6 +4,7 @@ module Rack
4
4
  POST_BODY = 'rack.input'.freeze
5
5
  FORM_INPUT = 'rack.request.form_input'.freeze
6
6
  FORM_HASH = 'rack.request.form_hash'.freeze
7
+ PARSER_RESULT = 'rack.parser.result'.freeze
7
8
 
8
9
  JSON_PARSER = proc { |data| JSON.parse data }
9
10
  ERROR_HANDLER = proc { |err, type| [400, {}, ['']] }
@@ -12,23 +13,24 @@ module Rack
12
13
 
13
14
  def initialize(app, options = {})
14
15
  @app = app
15
- @parsers = options.delete(:parsers) || { %r{json} => JSON_PARSER }
16
- @handlers = options.delete(:handlers) || {}
17
- @logger = options.delete(:logger)
16
+ @parsers = options[:parsers] || { %r{json} => JSON_PARSER }
17
+ @handlers = options[:handlers] || {}
18
+ @logger = options[:logger]
18
19
  end
19
20
 
20
21
  def call(env)
21
22
  type = Rack::Request.new(env).media_type
22
- parser = parsers.detect { |content_type, _| type.match(content_type) } if type
23
+ parser = match_content_types_for(parsers, type) if type
23
24
  return @app.call(env) unless parser
24
25
  body = env[POST_BODY].read ; env[POST_BODY].rewind
25
26
  return @app.call(env) unless body && !body.empty?
26
27
  begin
27
28
  parsed = parser.last.call body
28
- env.update FORM_HASH => parsed, FORM_INPUT => env[POST_BODY]
29
+ env[PARSER_RESULT] = parsed
30
+ env.update FORM_HASH => parsed, FORM_INPUT => env[POST_BODY] if parsed.is_a?(Hash)
29
31
  rescue StandardError => e
30
32
  warn! e, type
31
- handler = handlers.detect { |content_type, _| type.match(content_type) }
33
+ handler = match_content_types_for handlers, type
32
34
  handler ||= ['default', ERROR_HANDLER]
33
35
  return handler.last.call(e, type)
34
36
  end
@@ -45,5 +47,17 @@ module Rack
45
47
  message = "[Rack::Parser] Error on %s : %s" % [type, error.to_s]
46
48
  logger.warn message
47
49
  end
50
+
51
+ # Private: matches content types for the given media type
52
+ #
53
+ # content_types - An array of the parsers or handlers options
54
+ # type - The media type. gathered from the Rack::Request
55
+ #
56
+ # Returns The match from the parser/handler hash or nil
57
+ def match_content_types_for(content_types, type)
58
+ content_types.detect do |content_type, _|
59
+ content_type.is_a?(Regexp) ? type.match(content_type) : type == content_type
60
+ end
61
+ end
48
62
  end
49
63
  end
@@ -3,7 +3,7 @@ $:.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.6.1"
6
+ s.version = "0.7.0"
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"
@@ -3,12 +3,18 @@ require File.expand_path('../spec_helper', __FILE__)
3
3
  describe Rack::Parser do
4
4
 
5
5
  it "allows you to setup parsers for content types" do
6
- middleware = Rack::Parser.new ParserApp, :parsers => { 'foo' => 'bar' }
6
+ middleware = Rack::Parser.new ParserApp, :parsers => { 'foo' => 'bar' }
7
7
  assert_equal 'bar', middleware.parsers['foo']
8
8
  end
9
9
 
10
+ it "should not remove fields from options in setup" do
11
+ options = {:parsers => { 'foo' => 'bar' }}
12
+ middleware = Rack::Parser.new ParserApp, options
13
+ refute_nil options[:parsers]
14
+ end
15
+
10
16
  it "allows you to setup error handlers" do
11
- stack = Rack::Parser.new ParserApp, :handlers => { 'foo' => 'bar' }
17
+ stack = Rack::Parser.new ParserApp, :handlers => { 'foo' => 'bar' }
12
18
  assert_equal 'bar', stack.handlers['foo']
13
19
  end
14
20
 
@@ -42,6 +48,16 @@ describe Rack::Parser do
42
48
  assert_equal "{\"a\"=>2}", last_response.body
43
49
  end
44
50
 
51
+ it 'matches ambiguous string Content-Type and forces explicit regex' do
52
+ payload = JSON.dump(:a => 2)
53
+ parser = proc { |data| JSON.parse data }
54
+ stack Rack::Parser, :parsers => { 'application/vnd.foo+json' => parser }
55
+ post '/post', payload, { 'CONTENT_TYPE' => 'application/vnd.foo+json' }
56
+
57
+ assert last_response.ok?
58
+ assert_equal "{\"a\"=>2}", last_response.body
59
+ end
60
+
45
61
  it "handles upstream errors" do
46
62
  assert_raises StandardError, 'error!' do
47
63
  parser = proc { |data| JSON.parse data }
@@ -52,7 +68,7 @@ describe Rack::Parser do
52
68
 
53
69
  it "returns a default error" do
54
70
  parser = proc { |data| raise StandardError, 'wah wah' }
55
- stack Rack::Parser, :parsers => { %r{json} => parser }
71
+ stack Rack::Parser, :parsers => { %r{json} => parser }
56
72
  post '/post', '{}', { 'CONTENT_TYPE' => 'application/vnd.foo+json' }
57
73
 
58
74
  assert_equal 400, last_response.status
@@ -61,11 +77,33 @@ describe Rack::Parser do
61
77
  it "returns a custom error message" do
62
78
  parser = proc { |data| raise StandardError, "wah wah" }
63
79
  handler = proc { |err, type| [500, {}, "%s : %s" % [type, err]] }
64
- stack Rack::Parser, :parsers => { %r{json} => parser },
80
+ stack Rack::Parser, :parsers => { %r{json} => parser },
65
81
  :handlers => { %r{json} => handler }
66
82
  post '/post', '{}', { 'CONTENT_TYPE' => 'application/vnd.foo+json' }
67
83
 
68
84
  assert_equal 500, last_response.status
69
85
  assert_equal 'application/vnd.foo+json : wah wah', last_response.body
70
86
  end
87
+
88
+ it 'returns a custome error for ambiguous string Content-Type and forces explicit regex' do
89
+ parser = proc { |data| raise StandardError, "wah wah" }
90
+ handler = proc { |err, type| [500, {}, "%s : %s" % [type, err]] }
91
+ stack Rack::Parser, :parsers => { %r{json} => parser },
92
+ :handlers => { 'application/vnd.foo+json' => handler }
93
+ post '/post', '{}', { 'CONTENT_TYPE' => 'application/vnd.foo+json' }
94
+
95
+ assert_equal 500, last_response.status
96
+ assert_equal 'application/vnd.foo+json : wah wah', last_response.body
97
+ end
98
+
99
+ it "parses an array but do not set it to params" do
100
+ payload = JSON.dump([1,2,3])
101
+ parser = proc { |data| JSON.parse data }
102
+ stack Rack::Parser, :parsers => { 'application/json' => parser }
103
+ post '/post', payload, { 'CONTENT_TYPE' => 'application/json' }
104
+
105
+ assert last_response.ok?
106
+ assert_equal last_request.env['rack.parser.result'], [1, 2, 3]
107
+ assert_equal last_request.env['rack.request.form_hash'], nil
108
+ end
71
109
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arthur Chiu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-10 00:00:00.000000000 Z
11
+ date: 2016-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  version: '0'
89
89
  requirements: []
90
90
  rubyforge_project: rack-parser
91
- rubygems_version: 2.2.2
91
+ rubygems_version: 2.2.3
92
92
  signing_key:
93
93
  specification_version: 4
94
94
  summary: Rack Middleware for parsing post body data