http_router 0.6.6 → 0.6.7

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -2,7 +2,9 @@ require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
3
 
4
4
  desc "Run all tests"
5
- task :test => ['test:integration', 'test:examples']
5
+ task :test => ['test:integration', 'test:examples', 'test:rdoc_examples']
6
+
7
+ require 'pp'
6
8
 
7
9
  namespace :test do
8
10
  desc "Run integration tests"
@@ -52,6 +54,54 @@ namespace :test do
52
54
  end
53
55
  end
54
56
  end
57
+ desc "rdoc examples"
58
+ task :rdoc_examples do
59
+ $: << 'lib'
60
+ require 'http_router'
61
+ in_example = false
62
+ examples = []
63
+ STDOUT.sync = true
64
+ current_example = ''
65
+ rb_files = Dir['./lib/**/*.rb']
66
+ puts "Scanning #{rb_files * ', '}"
67
+ rb_files.each do |file|
68
+ lines = File.read(file).split(/\n/)
69
+ lines.each do |line|
70
+ if line[/^\s*#(.*)/] # comment
71
+ line = $1.strip
72
+ case line
73
+ when /^example:/i then in_example = true
74
+ when /^(?:# )?=+> (.*)/
75
+ expected = $1.strip
76
+ msg = expected.dup
77
+ msg << " was expected to be "
78
+ msg << "\#{__example_runner.inspect}"
79
+ current_example << "raise \"#{msg.gsub('"', '\\"')}\" unless __example_runner.strip == #{expected}\n" if in_example
80
+ when ''
81
+ unless current_example.empty?
82
+ examples << current_example
83
+ current_example = ''
84
+ end
85
+ in_example = false
86
+ else
87
+ current_example << "__example_runner = (" << line << ")\n" if in_example
88
+ end
89
+ else
90
+ unless current_example.empty?
91
+ examples << current_example
92
+ current_example = ''
93
+ end
94
+ in_example = false
95
+ end
96
+ end
97
+ end
98
+ puts "Running #{examples.size} example#{'s' if examples.size != 1}"
99
+ examples.each do |example|
100
+ print "."
101
+ eval(example)
102
+ end
103
+ puts " ✔"
104
+ end
55
105
  end
56
106
 
57
107
  require 'rake/rdoctask'
data/lib/http_router.rb CHANGED
@@ -154,7 +154,7 @@ class HttpRouter
154
154
  #
155
155
  # Example:
156
156
  # router = HttpRouter.new
157
- # router.add('/:foo.:format).name(:test).compile
157
+ # router.add('/:foo.:format').name(:test).to{|env| [200, {}, []]}
158
158
  # router.url(:test, 123, 'html')
159
159
  # # ==> "/123.html"
160
160
  # router.url(:test, 123, :format => 'html')
@@ -284,17 +284,22 @@ class HttpRouter
284
284
  path_obj
285
285
  end
286
286
 
287
+ def append_querystring_value(uri, key, value)
288
+ case value
289
+ when Array
290
+ k = "#{key}[]"
291
+ value.each{ |v| append_querystring_value(uri, k, v) }
292
+ when Hash
293
+ value.each{ |k, v| append_querystring_value(uri, "#{key}[#{k}]", v) }
294
+ else
295
+ uri << '&' << ::Rack::Utils.escape(key.to_s) << '=' << ::Rack::Utils.escape(value.to_s)
296
+ end
297
+ end
298
+
287
299
  def append_querystring(uri, params)
288
300
  if params && !params.empty?
289
301
  uri_size = uri.size
290
- params.each do |k,v|
291
- case v
292
- when Array
293
- v.each { |v_part| uri << '&' << ::Rack::Utils.escape(k.to_s) << '%5B%5D=' << ::Rack::Utils.escape(v_part.to_s) }
294
- else
295
- uri << '&' << ::Rack::Utils.escape(k.to_s) << '=' << ::Rack::Utils.escape(v.to_s)
296
- end
297
- end
302
+ params.each{ |k,v| append_querystring_value(uri, k, v) }
298
303
  uri[uri_size] = ??
299
304
  end
300
305
  uri
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  class HttpRouter #:nodoc
3
- VERSION = '0.6.6'
3
+ VERSION = '0.6.7'
4
4
  end
data/test/helper.rb CHANGED
@@ -71,6 +71,6 @@ class MiniTest::Unit::TestCase
71
71
  route = router.add(route)
72
72
  end
73
73
  route.to{|env| Rack::Response.new("Routing to #{route.to_s}").finish} if route && route.respond_to?(:to) && !route.dest
74
- assert_equal path, router.url(route, *args)
74
+ assert_equal path.gsub('[','%5B').gsub(']','%5D'), router.url(route, *args)
75
75
  end
76
76
  end
@@ -90,4 +90,16 @@ class TestGenerate < MiniTest::Unit::TestCase
90
90
  r = router { add(':var').matching(:var => /\d+/) }
91
91
  assert_raises(HttpRouter::InvalidRouteException) { router.url(r, 'asd') }
92
92
  end
93
+
94
+ def test_array
95
+ assert_generate '/var?foo[]=baz&foo[]=bar', '/var', :foo => ['baz', 'bar']
96
+ end
97
+
98
+ def test_hash
99
+ assert_generate '/var?foo[az]=baz&foo[ar]=bar', '/var', :foo => {:az => 'baz', :ar => 'bar'}
100
+ end
101
+
102
+ def test_hash_with_array
103
+ assert_generate '/var?foo[az]=baz&foo[ar][]=bar&foo[ar][]=barz', '/var', :foo => {:az => 'baz', :ar => ['bar', 'barz']}
104
+ end
93
105
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_router
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 6
10
- version: 0.6.6
9
+ - 7
10
+ version: 0.6.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hull
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-27 00:00:00 -07:00
18
+ date: 2011-03-28 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency