module Roda::RodaPlugins::RelativePath::InstanceMethods

  1. lib/roda/plugins/relative_path.rb

Methods

Public Instance

  1. relative_path
  2. relative_prefix

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
39 def relative_path(absolute_path)
40   relative_prefix + absolute_path
41 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
45 def relative_prefix
46   return @_relative_prefix if @_relative_prefix
47   env = @_request.env
48   script_name = env["SCRIPT_NAME"]
49   path_info = env["PATH_INFO"]
50 
51   # Check path begins with slash.  All valid paths should, but in case this
52   # request is bad, just skip using a relative prefix.
53   case script_name.getbyte(0)
54   when nil # SCRIPT_NAME empty
55     unless path_info.getbyte(0) == 47 # PATH_INFO starts with /
56       return(@_relative_prefix = '')
57     end
58   when 47 # SCRIPT_NAME starts with /
59     # nothing
60   else
61     return(@_relative_prefix = '')
62   end
63 
64   slash_count = script_name.count('/') + path_info.count('/')
65   @_relative_prefix = if slash_count > 1
66     ("../" * (slash_count - 2)) << ".."
67   else
68     "."
69   end
70 end