deas 0.21.0 → 0.22.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/deas/cgi.rb +31 -0
- data/lib/deas/url.rb +29 -9
- data/lib/deas/version.rb +1 -1
- data/test/unit/cgi_tests.rb +36 -0
- data/test/unit/url_tests.rb +13 -2
- metadata +7 -4
data/lib/deas/cgi.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Deas
|
2
|
+
|
3
|
+
module Cgi
|
4
|
+
# taken from http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/index.html
|
5
|
+
# => not requiring 'cgi' to save on memory usage
|
6
|
+
|
7
|
+
def self.escape(string)
|
8
|
+
string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
|
9
|
+
'%' + $1.unpack('H2' * $1.size).join('%').upcase
|
10
|
+
end.gsub(' ', '%20')
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.http_query(value, key_ns = nil)
|
14
|
+
# optimized version taken from:
|
15
|
+
# http://github.com/kelredd/useful/blob/master/lib/useful/ruby_extensions/hash.rb
|
16
|
+
value.sort{|a,b| a[0].to_s <=> b[0].to_s}.collect do |key_val|
|
17
|
+
key, val = key_val
|
18
|
+
key_s = key_ns ? "#{key_ns}[#{key_val[0].to_s}]" : key_val[0].to_s
|
19
|
+
if key_val[1].kind_of?(::Array)
|
20
|
+
key_val[1].sort.collect{|i| "#{key_s}[]=#{Deas::Cgi.escape(i.to_s)}"}.join('&')
|
21
|
+
elsif key_val[1].kind_of?(::Hash)
|
22
|
+
Deas::Cgi.http_query(key_val[1], key_s)
|
23
|
+
else
|
24
|
+
"#{key_s}=#{Deas::Cgi.escape(key_val[1].to_s)}"
|
25
|
+
end
|
26
|
+
end.join('&')
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/lib/deas/url.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'deas/cgi'
|
2
|
+
|
1
3
|
module Deas
|
2
4
|
class Url
|
3
5
|
|
@@ -8,25 +10,43 @@ module Deas
|
|
8
10
|
end
|
9
11
|
|
10
12
|
def path_for(*args)
|
11
|
-
|
13
|
+
hashed, ordered = [
|
12
14
|
args.last.kind_of?(::Hash) ? args.pop : {},
|
13
15
|
args
|
14
16
|
]
|
15
|
-
|
17
|
+
apply_ordered(apply_hashed(@path, hashed), ordered)
|
16
18
|
end
|
17
19
|
|
18
20
|
private
|
19
21
|
|
20
|
-
def
|
22
|
+
def apply_ordered(path, params)
|
23
|
+
params.inject(path){ |p, v| p.sub(/\*+|\:\w+/i, v.to_s) }.gsub(/\/\/+/, '/')
|
24
|
+
end
|
25
|
+
|
26
|
+
def apply_hashed(path, params)
|
21
27
|
# ignore captures in applying params
|
22
|
-
captures
|
23
|
-
splat
|
24
|
-
|
25
|
-
params.inject(splat_path){ |p, (k, v)| p.gsub(":#{k}", v.to_s) }
|
28
|
+
captures = params.delete(:captures) || params.delete('captures') || []
|
29
|
+
splat = params.delete(:splat) || params.delete('splat') || []
|
30
|
+
apply_extra(apply_named(apply_splat(@path, splat), params), params)
|
26
31
|
end
|
27
32
|
|
28
|
-
def
|
29
|
-
params.inject(path){ |p, v| p.sub(
|
33
|
+
def apply_splat(path, params)
|
34
|
+
params.inject(path){ |p, v| p.sub(/\*+/, v.to_s) }
|
35
|
+
end
|
36
|
+
|
37
|
+
def apply_named(path, params)
|
38
|
+
params.inject(path) do |p, (k, v)|
|
39
|
+
if p.include?(":#{k}")
|
40
|
+
params.delete(k)
|
41
|
+
p.gsub(":#{k}", v.to_s)
|
42
|
+
else
|
43
|
+
p
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def apply_extra(path, params)
|
49
|
+
params.empty? ? path : "#{path}?#{Deas::Cgi.http_query(params)}"
|
30
50
|
end
|
31
51
|
|
32
52
|
end
|
data/lib/deas/version.rb
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'deas/cgi'
|
3
|
+
|
4
|
+
module Deas::Cgi
|
5
|
+
|
6
|
+
class UnitTests < Assert::Context
|
7
|
+
desc "Deas::Cgi"
|
8
|
+
subject{ Deas::Cgi }
|
9
|
+
|
10
|
+
should have_imeths :escape, :http_query
|
11
|
+
|
12
|
+
should "cgi-escape data values" do
|
13
|
+
exp = "Right%21%20%5BSaid%5D%20Fred.%0D%0A"
|
14
|
+
assert_equal exp, Deas::Cgi.escape("Right! [Said] Fred.\r\n")
|
15
|
+
end
|
16
|
+
|
17
|
+
should "create http query strings" do
|
18
|
+
exp = "name=thomas%20hardy%20%2F%20thomas%20handy"
|
19
|
+
assert_equal exp, Deas::Cgi.http_query(:name => 'thomas hardy / thomas handy')
|
20
|
+
|
21
|
+
exp = "id=23423&since=2009-10-14"
|
22
|
+
assert_equal exp, Deas::Cgi.http_query(:id => 23423, :since => "2009-10-14")
|
23
|
+
|
24
|
+
exp = "id[]=1&id[]=2"
|
25
|
+
assert_equal exp, Deas::Cgi.http_query(:id => [1,2])
|
26
|
+
|
27
|
+
exp = "poo[bar]=2&poo[foo]=1"
|
28
|
+
assert_equal exp, Deas::Cgi.http_query(:poo => {:foo => 1, :bar => 2})
|
29
|
+
|
30
|
+
exp = "poo[bar][bar1]=1&poo[bar][bar2]=nasty&poo[foo]=1"
|
31
|
+
assert_equal exp, Deas::Cgi.http_query(:poo => {:foo => 1, :bar => {:bar1 => 1, :bar2 => "nasty"}})
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/test/unit/url_tests.rb
CHANGED
@@ -5,7 +5,7 @@ require 'test/support/view_handlers'
|
|
5
5
|
|
6
6
|
class Deas::Url
|
7
7
|
|
8
|
-
class
|
8
|
+
class UnitTests < Assert::Context
|
9
9
|
desc "Deas::Url"
|
10
10
|
setup do
|
11
11
|
@url = Deas::Url.new(:get_info, '/info')
|
@@ -22,7 +22,7 @@ class Deas::Url
|
|
22
22
|
|
23
23
|
end
|
24
24
|
|
25
|
-
class PathForTests <
|
25
|
+
class PathForTests < UnitTests
|
26
26
|
desc "when generating paths"
|
27
27
|
setup do
|
28
28
|
@url = Deas::Url.new(:some_thing, '/:some/:thing/*/*')
|
@@ -50,6 +50,17 @@ class Deas::Url
|
|
50
50
|
})
|
51
51
|
end
|
52
52
|
|
53
|
+
should "append other (additional) params as query params" do
|
54
|
+
exp_path = "/a/goose/cooked/well?aye=a%20a%20a&bee=b"
|
55
|
+
assert_equal exp_path, subject.path_for({
|
56
|
+
'some' => 'a',
|
57
|
+
:thing => 'goose',
|
58
|
+
'splat' => ['cooked', 'well'],
|
59
|
+
'bee' => 'b',
|
60
|
+
:aye => 'a a a'
|
61
|
+
})
|
62
|
+
end
|
63
|
+
|
53
64
|
should "generate given ordered params only" do
|
54
65
|
exp_path = "/a/:thing/*/*"
|
55
66
|
assert_equal exp_path, subject.path_for('a')
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deas
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 71
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 22
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.22.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kelly Redding
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2013-
|
19
|
+
date: 2013-10-03 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
name: ns-options
|
@@ -145,6 +145,7 @@ files:
|
|
145
145
|
- Rakefile
|
146
146
|
- deas.gemspec
|
147
147
|
- lib/deas.rb
|
148
|
+
- lib/deas/cgi.rb
|
148
149
|
- lib/deas/error_handler.rb
|
149
150
|
- lib/deas/exceptions.rb
|
150
151
|
- lib/deas/logging.rb
|
@@ -184,6 +185,7 @@ files:
|
|
184
185
|
- test/support/views/some_no_engine_extension
|
185
186
|
- test/support/views/with_layout.erb
|
186
187
|
- test/system/rack_tests.rb
|
188
|
+
- test/unit/cgi_tests.rb
|
187
189
|
- test/unit/error_handler_tests.rb
|
188
190
|
- test/unit/exceptions_tests.rb
|
189
191
|
- test/unit/logging_tests.rb
|
@@ -254,6 +256,7 @@ test_files:
|
|
254
256
|
- test/support/views/some_no_engine_extension
|
255
257
|
- test/support/views/with_layout.erb
|
256
258
|
- test/system/rack_tests.rb
|
259
|
+
- test/unit/cgi_tests.rb
|
257
260
|
- test/unit/error_handler_tests.rb
|
258
261
|
- test/unit/exceptions_tests.rb
|
259
262
|
- test/unit/logging_tests.rb
|