usher 0.5.6 → 0.5.7

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,4 +1,7 @@
1
- require 'lib/usher'
1
+ libdir = File.expand_path("lib")
2
+ $:.unshift(libdir) unless $:.include?(libdir)
3
+
4
+ require 'usher'
2
5
 
3
6
  begin
4
7
  require 'jeweler'
@@ -12,6 +15,7 @@ begin
12
15
  s.add_dependency 'fuzzyhash', '>=0.0.6'
13
16
  s.rubyforge_project = 'joshbuddy-usher'
14
17
  end
18
+ Jeweler::GemcutterTasks.new
15
19
  Jeweler::RubyforgeTasks.new do |rubyforge|
16
20
  rubyforge.doc_task = "rdoc"
17
21
  rubyforge.remote_doc_path = ''
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 5
3
- :patch: 6
3
+ :patch: 7
4
4
  :major: 0
data/lib/usher.rb CHANGED
@@ -9,7 +9,7 @@ require File.join(File.dirname(__FILE__), 'usher', 'util')
9
9
  class Usher
10
10
  attr_reader :root, :named_routes, :routes, :splitter,
11
11
  :delimiters, :delimiter_chars, :delimiters_regex,
12
- :parent_route, :generator
12
+ :parent_route, :generator, :grapher
13
13
 
14
14
  # Returns whether the route set is empty
15
15
  #
@@ -1,37 +1,43 @@
1
+ # From Extlib
2
+ module CamelCaseMixin
3
+ def camel_case
4
+ return self if self !~ /_/ && self =~ /[A-Z]+.*/
5
+ split('_').map{|e| e.capitalize}.join
6
+ end
7
+ end
8
+
9
+ # TODO: refactoring: I suggest to use usher/interfaces/rack.rb instead of
10
+ # usher/interface/rack_interface.rb, it will enable me to simplify this code
1
11
  class Usher
2
12
  module Interface
3
- autoload :Rails2_2Interface, File.join(File.dirname(__FILE__), 'interface', 'rails2_2_interface')
4
- autoload :Rails2_3Interface, File.join(File.dirname(__FILE__), 'interface', 'rails2_3_interface')
5
- autoload :MerbInterface, File.join(File.dirname(__FILE__), 'interface', 'merb_interface')
6
- autoload :RackInterface, File.join(File.dirname(__FILE__), 'interface', 'rack_interface')
7
- autoload :EmailInterface, File.join(File.dirname(__FILE__), 'interface', 'email_interface')
8
- autoload :Rails3Interface, File.join(File.dirname(__FILE__), 'interface', 'rails3_interface')
9
- autoload :TextInterface, File.join(File.dirname(__FILE__), 'interface', 'text_interface')
10
-
11
- def self.for(type, &blk)
12
- class_for(type).new(&blk)
13
+ # Get root directory of interfaces of path to specified interface
14
+ def self.interface_directory
15
+ File.join(File.dirname(__FILE__), "interface")
16
+ end
17
+
18
+ # path to file
19
+ def self.interface_path(name)
20
+ File.join(self.interface_directory, "#{name}_interface.rb")
21
+ end
22
+
23
+ # Usher::Interface.for(:rack, &block)
24
+ def self.for(name, &block)
25
+ if File.exist?(self.interface_path(name))
26
+ require self.interface_path(name)
27
+ snake_cased = "#{name}_interface".extend(CamelCaseMixin)
28
+ Usher::Interface.const_get(snake_cased.camel_case).new(&block)
29
+ else
30
+ raise ArgumentError, "Interface #{name} doesn't exist. Choose one of: #{self.interfaces.inspect}"
31
+ end
13
32
  end
14
-
15
- def self.class_for(type)
16
- case type
17
- when :rails2_2
18
- Rails2_2Interface
19
- when :rails2_3
20
- Rails2_3Interface
21
- when :merb
22
- MerbInterface
23
- when :rack
24
- RackInterface
25
- when :email
26
- EmailInterface
27
- when :rails3
28
- Rails3Interface
29
- when :text
30
- TextInterface
33
+
34
+ # Array of symbols
35
+ # Usher::Interface.interfaces
36
+ # => [:email_interface, :merb_interface, :rack_interface, :rails2_2_interface, :rails2_3_interface, :rails3_interface, :text_interface]
37
+ def self.interfaces
38
+ Dir["#{self.interface_directory}/*.rb"].map do |interface|
39
+ File.basename(interface).sub("_interface.rb", "").to_sym
31
40
  end
32
-
33
41
  end
34
-
35
-
36
42
  end
37
- end
43
+ end
@@ -3,16 +3,7 @@ require 'rack'
3
3
  class Usher
4
4
  module Interface
5
5
  class RackInterface
6
-
7
- attr_reader :router
8
- attr_accessor :app
9
-
10
- DEFAULT_APPLICATION = lambda do |env|
11
- Rack::Response.new("No route found", 404).finish
12
- end
13
-
14
6
  class Builder < Rack::Builder
15
-
16
7
  def initialize(&block)
17
8
  @usher = Usher::Interface::RackInterface.new
18
9
  super
@@ -23,10 +14,28 @@ class Usher
23
14
  @ins << @usher unless @ins.last == @usher
24
15
  end
25
16
 
17
+ def get(path, options = nil, &block)
18
+ self.map(path, options.merge!(:conditions => {:request_method => "GET"}), &block)
19
+ end
20
+
21
+ def post(path, options = nil, &block)
22
+ self.map(path, options.merge!(:conditions => {:request_method => "POST"}), &block)
23
+ end
24
+
25
+ def put(path, options = nil, &block)
26
+ self.map(path, options.merge!(:conditions => {:request_method => "PUT"}), &block)
27
+ end
28
+
29
+ def delete(path, options = nil, &block)
30
+ self.map(path, options.merge!(:conditions => {:request_method => "DELETE"}), &block)
31
+ end
26
32
  end
27
33
 
34
+ attr_reader :router
35
+ attr_accessor :app
36
+
28
37
  def initialize(app = nil, &blk)
29
- @app = app || DEFAULT_APPLICATION
38
+ @app = app || lambda { |env| Rack::Response.new("No route found", 404).finish }
30
39
  @router = Usher.new(:request_methods => [:request_method, :host, :port, :scheme], :generator => Usher::Util::Generators::URL.new)
31
40
  instance_eval(&blk) if blk
32
41
  end
@@ -42,7 +51,36 @@ class Usher
42
51
 
43
52
  def add(path, options = nil)
44
53
  @router.add_route(path, options)
45
- end
54
+ end
55
+
56
+ # default { |env| ... }
57
+ # default DefaultApp
58
+ def default(app = nil, &block)
59
+ @app = app ? app : block
60
+ end
61
+
62
+ # shortcuts for adding routes for HTTP methods, for example:
63
+ # add("/url", :conditions => {:request_method => "POST"}})
64
+ # is the same as:
65
+ # post("/url")
66
+ #
67
+ # if you need more complex setup, use method add directly, for example:
68
+ # add("/url", :conditions => {:request_method => ["POST", "PUT"]}})
69
+ def get(path, options = {})
70
+ self.add(path, options.merge!(:conditions => {:request_method => "GET"}))
71
+ end
72
+
73
+ def post(path, options = {})
74
+ self.add(path, options.merge!(:conditions => {:request_method => "POST"}))
75
+ end
76
+
77
+ def put(path, options = {})
78
+ self.add(path, options.merge!(:conditions => {:request_method => "PUT"}))
79
+ end
80
+
81
+ def delete(path, options = {})
82
+ self.add(path, options.merge!(:conditions => {:request_method => "DELETE"}))
83
+ end
46
84
 
47
85
  def parent_route=(route)
48
86
  @router.parent_route = route
@@ -57,8 +95,9 @@ class Usher
57
95
  end
58
96
 
59
97
  def call(env)
60
- response = @router.recognize(request = Rack::Request.new(env), request.path_info)
61
- after_match(env, response) if response
98
+ request = Rack::Request.new(env)
99
+ response = @router.recognize(request, request.path_info)
100
+ after_match(request, response) if response
62
101
  determine_respondant(response).call(env)
63
102
  end
64
103
 
@@ -70,18 +109,18 @@ class Usher
70
109
  # and calling the application
71
110
  #
72
111
  # @api plugin
73
- def after_match(env, response)
112
+ def after_match(request, response)
74
113
  params = response.path.route.default_values ?
75
- response.path.route.default_values.merge(Hash[response.params]) :
76
- Hash[response.params]
114
+ response.path.route.default_values.merge(Hash[*response.params.flatten]) :
115
+ Hash[*response.params.flatten]
77
116
 
78
- env['usher.params'] ?
79
- env['usher.params'].merge!(params) :
80
- env['usher.params'] = params
117
+ request.env['usher.params'] ?
118
+ request.env['usher.params'].merge!(params) :
119
+ (request.env['usher.params'] = params)
81
120
 
82
121
  # consume the path_info to the script_name
83
122
  # response.remaining_path
84
- consume_path!(env, response) if response.partial_match?
123
+ consume_path!(request, response) if response.partial_match?
85
124
  end
86
125
 
87
126
  # Determines which application to respond with.
@@ -102,9 +141,9 @@ class Usher
102
141
  end
103
142
 
104
143
  # Consume the path from path_info to script_name
105
- def consume_path!(env, response)
106
- env["SCRIPT_NAME"] = (env["SCRIPT_NAME"] + response.matched_path) || ""
107
- env["PATH_INFO"] = response.remaining_path || ""
144
+ def consume_path!(request, response)
145
+ request.env["SCRIPT_NAME"] = (request.env["SCRIPT_NAME"] + response.matched_path) || ""
146
+ request.env["PATH_INFO"] = response.remaining_path || ""
108
147
  end
109
148
  end
110
149
  end
@@ -2,8 +2,15 @@ class Usher
2
2
  module Interface
3
3
  class RackInterface
4
4
  module Route
5
-
5
+ # add("/index.html").redirect("/")
6
+ def redirect(path, status = 302)
7
+ lambda do
8
+ response = Rack::Response.new
9
+ response.redirect(path, status)
10
+ response.finish
11
+ end
12
+ end
6
13
  end
7
14
  end
8
15
  end
9
- end
16
+ end
@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), 'rails2_2_interface', 'mapper')
2
2
 
3
3
  class Usher
4
4
  module Interface
5
- class Rails2_2Interface
5
+ class Rails22Interface
6
6
 
7
7
  attr_reader :usher
8
8
  attr_accessor :configuration_file
@@ -1,6 +1,6 @@
1
1
  class Usher
2
2
  module Interface
3
- class Rails2_2Interface
3
+ class Rails22Interface
4
4
 
5
5
  class Mapper #:doc:
6
6
  def initialize(set) #:nodoc:
@@ -41,4 +41,4 @@ class Usher
41
41
 
42
42
  end
43
43
  end
44
- end
44
+ end
@@ -1,6 +1,6 @@
1
1
  class Usher
2
2
  module Interface
3
- class Rails2_3Interface
3
+ class Rails23Interface
4
4
 
5
5
  attr_reader :configuration_files
6
6
 
@@ -131,4 +131,4 @@ class Usher
131
131
 
132
132
  end
133
133
  end
134
- end
134
+ end
data/lib/usher/route.rb CHANGED
@@ -57,7 +57,7 @@ class Usher
57
57
  # route.to(:controller => 'testing', :action => 'index')
58
58
  # set.recognize(Request.new('/test')).first.params => {:controller => 'testing', :action => 'index'}
59
59
  def to(options = nil, &block)
60
- raise "cannot set destintaion as block and argument" if block_given? && options
60
+ raise "cannot set destination as block and argument" if block_given? && options
61
61
  @destination = if block_given?
62
62
  block
63
63
  else
@@ -22,7 +22,7 @@ class Usher
22
22
  (lval.size * rval.size).times do |index|
23
23
  val = []
24
24
  val.push(*lval[index % lval.size])
25
- val.push(*rval[index % rval.size])
25
+ val.push(*rval[index / lval.size])
26
26
  product << val
27
27
  end
28
28
  lval.replace(product)
@@ -1,4 +1,5 @@
1
- require 'lib/usher'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require "usher"
2
3
 
3
4
  def build_email_mock(email)
4
5
  request = mock "Request"
@@ -33,4 +34,4 @@ describe "Usher (for email) route recognition" do
33
34
  @route_set.act('sub+ect.123-456-sdqwe123ae@mydomain.org')
34
35
  end
35
36
 
36
- end
37
+ end
@@ -1,4 +1,5 @@
1
- require 'lib/usher'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+ require "usher"
2
3
  require 'rack'
3
4
 
4
5
  describe "Usher URL generation" do
@@ -1,5 +1,5 @@
1
- require 'lib/usher'
2
-
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+ require "usher"
3
3
 
4
4
  describe "Usher grapher" do
5
5
 
@@ -37,4 +37,4 @@ describe "Usher grapher" do
37
37
  # proc{ route_set.generate_url(nil, {:a => 'A', :b => 'B'})}.should raise_error Usher::ValidationException
38
38
  #end
39
39
 
40
- end
40
+ end
@@ -1,4 +1,5 @@
1
- require 'lib/usher'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+ require "usher"
2
3
 
3
4
  describe "Usher route tokenizing" do
4
5
 
@@ -72,4 +73,4 @@ describe "Usher route tokenizing" do
72
73
  parts[1].should == parts[3]
73
74
  end
74
75
 
75
- end
76
+ end
@@ -1,4 +1,5 @@
1
- require 'lib/usher'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+ require "usher"
2
3
 
3
4
  route_set = Usher.new
4
5
 
@@ -22,14 +23,13 @@ describe "Usher route adding" do
22
23
  it "should add every kind of optional route possible" do
23
24
  route_set.add_route('/a/b(/c)(/d(/e))')
24
25
  route_set.routes.first.paths.collect{|a| a.parts }.should == [
25
- ['/', "a", '/', "b"],
26
- ['/', "a", '/', "b", '/', "c", '/', "d"],
27
- ['/', "a", '/', "b", '/', "d", '/', "e"],
28
- ['/', "a", '/', "b", '/', "c"],
29
- ['/', "a", '/', "b", '/', "d"],
30
- ['/', "a", '/', "b", '/', "c", '/', "d", '/', "e"]
26
+ ["/", "a", "/", "b"],
27
+ ["/", "a", "/", "b", "/", "c"],
28
+ ["/", "a", "/", "b", "/", "d"],
29
+ ["/", "a", "/", "b", "/", "c", "/", "d"],
30
+ ["/", "a", "/", "b", "/", "d", "/", "e"],
31
+ ["/", "a", "/", "b", "/", "c", "/", "d", "/", "e"]
31
32
  ]
32
-
33
33
  end
34
34
 
35
35
  it "should allow named routes to be added" do
@@ -73,4 +73,4 @@ describe "Usher route adding" do
73
73
 
74
74
  end
75
75
 
76
- end
76
+ end
@@ -1,7 +1,7 @@
1
- require 'lib/usher'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require "usher"
2
3
  require 'rack'
3
4
 
4
- require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
5
5
  route_set = Usher::Interface.for(:rack)
6
6
  route_set.extend(CallWithMockRequestMixin)
7
7
 
@@ -1,7 +1,7 @@
1
- require 'lib/usher'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require "usher"
2
3
  require 'rack'
3
4
 
4
- require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
5
5
  route_set = Usher::Interface.for(:rack)
6
6
  route_set.extend(CallWithMockRequestMixin)
7
7
 
@@ -1 +1,2 @@
1
- require 'activesupport'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require 'activesupport'
@@ -1,5 +1,6 @@
1
- require File.join(File.dirname(__FILE__), 'compat')
2
- require 'lib/usher'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), 'compat'))
3
+ require "usher"
3
4
 
4
5
  route_set = Usher::Interface.for(:rails2_2)
5
6
 
@@ -25,4 +26,4 @@ describe "Usher (for rails 2.2) URL generation" do
25
26
  route_set.generate({:action => 'thingy'}, {:controller => 'sample', :action => 'index', :id => 123}, :generate).should == '/sample/thingy'
26
27
  end
27
28
 
28
- end
29
+ end
@@ -1,5 +1,6 @@
1
- require File.join(File.dirname(__FILE__), 'compat')
2
- require 'lib/usher'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), 'compat'))
3
+ require "usher"
3
4
 
4
5
  route_set = Usher::Interface.for(:rails2_2)
5
6
 
@@ -13,4 +14,4 @@ describe "Usher (for rails 2.2) route adding" do
13
14
  proc { route_set.add_route('/bad/route') }.should raise_error
14
15
  end
15
16
 
16
- end
17
+ end
@@ -1,5 +1,6 @@
1
- require File.join(File.dirname(__FILE__), 'compat')
2
- require 'lib/usher'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), 'compat'))
3
+ require "usher"
3
4
  require 'action_controller'
4
5
 
5
6
  route_set = Usher::Interface.for(:rails2_2)
@@ -76,4 +77,4 @@ describe "Usher (for rails 2.2) route recognition" do
76
77
  end
77
78
 
78
79
 
79
- end
80
+ end
@@ -1 +1,2 @@
1
- require 'activesupport'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require 'activesupport'
@@ -1,5 +1,6 @@
1
- require File.join(File.dirname(__FILE__), 'compat')
2
- require 'lib/usher'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), 'compat'))
3
+ require "usher"
3
4
 
4
5
  route_set = Usher::Interface.for(:rails2_3)
5
6
 
@@ -25,4 +26,4 @@ describe "Usher (for rails 2.3) URL generation" do
25
26
  route_set.generate({:action => 'thingy'}, {:controller => 'sample', :action => 'index', :id => 123}, :generate).should == '/sample/thingy'
26
27
  end
27
28
 
28
- end
29
+ end
@@ -1,5 +1,6 @@
1
- require File.join(File.dirname(__FILE__), 'compat')
2
- require 'lib/usher'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), 'compat'))
3
+ require "usher"
3
4
 
4
5
  route_set = Usher::Interface.for(:rails2_3)
5
6
 
@@ -13,4 +14,4 @@ describe "Usher (for rails 2.3) route adding" do
13
14
  proc { route_set.add_route('/bad/route') }.should raise_error
14
15
  end
15
16
 
16
- end
17
+ end
@@ -1,5 +1,6 @@
1
- require File.join(File.dirname(__FILE__), 'compat')
2
- require 'lib/usher'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), 'compat'))
3
+ require "usher"
3
4
  require 'action_controller'
4
5
 
5
6
  route_set = Usher::Interface.for(:rails2_3)
@@ -76,4 +77,4 @@ describe "Usher (for rails 2.3) route recognition" do
76
77
  end
77
78
 
78
79
 
79
- end
80
+ end
@@ -1,4 +1,5 @@
1
- require 'lib/usher'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+ require "usher"
2
3
 
3
4
  route_set = Usher.new
4
5
 
@@ -208,6 +209,14 @@ describe "Usher route recognition" do
208
209
  proc {route_set.recognize(build_request({:method => 'get', :path => '/products/show/qweasd', :domain => 'admin.host.com'}))}.should raise_error
209
210
  end
210
211
 
212
+ it "should recognize multiple optional parts" do
213
+ target_route = route_set.add_route('/test(/this)(/too)')
214
+ route_set.recognize_path('/test').path.route.should == target_route
215
+ route_set.recognize_path('/test/this').path.route.should == target_route
216
+ route_set.recognize_path('/test/too').path.route.should == target_route
217
+ route_set.recognize_path('/test/this/too').path.route.should == target_route
218
+ end
219
+
211
220
  describe "partial recognition" do
212
221
  it "should partially match a route" do
213
222
  route = route_set.add_route("/foo")
@@ -1,4 +1,5 @@
1
- require 'lib/usher'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+ require "usher"
2
3
 
3
4
  describe "Usher request method" do
4
5
 
@@ -12,4 +13,4 @@ describe "Usher request method" do
12
13
  end
13
14
 
14
15
 
15
- end
16
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,6 @@
1
+ libdir = File.expand_path("lib")
2
+ $:.unshift(libdir) unless $:.include?(libdir)
3
+
1
4
  module CallWithMockRequestMixin
2
5
  def call_with_mock_request(url = "/sample", method = "GET", params = Hash.new)
3
6
  params.merge!(:method => method)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.6
4
+ version: 0.5.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Hull
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-15 00:00:00 -06:00
12
+ date: 2009-09-25 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency