Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ip address, referer, and user agent in ruby?

I want to log user's ip address, referer, and user agent.
In PHP, I can get them from the following variables:

$_SERVER['REMOTE_ADDR']
$_SERVER['HTTP_REFERER']
$_SERVER['HTTP_USER_AGENT']

How to get them in ruby?

like image 529
js_ Avatar asked Jul 15 '11 00:07

js_


1 Answers

PHP is embedded in a web server. Ruby is a general-purpose language: if you need a web server context, you'll have to install it yourself. Fortunately, it's easy.

One of the easiest ways to get started is with Sinatra. Install the gem:

gem install sinatra

Then create myapp.rb:

require 'sinatra'

get '/' do
    request.user_agent
end

Start up the web server:

ruby -rubygems myapp.rb

Visit Sinatra's default URL: http://localhost:4567/

Et voilà.

like image 114
Mark Thomas Avatar answered Nov 04 '22 16:11

Mark Thomas