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
# File 'lib/http_router/route.rb', line 7

def initialize(router, path, opts = {})
  @router, @original_path, @opts = router, path, opts
  if @original_path
    @match_partially = true and path.slice!(-1) if @original_path[/[^\\]\*$/]
    @original_path[0, 0] = '/'                  if @original_path[0] != ?/
  else
    @match_partially = true
  end
  process_opts
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



3
4
5
# File 'lib/http_router/route.rb', line 3

def conditions
  @conditions
end

#default_valuesObject (readonly)

Returns the value of attribute default_values.



3
4
5
# File 'lib/http_router/route.rb', line 3

def default_values
  @default_values
end

#destObject (readonly)

Returns the value of attribute dest.



3
4
5
# File 'lib/http_router/route.rb', line 3

def dest
  @dest
end

#match_partiallyObject (readonly) Also known as: match_partially?

Returns the value of attribute match_partially.



3
4
5
# File 'lib/http_router/route.rb', line 3

def match_partially
  @match_partially
end

#matches_withObject (readonly)

Returns the value of attribute matches_with.



3
4
5
# File 'lib/http_router/route.rb', line 3

def matches_with
  @matches_with
end

#namedObject (readonly)

Returns the value of attribute named.



3
4
5
# File 'lib/http_router/route.rb', line 3

def named
  @named
end

#original_pathObject (readonly)

Returns the value of attribute original_path.



3
4
5
# File 'lib/http_router/route.rb', line 3

def original_path
  @original_path
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/http_router/route.rb', line 3

def path
  @path
end

#regexObject (readonly) Also known as: regex?

Returns the value of attribute regex.



3
4
5
# File 'lib/http_router/route.rb', line 3

def regex
  @regex
end

#routerObject (readonly)

Returns the value of attribute router.



3
4
5
# File 'lib/http_router/route.rb', line 3

def router
  @router
end

Instance Method Details

#add_to_contitions(name, *vals) ⇒ Object



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

def add_to_contitions(name, *vals)
  ((@conditions ||= {})[name] ||= []).concat(vals.flatten)
  self
end

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



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

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

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



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

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

#as_optionsObject



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

def as_options
  {:__matching__ => @matches_with, :__conditions__ => @conditions, :__default_values__ => @default_values, :__name__ => @named, :__partial__ => @partially_match, :__arbitrary__ => @arbitrary}
end

#clone(new_router) ⇒ Object



137
138
139
# File 'lib/http_router/route.rb', line 137

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

#compiled?Boolean

Returns:

  • (Boolean)


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

def compiled?
  !@paths.nil?
end

#conenctObject



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

def conenct; request_method('CONNECT');         end

#default(defaults) ⇒ Object



84
85
86
87
# File 'lib/http_router/route.rb', line 84

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

#deleteObject



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

def delete;  request_method('DELETE');          end

#getObject



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

def get;     request_method('GET');             end

#headObject



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

def head;    request_method('HEAD');            end

#host(*host) ⇒ Object



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

def host(*host)
  add_to_contitions(:host, host)
end

#matching(matchers) ⇒ Object



79
80
81
82
# File 'lib/http_router/route.rb', line 79

def matching(matchers)
  @matches_with.merge!(matchers.is_a?(Array) ? Hash[*matchers] : matchers)
  self
end

#matching_path(params, other_hash = nil) ⇒ Object



162
163
164
165
166
167
168
169
170
171
# File 'lib/http_router/route.rb', line 162

def matching_path(params, other_hash = nil)
  return @paths.first if @paths.size == 1
  case params
  when Array
    significant_keys = other_hash && significant_variable_names & other_hash.keys
    @paths.find { |path| path.param_names.size == (significant_keys ? params.size + significant_keys.size : params.size) }
  when Hash
    @paths.find { |path| (params && !params.empty? && (path.param_names & params.keys).size == path.param_names.size) || path.param_names.empty? }
  end
end

#name(n) ⇒ Object



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

def name(n)
  @named = n
  @router.named_routes[n] << self
  @router.named_routes[n].sort!{|r1, r2| r2.significant_variable_names.size <=> r1.significant_variable_names.size }
  self
end

#optionsObject



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

def options; request_method('OPTIONS');         end

#partial(match_partially = true) ⇒ Object



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

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

#patchObject



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

def patch;   request_method('PATCH');           end

#postObject



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

def post;    request_method('POST');            end

#process_optsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/http_router/route.rb', line 18

def process_opts
  @default_values = @opts[:__default_values__] || @opts[:default_values] || {}
  @arbitrary = @opts[:__arbitrary__] || @opts[:arbitrary]
  @matches_with = significant_variable_names.include?(:matching) ? @opts : @opts[:__matching__] || @opts[:matching] || {}
  significant_variable_names.each do |name|
    @matches_with[name] = @opts[name] if @opts.key?(name) && !@matches_with.key?(name)
  end
  @conditions = @opts[:__conditions__] || @opts[:conditions] || {}
  @match_partially = @opts[:__partial__] if @match_partially.nil? && !@opts[:__partial__].nil?
  @match_partially = @opts[:partial] if @match_partially.nil? && !@opts[:partial].nil?
  name(@opts[:__name__] || @opts[:name]) if @opts.key?(:__name__) || @opts.key?(:name)
  @needed_keys = significant_variable_names - @default_values.keys
end

#putObject



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

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)


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

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

#request_method(*method) ⇒ Object



58
59
60
# File 'lib/http_router/route.rb', line 58

def request_method(*method)
  add_to_contitions(:request_method, method)
end

#scheme(*scheme) ⇒ Object



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

def scheme(*scheme)
  add_to_contitions(:scheme, scheme)
end

#significant_variable_namesObject



158
159
160
# File 'lib/http_router/route.rb', line 158

def significant_variable_names
  @significant_variable_names ||= @original_path.nil? ? [] : @original_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.



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

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)
  @dest = dest || dest2
  add_path_to_tree
  self
end

#to_sObject



173
174
175
# File 'lib/http_router/route.rb', line 173

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

#traceObject



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

def trace;   request_method('TRACE');           end

#url(*args) ⇒ Object



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

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

#url_args_processing(args) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/http_router/route.rb', line 149

def url_args_processing(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
  result, params = yield args, options
  mount_point = router.url_mount && router.url_mount.url(options)
  mount_point ? [File.join(mount_point, result), params] : [result, params]
end

#url_with_params(*a) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/http_router/route.rb', line 141

def url_with_params(*a)
  url_args_processing(a) do |args, options|
    path = args.empty? ? matching_path(options) : matching_path(args, options)
    raise InvalidRouteException unless path
    path.url(args, options)
  end
end

#user_agent(*user_agent) ⇒ Object



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

def user_agent(*user_agent)
  add_to_contitions(:user_agent, user_agent)
end