Method: Sinatra::Helpers#uri

Defined in:
lib/sinatra/base.rb

#uri(addr = nil, absolute = true, add_script_name = true) ⇒ Object Also known as: url, to

Generates the absolute URI for a given path in the app. Takes Rack routers and reverse proxies into account.



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/sinatra/base.rb', line 325

def uri(addr = nil, absolute = true, add_script_name = true)
  return addr if addr.to_s =~ /\A[a-z][a-z0-9+.\-]*:/i

  uri = [host = String.new]
  if absolute
    host << "http#{'s' if request.secure?}://"
    host << if request.forwarded? || (request.port != (request.secure? ? 443 : 80))
              request.host_with_port
            else
              request.host
            end
  end
  uri << request.script_name.to_s if add_script_name
  uri << (addr || request.path_info).to_s
  File.join uri
end