lib/tomogram_routing/tomogram.rb in tomogram_routing-0.1.4 vs lib/tomogram_routing/tomogram.rb in tomogram_routing-0.1.5
- old
+ new
@@ -15,20 +15,41 @@
end
end
private
+ def initialize(array)
+ super
+ compile_path_patterns
+ end
+
+ def compile_path_patterns
+ each do |action|
+ next unless (path = action['path'])
+
+ regexp = compile_path_pattern(path)
+ action['path_regexp'] = regexp
+ end
+ end
+
+ def compile_path_pattern(path)
+ str = Regexp.escape(path)
+ str = str.gsub(/\\{\w+\\}/, '[^&=\/]+')
+ str = "\\A#{ str }\\z"
+ Regexp.new(str)
+ end
+
def find_request_path(method:, path:)
return '' unless path && path.size > 0
path = normalize_path(path)
action = search_for_an_exact_match(method, path, self)
return action['path'] if action
action = search_with_parameter(method, path, self)
- return action['path'] if action && action.first
+ return action['path'] if action
''
end
def normalize_path(path)
@@ -51,28 +72,19 @@
end
end
def search_with_parameter(method, path, documentation)
documentation = actions_with_same_method(documentation, method)
- sought_for_path = path.split('/')
- documentation.map do |action|
- current_path = action['path'].split('/')
- next unless sought_for_path.size == current_path.size
- next unless match(sought_for_path, current_path)
- return action
+ documentation.find do |action|
+ next unless regexp = action['path_regexp']
+ regexp =~ path
end
end
def actions_with_same_method(documentation, method)
documentation.find_all do |doc|
doc['method'] == method
- end
- end
-
- def match(sought_for_path, current_path)
- sought_for_path.zip(current_path).all? do |p|
- p[0] == p[1] || (p[1][0] == '{' && p[1][-1] == '}')
end
end
end
end