Forum: Ruby on Rails How to parse info from an xml response

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
648a71a35ce6e4a1c5cd63451a2a20df?d=identicon&s=25 Gavin Morrice (dr_gavin)
on 2008-11-28 00:44
(Received via mailing list)
Hi all!
In my app I want to detect what country a visitor is viewing from by
their IP.

Although it's by no means perfect, the simplest option I've found is
by using this site:
http://api.hostip.info/

by passing the ip address as a query, this site returns an xml file
with the country as one of the tags.

http://api.hostip.info/?ip=6.255.255.255 returns (amongst other
things)
"<countryAbbrev>US</countryAbbrev>"

Could somebody suggest the best way to assert the value of this
particular tag in the xml file?

Thanks

Gavin
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2008-11-28 02:06
(Received via mailing list)
>
> http://api.hostip.info/?ip=6.255.255.255 returns (amongst other
> things)
> "<countryAbbrev>US</countryAbbrev>"
>
> Could somebody suggest the best way to assert the value of this
> particular tag in the xml file?

hpricot or that new one... nokugirl (or something like that).  I know
hpricot can parse XML easily.
D50ec0e8921ce139b929a36c83193bd1?d=identicon&s=25 Song Ginger (Guest)
on 2008-11-28 10:35
(Received via mailing list)
require 'rubygems'
require 'hpricot'
require 'net/http'
html =
Net::HTTP.get(URI.parse("http://api.hostip.info/?ip=6.255.255.255"))
doc = Hpricot(html)
country_abbrev = doc.search("countryabbrev")
puts country_abbrev.inner_text()
648a71a35ce6e4a1c5cd63451a2a20df?d=identicon&s=25 Gavin Morrice (dr_gavin)
on 2008-11-28 14:10
(Received via mailing list)
Exactly what I was looking for!

Thanks for your help
:)
This topic is locked and can not be replied to.