Class: IceJade::Cradle::Server::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/ice_jade/cradle/server.rb

Overview

=================================================================

Route 定义

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, path_pattern, handler) ⇒ Route

Returns a new instance of Route.



221
222
223
224
225
226
# File 'lib/ice_jade/cradle/server.rb', line 221

def initialize(method, path_pattern, handler)
  @method = method == 'ALL' ? nil : method
  @path_pattern = path_pattern
  @handler = handler
  @regex, @param_names = compile(path_pattern)
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



219
220
221
# File 'lib/ice_jade/cradle/server.rb', line 219

def handler
  @handler
end

#methodObject (readonly)

Returns the value of attribute method.



219
220
221
# File 'lib/ice_jade/cradle/server.rb', line 219

def method
  @method
end

#path_patternObject (readonly)

Returns the value of attribute path_pattern.



219
220
221
# File 'lib/ice_jade/cradle/server.rb', line 219

def path_pattern
  @path_pattern
end

Instance Method Details

#extract_params(path) ⇒ Object



234
235
236
237
238
239
240
241
242
# File 'lib/ice_jade/cradle/server.rb', line 234

def extract_params(path)
  path_without_query = path.split('?').first
  match = @regex.match(path_without_query)
  return {} unless match

  @param_names.each_with_object({}).with_index do |(name, hash), i|
    hash[name] = match[i + 1]
  end
end

#match?(http_method, path) ⇒ Boolean

Returns:

  • (Boolean)


228
229
230
231
232
# File 'lib/ice_jade/cradle/server.rb', line 228

def match?(http_method, path)
  return false if @method && @method != http_method
  path_without_query = path.split('?').first
  @regex.match?(path_without_query)
end