How to get ip address, referer, and user agent in ruby? - 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?

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Ă .

You need the array request.env
request.env['REMOTE_ADDR']:

I'm assuming you by ruby you mean ruby on rails, the following link shows you how to access them:
http://techoctave.com/c7/posts/25-rails-request-environment-variables

Related

How Can I use nginx X-Accel-Redirect in a ruby cgi script

I'm using nginx to run a ruby script. I've already set up the nginx config correctly as it works with php-fpm
In php I set
header('X-Accel-Redirect, file_path)
Is there an equivalent for it in ruby.
I have tried it with
cgi = CGI.new
cgi.out( "X-Accel-Redirect" => new_file)
But it doesn't seem to work...
Is any any gem that I could include.
I'm new to ruby. any help would be highly appreciated.
Assuming you are using a Rack application (Rails, Sinatra, Padrino etc.) the same way you used your php-fpm apps (i.e. behind Nginx as a reverse proxy), you should be able to do the same thing you did in PHP:
response['X-Accel-Redirect'] = file_path

How to specify memcache server to Rack::Session::Memcache?

I'm trying to configure my Rack app to use Memcache for sessions with Rack::Session::Memcache
How do I give it the options (such as server, username and password)?
Presently I have
use Rack::Session::Memcache
But I get the error
in `initialize': No memcache servers (RuntimeError)
Heroku has put the config in environment variables
MEMCACHE_PASSWORD:
MEMCACHE_SERVERS:
MEMCACHE_USERNAME:
I know I can get these in Ruby with ENV['MEMCACHE_PASSWORD'] but I don't know how to give them to Rack::Session::Memcache
Edit: or to Rack::Session::Dalli that would be great too https://github.com/mperham/dalli
This config worked for Heroku, Dalli is clever and knows to look in the environment variables
require 'dalli'
require 'rack/session/dalli'
use Rack::Session::Dalli, :cache => Dalli::Client.new
After reading the source code at https://github.com/mperham/dalli/commit/4ac5a99

Sinatra - Accessing request in Rack::ResponseHeaders

I want to access request in the Rack::ResponseHeaders. I am using Sinatra in my app.
Below is my code:
use Rack::ResponseHeaders do |headers|
# Manipulation of request variables.
# Setting request headers.
end
The question is that in order to manipulate variables in request, I need to have the request variable first.
Please suggest.
First thing is, you need to install the gem rack-contrib via rubygems:
$ gem install rack-contrib
This gem contains contributed rack utilities. Then you need to require this gem in your app:
require 'rack/contrib'
It may be enough to only require the response headers utility (not tested):
require 'rack/contrib/response_headers'
Then you can use this utility to tap into the headers, for example:
use Rack::ResponseHeaders do |headers| # tap into headers
unless headers['cache-control'] # if header not set,
headers['cache-control'] = "public, max-age=1800" # set it to ...
end
end
Let me know whether this is working for you.

adding 'curl' command to sinatra and rails?

(GAVE UP ON INSTALLING CURB. POSTED NEW QUESTION PER SUGGESTION OF ONE OF THE RESPONDENTS)
I thought 'curl ' was 'built-in' but got an undefined method error in a sinatra app. is there a gem i need to add?
Same question for rails 3?
The application is that I have to simply 'hit' an external url (http://kickstartme.someplace.com?action=ACTIONNAME&token=XYZXYZXYZ) to kickstart a remote process.
the external url returns XML describing success/failure in the format:
<session>
<success>true</success>
<token>xyzxyzxyz</token>
<id>abcabcabc</id>
</session>
So really, ALL I need is for my rails and sinatra apps to hit that url and parse whatever is returned AND grcefully handle the remote server failing to reply.
require 'open-uri'
require 'nokogiri'
response = open("http://kickstartme.someplace.com?action=ACTIONNAME&token=XYZXYZXYZ").read
doc = Nokogiri::XML(response)
Use curb, a Ruby binding to libcurl. You will get all the curl features without having to shell out with system.
curl -b "auth=abcdef; ASP.NET_SessionId=lotsatext;" example.com
turns into
curl = Curl::Easy.new('http://example.com/')
curl.cookies = 'auth=abcdef; ASP.NET_SessionId=big-wall-of-text;'
curl.perform
More curb examples

Trying to Workout how-to run a Ruby (Sinatra) app on the Ebb webserver

I need to write a super fast Ruby application to process web requests on Sinatra - and want to run it on the Ebb webserver. But I cannot work out how to do this. Could someone please help me?
sinatra has a -s option to specify a handler. try running your app with -s ebb.
You need to look at Rack: http://rack.rubyforge.org/
It's pretty easy really, you have a .ru file which instructs Rack how to start your application, and in your application you have a 'call' method which is called on each request, and sends the response back to Rack.
In my_app.ru
require 'my_app'
require 'ebb'
# Rack config
use Rack::Static, urls: ['/js', '/public', '/index.html']
use Rack::ShowExceptions
# Run application
run MyApp.new
In my_app.rb
class MyApp
def call env
request = Rack::Request.new env
response = Rack::Response.new
params = request.params
response.body = "Hello World"
response['Content-Length'] = response.body.size.to_s
response.finish
end
end
Then you specify the .ru file in your sinatra config, like:
rackup: my_app.ru

Resources