adding 'curl' command to sinatra and rails? - ruby

(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

Related

How can I redirect error messages to the browser in CGI/Ruby?

I've been using CGI/Perl for a while and have got used to using Carp to redirect error messages to the browser with something like:
use CGI::Carp qw(warningsToBrowser fatalsToBrowser set_message);
set_message("Please report this error to the administrator");
...
warningsToBrowser(1);
I'm considering making the switch to using Ruby instead of Perl, but I can't find a way to do a similar error redirection. Is there a Ruby module which can do this?
group :development do
gem 'better_errors'
gem 'binding_of_caller'
gem 'meta_request'
end
See this railscast: Better Errors & RailsPanel and this thread redirect errors to browser in ruby + cgi, where it suggests this:
$stdout.sync = true
$stderr.reopen $stdout
puts "Content-type: text/html\n\n"`
The content-type needs to be early in the code, as any errors before it will not be sent to the browser.

How can I get Sinatra to set the content-length based on a static file's size?

I'm working on a Sinatra app and just started adding cacheing. Some of my files are cached correctly, but I keep seeing this warning when serving an image in the public folder:
WARN: Could not determine content-length of response body. Set
content-length of the response or set Response#chunked = true.
I don't understand why I'm getting this warning. Sinatra is serving the file correctly out of the public folder, and it says it defaults this header to the file size.
I'm using the following example settings from the README:
set :static_cache_control => [:public, :max_age => 60]
before do
cache_control :public, :must_revalidate, :max_age => 60
end
How can I get Sinatra to correctly set the content-length header to the static file's size?
Another workaround that removes the offending line from webrick. It's just not that useful:
cd `which ruby`/../../lib/ruby/1.9.1/webrick/ && sed -i '.bak' -e'/logger.warn/d' httpresponse.rb
(you may need to sudo)
Evidently, this message is safe to ignore.
See What does "WARN Could not determine content-length of response body." mean and how to I get rid of it?, or if you prefer to get the reply from the source, see the question posed at https://twitter.com/#!/luislavena/status/108998968859566080 and the answer at https://twitter.com/#!/tenderlove/status/108999110136303617.
A workaround: use thin
If the messages bother you, you can work around it by using thin. Add gem 'thin' to your Gemfile, then start your server using thin:
% bundle install
% rails server thin

Issues with HTTParty.post and Ruby 1.9.2

I used to have the following call working just fine on a Rails app running Ruby 1.8.7:
HTTParty.post("my uri", :body => "some body", :headers => { "Content-type" => "text/xml"})
When I run the same line on Ruby 1.9.2 I'm getting a MultiXml::ParseError with this message:
"xmlns: URI xyz is not absolute"
The call to my uri works just fine when I use curl, and I get back the expected response, which looks something like this:
<client login="foo" numsessions="1" xmlns="xyz"/>
Any Insight?
After much struggle, I gave up on HTTParty for this. I tried Patron, which worked local, but didn't on Heroku, and I finally settled on RestClient, which worked great. https://github.com/archiloque/rest-client
That's because curl doesn't try to parse the xmlns. You could either try making sure you use the same version of httparty with 1.9.2 as you use with 1.8.7 or asking the people in charge of that uri to make the xmlns valid

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?
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

SOAP - Need to understand the error I get

I'm trying to initialize a variable called proxy like this:
proxy = Savon::Client.new "http://192.168.1.1:8080"
The thing is, when I run my code, I only get the error:
NameError: uninitialized constant NameOfTheClass::Savon
Thanks for any help!
PD: I'm using Ruby 1.9.2
PD2: I'm trying to run this from console.
You found probably the documentation for versions < 0.8.x.
Unfortunately the syntax has changed! Have a look here: https://github.com/rubiii/savon/blob/master/README.md
Savon works with blocks now.
Your example should now look like this
require 'savon'
require 'pp'
proxy = Savon::Client.new do
wsdl.document = "http://my.webservices.net/service?wsdl"
end
pp proxy.wsdl.soap_actions

Resources