rack-jquery-params 0.1.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 +7 -0
- data/.gitignore +22 -0
- data/.yardopts +5 -0
- data/.yardopts.rb +1 -0
- data/Gemfile +12 -0
- data/README.md +79 -0
- data/Rakefile +22 -0
- data/lib/rack/jquery-params.rb +2 -0
- data/lib/rack/jquery-params/load.rb +63 -0
- data/lib/rack/jquery-params/version.rb +3 -0
- data/rack-jquery-params.gemspec +20 -0
- data/test/rack/params_test.rb +66 -0
- data/test/test_helper.rb +4 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9172bcca4b13cbe8b15425156a6139deb65cb013
|
4
|
+
data.tar.gz: 1c3452cd4b88cac928c732098e65ef2c7bc5fce8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 89ff06303106907931d430498f98fce1cf150a05ed03491ebab743c95e927b57ba6e1f5e2f1cd59f3dbc3521b8bee7a3eee21f899d4dab6b4693642bf11e5a4d
|
7
|
+
data.tar.gz: 299c03e247fa6dfb1bda73714940d4bd18091b007500b51334e94367cc9896474744bc12f3caeb52d6c184f112425c2f786d885b5daa0f39ec99ba6c280c03d2
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
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
|
+
|
15
|
+
# YARD artifacts
|
16
|
+
.yardoc
|
17
|
+
_yardoc
|
18
|
+
doc/
|
19
|
+
.DS_Store
|
20
|
+
.idea/
|
21
|
+
.idea/*
|
22
|
+
Gemfile.lock
|
data/.yardopts
ADDED
data/.yardopts.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'redcarpet/compat'
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
rack-jquery-params
|
2
|
+
===========
|
3
|
+
|
4
|
+
A Rack middleware that bridges the discrepancy between jQuery.param and how Rack parses nested params within query
|
5
|
+
strings and post bodies.
|
6
|
+
|
7
|
+
If you use jQuery's get, post, put, or ajax methods then jQuery.param is run on your data. The problem is that jQuery.param
|
8
|
+
turns this javascript object:
|
9
|
+
|
10
|
+
```
|
11
|
+
{test: [{this_is_an_object_inside_an_array:'yes'}]}
|
12
|
+
```
|
13
|
+
|
14
|
+
into a query string that Rack cannot read correctly:
|
15
|
+
|
16
|
+
```
|
17
|
+
test[0][this_is_an_object_inside_an_array]=yes
|
18
|
+
```
|
19
|
+
|
20
|
+
Rack expects all arrays to be sent as empty brackets (i.e., "test[]"). It parses jQuery's "test[0]" as a hash, resulting
|
21
|
+
in the following params:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
{
|
25
|
+
:test => {
|
26
|
+
:0 => {
|
27
|
+
:this_is_an_object_inside_an_array => 'yes'
|
28
|
+
}
|
29
|
+
}
|
30
|
+
}
|
31
|
+
```
|
32
|
+
|
33
|
+
This gem fixes the above params and converts it to:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
{
|
37
|
+
:test => [
|
38
|
+
{
|
39
|
+
:this_is_an_object_inside_an_array => 'yes'
|
40
|
+
}
|
41
|
+
]
|
42
|
+
}
|
43
|
+
```
|
44
|
+
|
45
|
+
## Setting Up JSONR
|
46
|
+
|
47
|
+
Install the gem:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
gem install rack-jquery-params
|
51
|
+
```
|
52
|
+
|
53
|
+
In your Gemfile:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
gem 'rack-jquery-params', :require => 'rack/jquery-params'
|
57
|
+
```
|
58
|
+
|
59
|
+
Activate by including it your config.ru file:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
use Rack::JQueryParams
|
63
|
+
run Sinatra::Application
|
64
|
+
```
|
65
|
+
|
66
|
+
## Options
|
67
|
+
|
68
|
+
By default the JQueryParams fix is applied to all http methods, however you can change this functionality by setting
|
69
|
+
the applies_to option with your chosen http method:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
use Rack::JQueryParams :applies_to => :get
|
73
|
+
```
|
74
|
+
|
75
|
+
You can also set applies_to to be an array of http methods:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
use Rack::JQueryParams :applies_to => [:get, :put, :delete]
|
79
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'fileutils'
|
4
|
+
include FileUtils
|
5
|
+
|
6
|
+
task :run_test do
|
7
|
+
Rake::Task['test'].execute
|
8
|
+
puts '---------------------------------------------------------------------'
|
9
|
+
puts 'TESTS PASSED... READY TO BUILD...'
|
10
|
+
puts '---------------------------------------------------------------------'
|
11
|
+
end
|
12
|
+
task :build => :run_test
|
13
|
+
task :default => :build
|
14
|
+
|
15
|
+
########################################################################
|
16
|
+
|
17
|
+
Rake::TestTask.new do |t|
|
18
|
+
t.libs.push 'lib'
|
19
|
+
t.libs.push 'test'
|
20
|
+
t.pattern = 'test/**/*_test.rb'
|
21
|
+
t.verbose = false
|
22
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'rack'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class JQueryParams
|
5
|
+
include Rack::Utils
|
6
|
+
|
7
|
+
HTTP_METHODS = %w(GET PUT POST DELETE HEAD OPTIONS PATCH)
|
8
|
+
ALL = 'ALL'
|
9
|
+
|
10
|
+
def initialize(app, options={})
|
11
|
+
@options = options
|
12
|
+
@app = app
|
13
|
+
end
|
14
|
+
|
15
|
+
# Loops through all params and convert the hashes to arrays only if all keys are comprised of integers.
|
16
|
+
#
|
17
|
+
def call(env)
|
18
|
+
status, headers, response = @app.call(env)
|
19
|
+
|
20
|
+
self.class.fix(env, @options[:applies_to])
|
21
|
+
[status, headers, response]
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.fix(env, valid_methods=:all)
|
25
|
+
valid_methods = extract_valid_methods(valid_methods)
|
26
|
+
return if valid_methods != :all and !valid_methods.include?(env['REQUEST_METHOD'])
|
27
|
+
env['rack.request.query_hash'].each {|k,p| env['rack.request.query_hash'][k] = fix_param(p) }
|
28
|
+
env['rack.request.form_hash'].each {|k,p| env['rack.request.form_hash'][k] = fix_param(p) }
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.fix_param(param)
|
32
|
+
if param.is_a?(Hash)
|
33
|
+
if param.all?{|k,v| k =~ /^[0-9]+$/}
|
34
|
+
param.sort.inject([]){|result, v| result << fix_param(v[1]) }
|
35
|
+
else
|
36
|
+
param.each{|k,v| param[k] = fix_param(v)}
|
37
|
+
end
|
38
|
+
elsif param.is_a?(Array)
|
39
|
+
param.each_with_index {|v,i| param[i] = fix_param(v) }
|
40
|
+
return param
|
41
|
+
else
|
42
|
+
return param
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.extract_valid_methods(object)
|
47
|
+
valid_methods = []
|
48
|
+
if object.is_a?(Array)
|
49
|
+
object.each do |a|
|
50
|
+
a = a.to_s.upcase
|
51
|
+
break valid_methods = [] if a == 'ALL'
|
52
|
+
valid_methods << a if HTTP_METHODS.include?(a)
|
53
|
+
end
|
54
|
+
else
|
55
|
+
method = object.to_s.upcase
|
56
|
+
valid_methods = [method] if HTTP_METHODS.include?(method)
|
57
|
+
end
|
58
|
+
(valid_methods.size > 0) ? valid_methods : :all
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rack/jquery-params'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'rack-jquery-params'
|
8
|
+
gem.version = Rack::JQueryParams::VERSION
|
9
|
+
gem.date = '2015-01-18'
|
10
|
+
gem.summary = %q{Rack middleware that fixes the discrepancy between jQuery.param and how Rack parses nested queries.}
|
11
|
+
gem.authors = ['Caleb Clark']
|
12
|
+
gem.email = ['cclark@fanforce.com']
|
13
|
+
gem.homepage = 'http://github.com/calebclark/rack-jquery-params'
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.require_paths = ['lib']
|
18
|
+
|
19
|
+
gem.add_development_dependency 'rack'
|
20
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'rack/jquery-params'
|
3
|
+
|
4
|
+
describe 'params' do
|
5
|
+
|
6
|
+
def create_env
|
7
|
+
{
|
8
|
+
'rack.request.query_hash' => {},
|
9
|
+
'rack.request.form_hash' => {},
|
10
|
+
'REQUEST_METHOD' => 'POST'
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should ignore hashes that have non-integers in the key' do
|
15
|
+
env = create_env.merge('rack.request.query_hash' => {non_array: {'0' => 'is integer', '1' => 'is integer', 'a' => 'is not integer'}})
|
16
|
+
Rack::JQueryParams.fix(env, :all)
|
17
|
+
assert env['rack.request.query_hash'][:non_array].is_a?(Hash)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should convert first-level hashes that resembles an array' do
|
21
|
+
env = create_env.merge('rack.request.query_hash' => {array: {'0' => 'is integer', '1' => 'is integer', '2' => 'is integer'}})
|
22
|
+
Rack::JQueryParams.fix(env, :all)
|
23
|
+
assert env['rack.request.query_hash'][:array].is_a?(Array)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should convert any level of hash that resembles an array' do
|
27
|
+
env = create_env.merge('rack.request.query_hash' => {non_array: {'a' => 'is not integer', :array => {'0' => 'is integer', '1' => 'is integer'}}})
|
28
|
+
Rack::JQueryParams.fix(env, :all)
|
29
|
+
assert env['rack.request.query_hash'][:non_array][:array].is_a?(Array)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should fix query_hash or form_hash' do
|
33
|
+
env = create_env.merge('rack.request.query_hash' => {array: {'0' => 'is integer', '1' => 'is integer', '2' => 'is integer'}})
|
34
|
+
Rack::JQueryParams.fix(env, :all)
|
35
|
+
assert env['rack.request.query_hash'][:array].is_a?(Array)
|
36
|
+
|
37
|
+
env = create_env.merge('rack.request.form_hash' => {array: {'0' => 'is integer', '1' => 'is integer', '2' => 'is integer'}})
|
38
|
+
Rack::JQueryParams.fix(env, :all)
|
39
|
+
assert env['rack.request.form_hash'][:array].is_a?(Array)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should apply to all methods' do
|
43
|
+
env = create_env.merge('rack.request.form_hash' => {array: {'0' => 'is integer', '1' => 'is integer', '2' => 'is integer'}})
|
44
|
+
env['REQUEST_METHOD'] = 'POST'
|
45
|
+
Rack::JQueryParams.fix(env, :all)
|
46
|
+
assert env['rack.request.form_hash'][:array].is_a?(Array)
|
47
|
+
|
48
|
+
env = create_env.merge('rack.request.query_hash' => {array: {'0' => 'is integer', '1' => 'is integer', '2' => 'is integer'}})
|
49
|
+
env['REQUEST_METHOD'] = 'GET'
|
50
|
+
Rack::JQueryParams.fix(env, :all)
|
51
|
+
assert env['rack.request.query_hash'][:array].is_a?(Array)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should allow apply to a single methods' do
|
55
|
+
env = create_env.merge('rack.request.query_hash' => {array: {'0' => 'is integer', '1' => 'is integer', '2' => 'is integer'}})
|
56
|
+
env['REQUEST_METHOD'] = 'GET'
|
57
|
+
Rack::JQueryParams.fix(env, :get)
|
58
|
+
assert env['rack.request.query_hash'][:array].is_a?(Array)
|
59
|
+
|
60
|
+
env = create_env.merge('rack.request.form_hash' => {array: {'0' => 'is integer', '1' => 'is integer', '2' => 'is integer'}})
|
61
|
+
env['REQUEST_METHOD'] = 'POST'
|
62
|
+
Rack::JQueryParams.fix(env, :get)
|
63
|
+
assert env['rack.request.form_hash'][:array].is_a?(Hash)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-jquery-params
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Caleb Clark
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- cclark@fanforce.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- .gitignore
|
35
|
+
- .yardopts
|
36
|
+
- .yardopts.rb
|
37
|
+
- Gemfile
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- lib/rack/jquery-params.rb
|
41
|
+
- lib/rack/jquery-params/load.rb
|
42
|
+
- lib/rack/jquery-params/version.rb
|
43
|
+
- rack-jquery-params.gemspec
|
44
|
+
- test/rack/params_test.rb
|
45
|
+
- test/test_helper.rb
|
46
|
+
homepage: http://github.com/calebclark/rack-jquery-params
|
47
|
+
licenses: []
|
48
|
+
metadata: {}
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 2.0.14
|
66
|
+
signing_key:
|
67
|
+
specification_version: 4
|
68
|
+
summary: Rack middleware that fixes the discrepancy between jQuery.param and how Rack
|
69
|
+
parses nested queries.
|
70
|
+
test_files:
|
71
|
+
- test/rack/params_test.rb
|
72
|
+
- test/test_helper.rb
|
73
|
+
has_rdoc:
|