Extracting the URL into several parts, in RoR

  From the following example we can notice that the values of headers are coming in a HASH and from there it can be extracted easily.
Headers (request.headers) :  
{“HTTP_HOST”=>”localhost:3000”, “HTTP_ACCEPT”=>”text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”, “SERVER_NAME”=>”localhost”, “rack.session”=>{:session_id=>”7aad641cdd3c586d8112302977101ce0”}, “rack.url_scheme”=>”http”, “REQUEST_PATH”=>”/user/login”, “HTTP_USER_AGENT”=>”Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.8) Gecko/20100723 Ubuntu/10.04 (lucid) Firefox/3.6.8”, “HTTP_KEEP_ALIVE”=>”115”, “rack.errors”=>#<IO:0xb76e6560>, “HTTP_ACCEPT_LANGUAGE”=>”en-us,en;q=0.5”, “SERVER_PROTOCOL”=>”HTTP/1.1”, “action_controller.request.request_parameters”=>{}, “rack.version”=>[1, 0], “rack.run_once”=>false, “SERVER_SOFTWARE”=>”Mongrel 1.1.5”, “PATH_INFO”=>”/user/login”, “REMOTE_ADDR”=>”127.0.0.1”, “SCRIPT_NAME”=>””, “rack.multithread”=>false, “HTTP_VERSION”=>”HTTP/1.1”, “rack.multiprocess”=>false, “REQUEST_URI”=>”/user/login?client_id=70104c672f9bf60561a7f16ade1216a3&scope=DC,TL&id=dd&redirect_url=http://google.com“, “action_controller.request.path_parameters”=>{“action”=>”login_manage”, “controller”=>”manage_profiles”}, “HTTP_ACCEPT_CHARSET”=>”ISO-8859-1,utf-8;q=0.7,*;q=0.7”, “SERVER_PORT”=>”3000”, “rack.request.query_hash”=>{“scope”=>”DC,TL”, “redirect_url”=>”dfdf”, “id”=>”123456”, “client_id”=>”70104c672f9bf60561a7f16ade1216a3”}…………}
From the above we can extract the values separately as below :-
Protocol (request.headers[‘rack.url_scheme’]) –> http
Host Name (request.headers[‘SERVER_NAME’]) –> localhost
Host Port (request.headers[‘SERVER_PORT’]) –> 3000
URL Path (request.headers[‘REQUEST_PATH’]) –> /user/login
Parameters :
Scope (request.headers[‘rack.request.query_hash’][‘scope’]) –> DC,TL
Redirect URL (request.headers[‘rack.request.query_hash’][‘redirect_url’]) –> http://google.com
ID (request.headers[‘rack.request.query_hash’][‘id’]) –> 123456
Client ID (request.headers[‘rack.request.query_hash’][‘client_id’]) –> 70104c672f9bf60561a7f16ade1216a3

Code Example :-

p “Protocol : #{request.headers[‘rack.url_scheme’]}”
p “Host Name: #{request.headers[‘SERVER_NAME’]}”
p “Host Port: #{request.headers[‘SERVER_PORT’]}”
p “Path : #{request.headers[‘REQUEST_PATH’]}”
p “Scope : #{request.headers[‘rack.request.query_hash’][‘scope’]}”
p “Redirect URL : #{request.headers[‘rack.request.query_hash’][‘redirect_url’]}”
p “ID : #{request.headers[‘rack.request.query_hash’][‘id’]}”
p “Client ID : #{request.headers[‘rack.request.query_hash’][‘client_id’]}”

Result :-

“Protocol : http”
“Host Name: localhost”
“Host Port: 3000”
“Path : /user/login”
“Scope : DC,TL”
“Redirect URL : http://google.com
“ID : 123456”
“Client ID : 70104c672f9bf60561a7f16ade1216a3”
Advertisements