GeekedOut
GeekedOut

Reputation: 17185

How to visit a URL with Ruby via http and read the output?

So far I have been able to stitch this together :)

 begin
  open("http://www.somemain.com/" + path + "/" + blah)
 rescue OpenURI::HTTPError
   @failure += painting.permalink
 else
   @success += painting.permalink
 end

But how do I read the output of the service that I would be calling?

Upvotes: 6

Views: 2357

Answers (3)

David Tuite
David Tuite

Reputation: 22663

I'm not sure if you want to do this yourself for the hell of it or not but if you don't.. Mecanize is a really nice gem for doing this.

It will visit the page you want and automatically wrap the page with nokogiri so that you can access it's elements with css selectors such as "div#header h1". Ryan Bates has a video tutorial on it which will teach you everything you need to know to use it.

Basically you can just

require 'rubygems'
require 'mechanize'

agent = Mechanize.new
agent.get("http://www.google.com")
agent.page.at("some css selector").text

It's that simple.

Upvotes: 4

the Tin Man
the Tin Man

Reputation: 160631

Open-URI extends open, so you'll get a type of IO stream returned:

open('http://www.example.com') #=> #<StringIO:0x00000100977420>

You have to read that to get content:

open('http://www.example.com').read[0 .. 10] #=> "<!DOCTYPE h"

A lot of times a method will let you pass different types as a parameter. They check to see what it is and either use the contents directly, in the case of a string, or read the handle if it's a stream.

For HTML and XML, such as RSS feeds, we'll typically pass the handle to a parser and let it grab the content, parse it, and return an object suitable for searching further:

require 'nokogiri'
doc = Nokogiri::HTML(open('http://www.example.com'))
doc.class #=> Nokogiri::HTML::Document
doc.to_html[0 .. 10] #=> "<!DOCTYPE h"
doc.at('h1').text #=> "Example Domains"

Upvotes: 6

smathy
smathy

Reputation: 27971

doc = open("http://etc..")
content = doc.read

More often people want to be able to parse the returned document, for this use something like hpricot or nokogiri

Upvotes: 6

Related Questions