lib/roda.rb in roda-2.27.0 vs lib/roda.rb in roda-2.28.0

- old
+ new

@@ -87,10 +87,18 @@ # Roda::RodaPlugins.register_plugin(:plugin_name, PluginModule) def self.register_plugin(name, mod) @plugins[name] = mod end + # Deprecate the constant with the given name in the given module, + # if the ruby version supports it. + def self.deprecate_constant(mod, name) + if RUBY_VERSION >= '2.3' + mod.deprecate_constant(name) + end + end + # The base plugin for Roda, implementing all default functionality. # Methods are put into a plugin so future plugins can easily override # them and call super to get the default behavior. module Base # Class methods for the Roda class. @@ -338,23 +346,33 @@ # Instance methods for RodaRequest, mostly related to handling routing # for the request. module RequestMethods PATH_INFO = "PATH_INFO".freeze + RodaPlugins.deprecate_constant(self, :PATH_INFO) SCRIPT_NAME = "SCRIPT_NAME".freeze + RodaPlugins.deprecate_constant(self, :SCRIPT_NAME) REQUEST_METHOD = "REQUEST_METHOD".freeze + RodaPlugins.deprecate_constant(self, :REQUEST_METHOD) EMPTY_STRING = "".freeze + RodaPlugins.deprecate_constant(self, :EMPTY_STRING) SLASH = "/".freeze + RodaPlugins.deprecate_constant(self, :SLASH) COLON = ":".freeze + RodaPlugins.deprecate_constant(self, :COLON) SEGMENT = "([^\\/]+)".freeze + RodaPlugins.deprecate_constant(self, :SEGMENT) TERM_INSPECT = "TERM".freeze + RodaPlugins.deprecate_constant(self, :TERM_INSPECT) GET_REQUEST_METHOD = 'GET'.freeze + RodaPlugins.deprecate_constant(self, :GET_REQUEST_METHOD) SESSION_KEY = 'rack.session'.freeze + RodaPlugins.deprecate_constant(self, :SESSION_KEY) TERM = Object.new def TERM.inspect - TERM_INSPECT + "TERM" end TERM.freeze # The current captures for the request. This gets modified as routing # occurs. @@ -406,11 +424,11 @@ # request method and full path. # # r.inspect # # => '#<Roda::RodaRequest GET /foo/bar>' def inspect - "#<#{self.class.inspect} #{@env[REQUEST_METHOD]} #{path}>" + "#<#{self.class.inspect} #{@env["REQUEST_METHOD"]} #{path}>" end # Does a terminal match on the current path, matching only if the arguments # have fully matched the path. If it matches, the match block is # executed, and when the match block returns, the rack response is @@ -467,11 +485,11 @@ # Optimized method for whether this request is a +GET+ request. # Similar to the default Rack::Request get? method, but can be # overridden without changing rack's behavior. def is_get? - @env[REQUEST_METHOD] == GET_REQUEST_METHOD + @env["REQUEST_METHOD"] == 'GET' end # Does a match on the path, matching only if the arguments # have matched the path. Because this doesn't fully match the # path, this is usually used to setup branches of the routing tree, @@ -508,22 +526,22 @@ end # The already matched part of the path, including the original SCRIPT_NAME. def matched_path e = @env - e[SCRIPT_NAME] + e[PATH_INFO].chomp(@remaining_path) + e["SCRIPT_NAME"] + e["PATH_INFO"].chomp(@remaining_path) end # This an an optimized version of Rack::Request#path. # # r.env['SCRIPT_NAME'] = '/foo' # r.env['PATH_INFO'] = '/bar' # r.path # # => '/foo/bar' def path e = @env - "#{e[SCRIPT_NAME]}#{e[PATH_INFO]}" + "#{e["SCRIPT_NAME"]}#{e["PATH_INFO"]}" end # The current path to match requests against. attr_reader :remaining_path @@ -629,11 +647,11 @@ # end # # Use <tt>r.get true</tt> to handle +GET+ requests where the current # path is empty. def root(&block) - if remaining_path == SLASH && is_get? + if remaining_path == "/" && is_get? always(&block) end end # Call the given rack app with the environment and return the response @@ -648,12 +666,12 @@ # before dispatching to another rack app, so the app still works as # a URL mapper. def run(app) e = @env path = real_remaining_path - sn = SCRIPT_NAME - pi = PATH_INFO + sn = "SCRIPT_NAME" + pi = "PATH_INFO" script_name = e[sn] path_info = e[pi] begin e[sn] += path_info.chomp(path) e[pi] = path @@ -665,11 +683,11 @@ end # The session for the current request. Raises a RodaError if # a session handler has not been loaded. def session - @env[SESSION_KEY] || raise(RodaError, "You're missing a session handler. You can get started by adding use Rack::Session::Cookie") + @env['rack.session'] || raise(RodaError, "You're missing a session handler. You can get started by adding use Rack::Session::Cookie") end private # Match any of the elements in the given array. Return at the @@ -719,25 +737,25 @@ # Match the given string to the request path. Regexp escapes the # string so that regexp metacharacters are not matched, and recognizes # colon tokens for placeholders. def _match_string(str) - if str.index(COLON) && placeholder_string_matcher? + if str.index(":") && placeholder_string_matcher? consume(self.class.cached_matcher(str){Regexp.escape(str).gsub(/:(\w+)/){|m| _match_symbol_regexp($1)}}) else rp = @remaining_path if rp.start_with?("/#{str}") last = str.length + 1 case rp[last] - when SLASH + when "/" @remaining_path = rp[last, rp.length] when nil - @remaining_path = EMPTY_STRING + @remaining_path = "" when Integer # :nocov: # Ruby 1.8 support - if rp[last].chr == SLASH + if rp[last].chr == "/" @remaining_path = rp[last, rp.length] end # :nocov: end end @@ -745,35 +763,35 @@ end # Match the given symbol if any segment matches. def _match_symbol(sym=nil) rp = @remaining_path - if rp[0, 1] == SLASH + if rp[0, 1] == "/" if last = rp.index('/', 1) if last > 1 @captures << rp[1, last-1] @remaining_path = rp[last, rp.length] end elsif rp.length > 1 @captures << rp[1,rp.length] - @remaining_path = EMPTY_STRING + @remaining_path = "" end end end # Match any nonempty segment. This should be called without an argument. alias _match_class_String _match_symbol # The regular expression to use for matching symbols. By default, any non-empty # segment matches. def _match_symbol_regexp(s) - SEGMENT + "([^\\/]+)" end # The base remaining path to use. def _remaining_path(env) - env[PATH_INFO] + env["PATH_INFO"] end # Backbone of the verb method support, using a terminal match if # args is not empty, or a regular match if it is empty. def _verb(args, &block) @@ -839,11 +857,11 @@ 302 end # Whether the current path is considered empty. def empty_path? - remaining_path == EMPTY_STRING + remaining_path == "" end # If all of the arguments match, yields to the match block and # returns the rack response when the block returns. If any of # the match arguments doesn't match, does nothing. @@ -898,11 +916,11 @@ # to match on multiple methods. def match_method(type) if type.is_a?(Array) type.any?{|t| match_method(t)} else - type.to_s.upcase == @env[REQUEST_METHOD] + type.to_s.upcase == @env["REQUEST_METHOD"] end end # Whether string matchers are used verbatim, without handling # placeholders via colons. @@ -932,14 +950,18 @@ end end # Instance methods for RodaResponse module ResponseMethods + DEFAULT_HEADERS = {"Content-Type" => "text/html".freeze}.freeze + CONTENT_LENGTH = "Content-Length".freeze + RodaPlugins.deprecate_constant(self, :CONTENT_LENGTH) CONTENT_TYPE = "Content-Type".freeze - DEFAULT_HEADERS = {"Content-Type" => "text/html".freeze}.freeze + RodaPlugins.deprecate_constant(self, :CONTENT_TYPE) LOCATION = "Location".freeze + RodaPlugins.deprecate_constant(self, :LOCATION) # The body for the current response. attr_reader :body # The hash of response headers for the current response. @@ -1005,13 +1027,13 @@ s = (@status ||= empty ? 404 : default_status) set_default_headers h = @headers if empty && (s == 304 || s == 204 || s == 205 || (s >= 100 && s <= 199)) - h.delete(CONTENT_TYPE) + h.delete("Content-Type") else - h[CONTENT_LENGTH] ||= @length.to_s + h["Content-Length"] ||= @length.to_s end [s, h, b] end @@ -1040,10 +1062,10 @@ # to the given status. Example: # # response.redirect('foo', 301) # response.redirect('bar') def redirect(path, status = 302) - @headers[LOCATION] = path + @headers["Location"] = path @status = status nil end # Return the Roda class related to this response.