Class: HttpRouter::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/http_router/route.rb

Direct Known Subclasses

RegexRoute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(router, path, opts = {}) ⇒ Route

Returns a new instance of Route.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/http_router/route.rb', line 7

def initialize(router, path, opts = {})
  @router = router
  @original_path = path
  @path = path
  @opts = opts
  @arbitrary = opts[:arbitrary] || opts[:__arbitrary__]
  @conditions = opts[:conditions] || opts[:__conditions__] || {}
  name(opts.delete(:name)) if opts.key?(:name)
  @matches_with = {}
  @default_values = opts[:default_values] || {}
  if @original_path[-1] == ?*
    @match_partially = true
    path.slice!(-1)
  end
  @paths = OptionalCompiler.new(path).paths
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



5
6
7
# File 'lib/http_router/route.rb', line 5

def conditions
  @conditions
end

#default_valuesObject (readonly)

Returns the value of attribute default_values.



5
6
7
# File 'lib/http_router/route.rb', line 5

def default_values
  @default_values
end

#matches_withObject (readonly)

Returns the value of attribute matches_with.



5
6
7
# File 'lib/http_router/route.rb', line 5

def matches_with
  @matches_with
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/http_router/route.rb', line 5

def path
  @path
end

#routerObject (readonly)

Returns the value of attribute router.



5
6
7
# File 'lib/http_router/route.rb', line 5

def router
  @router
end

Instance Method Details

#arbitrary(blk = nil, &blk2) ⇒ Object



115
116
117
118
119
# File 'lib/http_router/route.rb', line 115

def arbitrary(blk = nil, &blk2)
  arbitrary_with_continue { |req, params|
    req.continue[(blk || blk2)[req, params]]
  }
end

#arbitrary_with_continue(blk = nil, &blk2) ⇒ Object



121
122
123
124
# File 'lib/http_router/route.rb', line 121

def arbitrary_with_continue(blk = nil, &blk2)
  (@arbitrary ||= []) << (blk || blk2)
  self
end

#as_optionsObject



24
25
26
# File 'lib/http_router/route.rb', line 24

def as_options
  {:matching => @matches_with, :conditions => @conditions, :default_values => @default_values, :name => @name, :partial => @partially_match, :arbitrary => @arbitrary}
end

#clone(new_router) ⇒ Object



131
132
133
# File 'lib/http_router/route.rb', line 131

def clone(new_router)
  Route.new(new_router, @original_path.dup, as_options)
end

#compiled?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/http_router/route.rb', line 51

def compiled?
  @compiled
end

#default(defaults) ⇒ Object



82
83
84
85
# File 'lib/http_router/route.rb', line 82

def default(defaults)
  (@default_values ||= {}).merge!(defaults)
  self
end

#deleteObject



112
# File 'lib/http_router/route.rb', line 112

def delete; request_method('DELETE'); end

#destObject



37
38
39
# File 'lib/http_router/route.rb', line 37

def dest
  @app
end

#getObject



110
# File 'lib/http_router/route.rb', line 110

def get;    request_method('GET');    end

#headObject



113
# File 'lib/http_router/route.rb', line 113

def head;   request_method('HEAD');   end

#host(host) ⇒ Object



65
66
67
# File 'lib/http_router/route.rb', line 65

def host(host)
  ((@conditions ||= {})[:host] ||= []) << host; self
end

#match_partially?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/http_router/route.rb', line 33

def match_partially?
  @match_partially
end

#matching(matchers) ⇒ Object



77
78
79
80
# File 'lib/http_router/route.rb', line 77

def matching(matchers)
  @opts.merge!(matchers)
  self
end

#matching_path(params, other_hash = nil) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/http_router/route.rb', line 154

def matching_path(params, other_hash = nil)
  if @paths.size == 1
    @paths.first
  else
    if params.is_a?(Array)
      significant_keys = other_hash && significant_variable_names & other_hash.keys
      @paths.find { |path|
        var_count = significant_keys ? params.size + significant_keys.size : params.size
        path.param_names.size == var_count
      }
    else
      @paths.reverse_each do |path|
        if params && !params.empty?
          return path if (path.param_names & params.keys).size == path.param_names.size
        elsif path.param_names.empty?
          return path
        end
      end
      nil
    end
  end
end

#name(n) ⇒ Object



55
56
57
58
59
# File 'lib/http_router/route.rb', line 55

def name(n)
  @name = n
  @router.named_routes[n] = self
  self
end

#namedObject



177
178
179
# File 'lib/http_router/route.rb', line 177

def named
  @name
end

#partial(match_partially = true) ⇒ Object



28
29
30
31
# File 'lib/http_router/route.rb', line 28

def partial(match_partially = true)
  @match_partially = match_partially
  self
end

#postObject



109
# File 'lib/http_router/route.rb', line 109

def post;   request_method('POST');   end

#putObject



111
# File 'lib/http_router/route.rb', line 111

def put;    request_method('PUT');    end

#redirect(path, status = 302) ⇒ Object

Sets the destination of this route to redirect to an arbitrary URL.

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
97
# File 'lib/http_router/route.rb', line 88

def redirect(path, status = 302)
  raise ArgumentError, "Status has to be an integer between 300 and 399" unless (300..399).include?(status)
  to { |env|
    params = env['router.params']
    response = ::Rack::Response.new
    response.redirect(eval(%|"#{path}"|), status)
    response.finish
  }
  self
end

#regex?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/http_router/route.rb', line 41

def regex?
  false
end

#request_method(m) ⇒ Object



61
62
63
# File 'lib/http_router/route.rb', line 61

def request_method(m)
  ((@conditions ||= {})[:request_method] ||= []) << m; self
end

#scheme(scheme) ⇒ Object



69
70
71
# File 'lib/http_router/route.rb', line 69

def scheme(scheme)
  ((@conditions ||= {})[:scheme] ||= []) << scheme; self
end

#significant_variable_namesObject



150
151
152
# File 'lib/http_router/route.rb', line 150

def significant_variable_names
  @significant_variable_names ||= @path.scan(/(^|[^\\])[:\*]([a-zA-Z0-9_]+)/).map{|p| p.last.to_sym}
end

#static(root) ⇒ Object

Sets the destination of this route to serve static files from either a directory or a single file.



100
101
102
103
104
105
106
107
# File 'lib/http_router/route.rb', line 100

def static(root)
  if File.directory?(root)
    partial.to ::Rack::File.new(root)
  else
    to {|env| env['PATH_INFO'] = File.basename(root); ::Rack::File.new(File.dirname(root)).call(env) }
  end
  self
end

#to(dest = nil, &dest2) ⇒ Object



45
46
47
48
49
# File 'lib/http_router/route.rb', line 45

def to(dest = nil, &dest2)
  @app = dest || dest2
  compile
  self
end

#to_sObject



181
182
183
# File 'lib/http_router/route.rb', line 181

def to_s
  "#<HttpRouter:Route #{object_id} @original_path=#{@original_path.inspect} @conditions=#{@conditions.inspect} @arbitrary=#{@arbitrary.inspect}>"
end

#url(*args) ⇒ Object



126
127
128
129
# File 'lib/http_router/route.rb', line 126

def url(*args)
  result, extra_params = url_with_params(*args)
  append_querystring(result, extra_params)
end

#url_with_params(*args) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/http_router/route.rb', line 135

def url_with_params(*args)
  options = args.last.is_a?(Hash) ? args.pop : nil
  options = options.nil? ? default_values.dup : default_values.merge(options) if default_values
  options.delete_if{ |k,v| v.nil? } if options
  path = if args.empty?
    matching_path(options)
  else
    matching_path(args, options)
  end
  raise UngeneratableRouteException unless path
  result, params = path.url(args, options)
  mount_point = router.url_mount && router.url_mount.url(options)
  mount_point ? [File.join(mount_point, result), params] : [result, params]
end

#user_agent(user_agent) ⇒ Object



73
74
75
# File 'lib/http_router/route.rb', line 73

def user_agent(user_agent)
  ((@conditions ||= {})[:user_agent] ||= []) << user_agent; self
end