Public Instance methods
relative_path(absolute_path)
Return a relative path for the absolute path based on the current path of the request by adding the appropriate prefix.
[show source]
# File lib/roda/plugins/relative_path.rb 37 def relative_path(absolute_path) 38 relative_prefix + absolute_path 39 end
relative_prefix()
Return a relative prefix to append to an absolute path to a relative path based on the current path of the request.
[show source]
# File lib/roda/plugins/relative_path.rb 43 def relative_prefix 44 return @_relative_prefix if @_relative_prefix 45 env = @_request.env 46 script_name = env["SCRIPT_NAME"] 47 path_info = env["PATH_INFO"] 48 49 # Check path begins with slash. All valid paths should, but in case this 50 # request is bad, just skip using a relative prefix. 51 case script_name.getbyte(0) 52 when nil # SCRIPT_NAME empty 53 unless path_info.getbyte(0) == 47 # PATH_INFO starts with / 54 return(@_relative_prefix = '') 55 end 56 when 47 # SCRIPT_NAME starts with / 57 # nothing 58 else 59 return(@_relative_prefix = '') 60 end 61 62 slash_count = script_name.count('/') + path_info.count('/') 63 @_relative_prefix = if slash_count > 1 64 ("../" * (slash_count - 2)) << ".." 65 else 66 "." 67 end 68 end