Module: Roda::RodaPlugins::RelativePath::InstanceMethods

Defined in:
lib/roda/plugins/relative_path.rb

Instance Method Summary collapse

Instance Method Details

#relative_path(absolute_path) ⇒ Object

Return a relative path for the absolute path based on the current path of the request by adding the appropriate prefix.



37
38
39
# File 'lib/roda/plugins/relative_path.rb', line 37

def relative_path(absolute_path)
  relative_prefix + absolute_path
end

#relative_prefixObject

Return a relative prefix to append to an absolute path to a relative path based on the current path of the request.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/roda/plugins/relative_path.rb', line 43

def relative_prefix
  return @_relative_prefix if @_relative_prefix
  env = @_request.env
  script_name = env["SCRIPT_NAME"]
  path_info = env["PATH_INFO"]

  # Check path begins with slash.  All valid paths should, but in case this
  # request is bad, just skip using a relative prefix.
  case script_name.getbyte(0)
  when nil # SCRIPT_NAME empty
    unless path_info.getbyte(0) == 47 # PATH_INFO starts with /
      return(@_relative_prefix = '')
    end
  when 47 # SCRIPT_NAME starts with /
    # nothing
  else
    return(@_relative_prefix = '')
  end

  slash_count = script_name.count('/') + path_info.count('/')
  @_relative_prefix = if slash_count > 1
    ("../" * (slash_count - 2)) << ".."
  else
    "."
  end
end