lib/proxes/request.rb in proxes-0.5.2 vs lib/proxes/request.rb in proxes-0.6.0

- old
+ new

@@ -2,33 +2,41 @@ require 'rack' module ProxES class Request < Rack::Request + ID_ENDPOINTS = %w[_create _explain _mlt _percolate _source _termvector _update] + def self.from_env(env) - request = Rack::Request.new(env) - splits = request.path.split('/') - endpoint = if splits[1] && splits[1][0] == '_' - splits[1][1..-1].titlecase - else - splits.count > 0 ? splits[-1][1..-1].titlecase : 'Root' - end + endpoint = path_endpoint(env['REQUEST_PATH']) + return new(env) if endpoint.nil? + endpoint_class = endpoint[1..-1] begin - require 'proxes/request/' + endpoint.downcase - Request.const_get(endpoint).new(env) + require 'proxes/request/' + endpoint_class.downcase + Request.const_get(endpoint_class.titlecase).new(env) rescue LoadError new(env) end end + def self.path_endpoint(path) + return '_root' if ['', nil, '/'].include? path + path_parts = path[1..-1].split('/') + return path_parts[-1] if ID_ENDPOINTS.include? path_parts[-1] + return path_parts[-2] if path_parts[-1] == 'count' && path_parts[-2] == '_percolate' + return path_parts[-2] if path_parts[-1] == 'scroll' && path_parts[-2] == '_search' + path_parts.find { |part| part[0] == '_' } + end + def initialize(env) super parse end def endpoint path_parts[0] end + def parse path_parts end