rack-middleware-json-parser 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 938399f17e3d3522d2c204265bcc6333fd8653c9
4
+ data.tar.gz: 9ae19275a1b43382764aeeb1eb6bf9d9dae2121a
5
+ SHA512:
6
+ metadata.gz: 24d87fdb73fa7993f083e0d4810494aa1fddbc6fb054c9c7d19d65e3bf952043f49ddc6b3c87628d1f2536ecd169c1893669518706cc9db8da7f855dbd82adc9
7
+ data.tar.gz: 59867130ac5452cdf33453d9d7e7094182d991dd2515f22ff2915649acd732c021f9ced5b7910d7b2233e68cbfe5d31e8b87c60eb38bb053b7e292089793af75
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+ *.swp
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --profile
3
+ --order random
4
+
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'http://rubygems.org'
2
+
3
+ ruby '2.0.0'
4
+
5
+ # Specify your gem's dependencies in auth-map.gemspec
6
+ gemspec
7
+
8
+ gem 'rspec', '~> 2.14.1'
9
+ gem 'rack-test', '~> 0.6.2', :require => 'rack/test'
10
+
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-middleware-json-parser (0.0.1)
5
+ oj
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.4)
11
+ oj (2.1.4)
12
+ rack (1.5.2)
13
+ rack-test (0.6.2)
14
+ rack (>= 1.0)
15
+ rspec (2.14.1)
16
+ rspec-core (~> 2.14.0)
17
+ rspec-expectations (~> 2.14.0)
18
+ rspec-mocks (~> 2.14.0)
19
+ rspec-core (2.14.4)
20
+ rspec-expectations (2.14.0)
21
+ diff-lcs (>= 1.1.3, < 2.0)
22
+ rspec-mocks (2.14.2)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ rack-middleware-json-parser!
29
+ rack-test (~> 0.6.2)
30
+ rspec (~> 2.14.1)
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5
+
6
+ Everyone is permitted to copy and distribute verbatim or modified
7
+ copies of this license document, and changing it is allowed as long
8
+ as the name is changed.
9
+
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12
+
13
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ rack-middleware-json-parser
2
+ ============
3
+
4
+ Simple rack middleware class that parses JSON requests and puts the result to env['rack.json']
5
+
6
+
7
+ Gemfile
8
+ ```ruby
9
+ gem "rack-middleware-json-parser", "~> 0.0.1", :require => "rack/middleware/json_parser"
10
+ ```
11
+
12
+ Usage example for Siantra/Padrino:
13
+
14
+ In app.rb
15
+ ```ruby
16
+ use Rack::Middleware::JsonParser
17
+ ```
18
+
19
+ In a controller
20
+ ```ruby
21
+ put '/the/action' do
22
+ my_json = env['rack.json']
23
+ my_json[:foo][:bar]
24
+ end
25
+ ```
26
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,27 @@
1
+ require 'oj'
2
+
3
+ module Rack
4
+ module Middleware
5
+ class JsonParser
6
+ class Error < StandardError; end
7
+
8
+ attr_reader :app
9
+
10
+ def initialize(app)
11
+ @app = app
12
+ end
13
+
14
+ def call(env)
15
+ if env['CONTENT_TYPE'] == 'application/json' and (%w[put post].include?(env['REQUEST_METHOD'].downcase))
16
+ input = env['rack.input']
17
+ input.rewind
18
+ env['rack.json'] = Oj.load(input.read, symbol_keys: true)
19
+ end
20
+ app.call(env)
21
+ rescue Oj::ParseError
22
+ raise Error
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/middleware/json_parser'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'rack-middleware-json-parser'
8
+ gem.version = '0.0.1'
9
+ gem.date = '2013-08-02'
10
+ gem.summary = "Rack middleware JSON parser"
11
+ gem.description = "Rack middleware for parsing json requests."
12
+ gem.authors = ["Yuriy Kharchenko"]
13
+ gem.email = 'yuri.kharchenko@gmail.com'
14
+ gem.homepage = 'https://github.com/letmein/rack-middleware-json-parser'
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^spec/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_dependency "oj"
22
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::Middleware::JsonParser do
4
+
5
+ let(:inner_app) do
6
+ proc { |env| [200, {'Content-Type' => 'text/plain'}, [env['rack.json'] || 'ok']] }
7
+ end
8
+
9
+ let(:app) { described_class.new(inner_app) }
10
+
11
+ context "with Content-Type: application/json" do
12
+ context "requested via GET" do
13
+ before do
14
+ get '/', {}, 'CONTENT_TYPE' => 'application/json'
15
+ end
16
+
17
+ it "should not parse json" do
18
+ last_response.should be_ok
19
+ last_response.body.should eq 'ok'
20
+ end
21
+ end
22
+
23
+ context "requested via POST" do
24
+ before do
25
+ post '/', '{"foo":"bar"}', 'CONTENT_TYPE' => 'application/json'
26
+ end
27
+
28
+ it "should parse json" do
29
+ last_response.should be_ok
30
+ last_response.body.should eq '{:foo=>"bar"}'
31
+ end
32
+ end
33
+
34
+ context "requested via PUT" do
35
+ before do
36
+ put '/', '{"foo":"bar"}', 'CONTENT_TYPE' => 'application/json'
37
+ end
38
+
39
+ it "should parse json" do
40
+ last_response.should be_ok
41
+ last_response.body.should eq '{:foo=>"bar"}'
42
+ end
43
+ end
44
+
45
+ context "with invalid JSON" do
46
+ it "should raise error" do
47
+ expect { post '/', '{"foobar"}', 'CONTENT_TYPE' => 'application/json' }.to raise_error(Rack::Middleware::JsonParser::Error)
48
+ end
49
+ end
50
+ end
51
+
52
+ context "with non-json Content-Type" do
53
+ context "requested via GET" do
54
+ before do
55
+ get '/', {}, 'CONTENT_TYPE' => 'text/plain'
56
+ end
57
+
58
+ it "should not parse json" do
59
+ last_response.should be_ok
60
+ last_response.body.should eq 'ok'
61
+ end
62
+ end
63
+
64
+ context "requested via POST" do
65
+ before do
66
+ post '/', 'foobar', 'CONTENT_TYPE' => 'text/plain'
67
+ end
68
+
69
+ it "should not parse json" do
70
+ last_response.should be_ok
71
+ last_response.body.should eq 'ok'
72
+ end
73
+ end
74
+
75
+ context "requested via PUT" do
76
+ before do
77
+ put '/', 'foobar', 'CONTENT_TYPE' => 'text/plain'
78
+ end
79
+
80
+ it "should not parse json" do
81
+ last_response.should be_ok
82
+ last_response.body.should eq 'ok'
83
+ end
84
+ end
85
+ end
86
+
87
+ end
88
+
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rack/test'
4
+ require 'rack/middleware/json_parser'
5
+
6
+ RSpec.configure do |conf|
7
+
8
+ conf.include Rack::Test::Methods
9
+
10
+ end
11
+
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-middleware-json-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yuriy Kharchenko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oj
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Rack middleware for parsing json requests.
28
+ email: yuri.kharchenko@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - .rspec
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - lib/rack/middleware/json_parser.rb
41
+ - rack-middleware-json-parser.gemspec
42
+ - spec/lib/rack/middleware/json_parser_spec.rb
43
+ - spec/spec_helper.rb
44
+ homepage: https://github.com/letmein/rack-middleware-json-parser
45
+ licenses: []
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.0.3
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Rack middleware JSON parser
67
+ test_files:
68
+ - spec/lib/rack/middleware/json_parser_spec.rb
69
+ - spec/spec_helper.rb