[QUIZ] Current Temperature (#68)

R

Ruby Quiz

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

by Caleb Tennis

Write a Ruby program such that given a certain argument to the program it
will return the current temperature of that location. People living in
the United States may be interested in temperature by ZIP code:

$ ruby current_temp.rb 47201
The temperature in Columbus, Indiana is 32 degrees F.

Other locales may want to use their own mailing codes, or city names:

$ ruby current_temp.rb madrid
The temperature in Madrid, Spain is 12 degrees C.

Which arguments you support is up to you.
 
D

Dave Burt

Ruby said:
Write a Ruby program such that given a certain argument to the program it
will return the current temperature of that location.

I still haven't done last week's metakoans, but I thought to myself, "Dave,"
(because I always address myself by name in my thoughts). "Dave," I thought,
"a universal Ruby-based indicator of the current temperature that works in
any location on any Ruby platform would be a great boon not only to you, but
to the entire Ruby community. In fact, why stop at the temperature? Ruby has
the power to turn almost any device into a fully functional weather station,
measuring rain, wind and snow. The world will be amazed."

So, thinking of you all, I wrote the code I now humbly present.

Cheers,
Dave.

#!/usr/bin/ruby
#
# Current Weather
#
# A response to Ruby Quiz #68 [ruby-talk:181420]
#
# This script basically turns your Ruby device into a weather machine. It
# leverages the latest technology to enable most laptops, PDAs, etc. to
capture
# meterorological metrics.
#
# WARNING: this program has a bug resulting in an infinite loop on
non-portable
# platforms.
#
# Please ONLY EXECUTE THIS PROGRAM ON PORTABLE DEVICES.
#
# Author: Dave Burt <dave at burt.id.au>
#
# Created: 23 Oct 2005
#

require 'highline/import'

# Work around bug
agree("Are you using a portable Ruby device? ") or
abort("Sorry, this program has not yet been ported to your platform.")

# Calibrate instrumentation
begin
say "Go outside."
end until agree("Are you outside now? ")

# Ascertain cloud cover
if agree("Is your Ruby device casting a defined shadow? ")
say "It's sunny."
else
say "It's overcast."
end

# Capture rainfall
if agree("Are your Ruby device or your umbrella wet? ")
say "It's raining."
else
say "It's fine."
end

# Weigh other precipitation
if agree("Is your Ruby device becoming white? ")
say "It's snowing."
else
say "It's not snowing."
end

# Discern current temperature
if agree("Are your fingers getting cold? ")
say "It's cold."
else
say "It's warm."
end

# Measure wind speed
if agree("Do you feel lateral forces on your Ruby device? ")
say "It's windy."
else
say "It's calm."
end

say "This weather report has been brought to you by Ruby, the letter D,"
say "and the number 42."
 
N

Nola Stowe

hahaha, thats great. Very entertaining Dave. Sometimes I wish I was
that funny :)


Ruby said:
Write a Ruby program such that given a certain argument to the program = it
will return the current temperature of that location.

I still haven't done last week's metakoans, but I thought to myself, "Dav= e,"
(because I always address myself by name in my thoughts). "Dave," I thoug= ht,
"a universal Ruby-based indicator of the current temperature that works i= n
any location on any Ruby platform would be a great boon not only to you, = but
to the entire Ruby community. In fact, why stop at the temperature? Ruby = has
the power to turn almost any device into a fully functional weather stati= on,
measuring rain, wind and snow. The world will be amazed."

So, thinking of you all, I wrote the code I now humbly present.

Cheers,
Dave.

#!/usr/bin/ruby
#
# Current Weather
#
# A response to Ruby Quiz #68 [ruby-talk:181420]
#
# This script basically turns your Ruby device into a weather machine. It
# leverages the latest technology to enable most laptops, PDAs, etc. to
capture
# meterorological metrics.
#
# WARNING: this program has a bug resulting in an infinite loop on
non-portable
# platforms.
#
# Please ONLY EXECUTE THIS PROGRAM ON PORTABLE DEVICES.
#
# Author: Dave Burt <dave at burt.id.au>
#
# Created: 23 Oct 2005
#

require 'highline/import'

# Work around bug
agree("Are you using a portable Ruby device? ") or
abort("Sorry, this program has not yet been ported to your platform."= )

# Calibrate instrumentation
begin
say "Go outside."
end until agree("Are you outside now? ")

# Ascertain cloud cover
if agree("Is your Ruby device casting a defined shadow? ")
say "It's sunny."
else
say "It's overcast."
end

# Capture rainfall
if agree("Are your Ruby device or your umbrella wet? ")
say "It's raining."
else
say "It's fine."
end

# Weigh other precipitation
if agree("Is your Ruby device becoming white? ")
say "It's snowing."
else
say "It's not snowing."
end

# Discern current temperature
if agree("Are your fingers getting cold? ")
say "It's cold."
else
say "It's warm."
end

# Measure wind speed
if agree("Do you feel lateral forces on your Ruby device? ")
say "It's windy."
else
say "It's calm."
end

say "This weather report has been brought to you by Ruby, the letter D,"
say "and the number 42."
 
J

James Edward Gray II

# Calibrate instrumentation
begin
say "Go outside."
end until agree("Are you outside now? ")

This step could prove very difficult for some programmers. ;)

James Edward Gray II
 
A

Aditya Mahajan

by Caleb Tennis

Write a Ruby program such that given a certain argument to the program it
will return the current temperature of that location. People living in
the United States may be interested in temperature by ZIP code:

$ ruby current_temp.rb 47201
The temperature in Columbus, Indiana is 32 degrees F.

Other locales may want to use their own mailing codes, or city names:

$ ruby current_temp.rb madrid
The temperature in Madrid, Spain is 12 degrees C.

Which arguments you support is up to you.

This quiz finally got me going to read Ruby's Net API. It turned out
to be very pleasing. Here is my solution.

I use the wunderground website to get the weather. The program
supports all kinds of search arguments that are supported by
wundergroud. I pass on the input arguments to wu website. If a city is
found, the search results contain a link to rss feed. Instead of
parsing the html document, I get this rss feed and parse it. This
method fails sometimes, for small cities outside US.

Here is the code

# Examples:
# $ current_temp.rb 48105
# > The temperature in Ann Arbor, MI is 34 degrees F / 1 degrees C
#
# $ current_temp.rb Ann Arbor, MI
# > The temperature in Ann Arbor, MI is 34 degrees F / 1 degrees C
#
# $ current_temp DTW
# > The temperature in Detroit Metro Wayne County, MI is 36 degrees F / 2 degrees C
#
# $ current_temp New Delhi, India
# > The temperature in New Delhi, India is 77 degrees F / 25 degrees C
#
# $ current_temp DEL
# > The temperature in New Delhi, India is 77 degrees F / 25 degrees C
#

#--------------------------------------%<--------------------------------------
require 'net/http'
require 'uri'
require 'rexml/document'

if ARGV.length == 0
puts "Usage: ruby current_temp.rb [city, state | zipcode | city,
country | airport code]"
exit
end
urlbase =
"http://www.wunderground.com/cgi-bin/findweather/getForecast?query="
zipcode = ARGV.join('%20')

# Search for the zipcode on wunderground website
response = Net::HTTP.get_response URI.parse(urlbase << zipcode)

# Parse the result for the link to a rss feed
rss_feed = String.new
# Get the line with rss feed
response.body.each do |line|
if line.include?("application/rss+xml") then
stop_pos = line.rindex('"') - 1
start_pos = line.rindex('"',stop_pos) + 1
rss_feed = line.slice(start_pos..stop_pos)
break
end
end
# Get the feed and parse it for city and weather information
# The response is different for US cities and places outside US.
# Use appropritate regular expression to parse both simultaneously
if rss_feed == "" then
puts ARGV.join(' ') << ": No such city"
else
feed = Net::HTTP.get_response(URI.parse(rss_feed))
document = REXML::Document.new feed.body
title = document.elements.to_a("//title")[0].text
channel =
document.elements.to_a("//channel/item/description")[0].text
city = title.gsub(/\s*(Weather from)?\s*Weather
Underground\s*(-)?\s*/,"")
temp = channel.gsub(/(^Temperature:|\|.*$|\W)/,"")
temp = temp.gsub("F", " degrees F / ").gsub("C", " degrees C")
# For exact format as asked in the quiz, uncomment the following
# temp = temp.gsub("F.*$", "F")
puts "The temperature in #{city} is #{temp}"
end
#--------------------------------------%<--------------------------------------


Thanks for the nice quiz.

Aditya
 
R

Ryan Leavengood

Here is mine. It only provides temperatures for US zip codes. I've
been doing some HTML scraping like this lately for some utilities of
my own, so this was pretty easy (i.e. the techniques were fresh in my
mind.) Though for my other utilities I've been using WWW:Mechanize and
in this case I decided to go a little lower level.

One problem with this or any other HTML scraping solution, is minor
changes to the HTML can totally break things.

Beware of wrapping, especially on the "parse_list":

require 'open-uri'

if $0 =3D=3D __FILE__
if ARGV.length < 1
puts "Usage: #$0 <zip code>"
exit(1)
end
parse_list =3D [[/<B>Local Forecast for (.* \(\d{5}\))<\/B>/, 'Local
temperature for #$1: '],
[/<B CLASS=3DobsTempTextA>([^&]*)&deg;(.)<\/B>/, '#$1 degrees #$2 '],
[/<B CLASS=3DobsTextA>Feels Like<BR> ([^&]*)&deg;(.)<\/B>/, '[It
feels like #$1 degrees #$2]']
]
# Blessed be the internet, the great provider of information
open('http://beta.weather.com/weather/local/'+ARGV[0]) do |io|
html =3D io.read
parse_list.each do |p|
# We don't need no steenkin' HTML parser
if html =3D~ p[0]
print eval(%Q{"#{p[1]}"})
end
end
puts
end
end

Ryan
 
G

gordon

Write a Ruby program such that given a certain argument to the program it
will return the current temperature of that location. People living in
the United States may be interested in temperature by ZIP code:

This is my first submission to rubyquiz. I've been learning ruby for
about 6 months now, and it's my first foray into programming. I'd love
some feedback.

Thanks,

# current_temp.rb

require 'net/http'
require 'rexml/document'
require 'optparse'

class CurrentTemp
include REXML

def initialize(loc,u='f')
uri = "http://xml.weather.yahoo.com/forecastrss?p=#{loc}&u=#{u}"
@Doc = Document.new Net::HTTP.get(URI.parse(uri))
raise "Invalid city, #{loc}" if /error/i =~
@doc.elements["//description"].to_s
end

def method_missing(methodname)
XPath.match(@doc,"//*[starts-with(name(), 'yweather')]").each
do|elem|
return elem.attributes[methodname.to_s] if
elem.attributes[methodname.to_s]
end
Object.method_missing(methodname)
end

def unit
self.temperature
end

def state
self.region
end

def to_s
"The current temperature in #{self.city}, #{self.state} is
#{self.temp} degrees #{self.unit}."
end

end

opts = OptionParser.new
opts.banner = "Usage:\n\n current_temp.rb city [-u unit]\n\n "
opts.banner += "city should be a zip code, or a Yahoo Weather location
id.\n\n"
opts.on("-uARG", "--unit ARG","Should be f or c", String) {|val| @u =
val }
opts.on("-h", "--help") {puts opts.to_s ; exit 0}

loc = opts.parse!
@u ||='f'

begin

puts CurrentTemp.new(loc,@u)

rescue
puts $!
puts opts.to_s
exit 1
end
 
H

horndude77

My solution uses yahoo weather like another of the solutions here. It's
easy to use with US zip codes, but for international cities you have to
know the yahoo weather location id. I looked around for a big list of
these but couldn't find it. Anyways it would be nice to have some sort
of searching mechanism to turn a city name into a location id.

Also I used rexml to parse the rss feed. I tried to use the rss library,
but couldn't figure out how to pull out the 'yweather' tags.

Anyways, fun quiz. I enjoyed it a lot.

-----Jay Anderson


require 'rexml/document'
require 'open-uri'

#Returns a hash containing the location and temperature information
#Accepts US zip codes or Yahoo location id's
def yahoo_weather_query(loc_id, units)
h = {}
open("http://xml.weather.yahoo.com/forecastrss?p=#{loc_id}&u=#{units}")
do |http|
response = http.read
doc = REXML::Document.new(response)
root = doc.root
channel = root.elements['channel']
location = channel.elements['yweather:location']
h[:city] = location.attributes["city"]
h[:region] = location.attributes["region"]
h[:country] = location.attributes["country"]
h[:temp] =
channel.elements["item"].elements["yweather:condition"].attributes["temp"]
end
h
end

if ARGV.length < 1 then
puts "usage: #$0 <location> [f|c]"
exit
end
loc_id = ARGV[0]
units = (ARGV[1] || 'f').downcase
units = (units =~ /^(f|c)$/) ? units : 'f'

#An improvement would be to allow searches for the yahoo location id
#loc_id = yahoo_loc_search(loc_id)
weather_info = yahoo_weather_query(loc_id, units)
city = weather_info[:city]
region = weather_info[:region]
country = weather_info[:country]
temp = weather_info[:temp]

puts "The temperature in #{city}, #{region}, #{country} is #{temp}
degrees #{units.upcase}"
 
R

Ryan Leavengood

This is my first submission to rubyquiz. I've been learning ruby for
about 6 months now, and it's my first foray into programming. I'd love
some feedback.

I think this is pretty darn slick Gordon, and you should continue in
programming.

My only suggestion would be that you check for an empty argument list
explicitly so that someone will a slow internet connection does not
have to wait for the request to go to Yahoo to get an error.

Ryan
 
S

Stephen Waits

I've been learning ruby for
about 6 months now, and it's my first foray into programming. I'd
love
some feedback.

As Ryan said, excellent job! Keep at it.

--Steve
 
J

Jeff McNeil

This is also my first Ruby Quiz submission.

I was going to neglect this one as well, but I got to thinking how
nicely lazy.rb might work with some web services. Once someone
actually came across a working SOAP service (xmethods lists quite a
few that are iffy),
I decided to give it a go.

Thanks =)

Jeff


#!/usr/bin/env ruby

require 'lazy'
require 'soap/wsdlDriver'
require 'rexml/document'
$-w = nil

$wsdl_loc = "http://www.webservicex.net/globalweather.asmx?WSDL"
class WeatherState
def initialize(city, country)
stub = SOAP::WSDLDriverFactory.new($wsdl_loc).create_rpc_driver

@keep_me = promise do
conditions = stub.getWeather:)CityName
=>city, :CountryName=>country)
data = REXML::Document.new(conditions.getWeatherResult.gsub(/<
\?.*?>\n/, ''))
{ :temp => data.elements["//Temperature"].text, loc =>
data.elements["//Location"].text }
end
end

def temp
demand(@keep_me)[:temp]
end

def loc
demand(@keep_me)[:loc]
end
end

if ARGV.length != 2
abort("Usage: weather.rb city country")
end

# Create Weather Object
weatherProxy = WeatherState.new(ARGV[0], ARGV[1])
puts "Location: " + weatherProxy.loc
puts "Current Temp: " + weatherProxy.temp.strip
 
A

Adam Shelly

Write a Ruby program such that given a certain argument to the program it
will return the current temperature of that location. People living in
the United States may be interested in temperature by ZIP code:

I used Yahoo Weather's RSS feed. net/http and simple-rss did most of
the work for me.
But Yahoo keeps some interesting data in the attributes of some custom
tags, like <yweather:condition temp=3D"99"... \>. SimpleRSS wasn't
returning the attribute values (I'm not even sure if Yahoo's method is
compliant RSS). So I extended SimpleRSS to give me the values I want.
Then I added an basic RSSFeeder class which puts Net fetch and RSS
parse together, and adds caching, so that you can run it continuously
without hammering the server. The script takes a zipcode and an
optional -f to get today's forecast, too

-Adam

#------ weatherman.rb -------------
require 'net/http'
require 'simple-rss'

class Object
def metaclass; class << self; self; end; end
end #thanks, _why

#Extends Simple RSS to add tag attributes as methods to the tag object
# given <sometag var=3D"2">hello</sometag>,
# allows item.sometag =3D=3D> hello
# and item.sometag.var =3D=3D> 2
class SimpleRSSwAttributes < SimpleRSS
def clean_content(tag, attrs, content)
s=3D super
while n=3D (attrs =3D~ /((\w*)=3D"([^"]*)" )/mi)
attr_name =3D clean_tag($2)
s.metaclass.send:)attr_reader, attr_name)
s.instance_variable_set("@#{attr_name}",unescape($3))
attrs.slice!(n,$1.length)
end
s
end
def method_missing meth
nil
end
end

#Simple RSS feed reader.
# takes url, array of custom tags, and optional filename for caching result=
s
# provides #each_item and #item(title) methods

class RSSFeeder
def initialize feed_url, extra_tags=3D[], cache=3Dnil
raise 'Invalid URL' unless feed_url =3D~ /(.*\w*\.\w*\.\w*)(\/.*)/ =20
#separate host, rest
@url,@feed =3D $1, $2
@cache =3D cache
extra_tags.each{|tag| SimpleRSSwAttributes.feed_tags << tag}
end

#tyields [item,channel] for item with title matching name
def item name, &block
fetch
[email protected]{|item| item.title =3D~ name} if @data
yield [i,@data.channel] if i
end
def each_item &block
fetch
@data.items.each{|item| yield item}
end

private
def time_to_fetch?
@timestamp.nil? || (@timestamp < Time.now)
end

def fetch
#read the cache if we don't have data
if !@data && @cache
File.open(@cache, "r") {|f|
@timestamp =3D Time.parse(f.gets)
@data =3D SimpleRSSwAttributes.parse(f)
} if File.exists?(@cache)
end
#only fetch data from net if current data is expired
time_to_fetch? ? net_fetch : @data
end

def net_fetch
text =3D Net::HTTP.start(@url).get(@feed).body
@data =3D SimpleRSSwAttributes.parse(text)
#try to create a reasonable expiration date. Defaults to 10 mins in fut=
ure
date =3D @data.lastBuildDate || @data.pubDate ||
@data.expirationDate || Time.now
@timestamp =3D date + (@data.ttl ? @data.ttl.to_i*60 : 600)
@timestamp =3D Time.now + 600 if @timestamp < Time.now

File.open(@cache, "w+"){|f|
f.puts @timestamp;
f.write text
} if @cache
end
end


if __FILE__=3D=3D$0
exit(-1+puts("Usage #{$0} zipcode [-f]\nGives current temperature
for zipcode, "+
"-f to get forecast too").to_i) if ARGV.size < 1
zipcode =3D ARGV[0]

yahoo_tags =3D %w(yweather:condition yweather:location yweather:forecast=
)
w =3D RSSFeeder.new("xml.weather.yahoo.com/forecastrss?p=3D#{zipcode}",
yahoo_tags, "yahoo#{zipcode}.xml")
w.item(/Conditions/) { |item,chan|
puts "The #{item.title} are:\n\t#{chan.yweather_condition.temp}F and "+
"#{chan.yweather_condition.text}"
}
w.item(/Conditions/) { |item,chan|
puts "\nThe forecast for #{chan.yweather_location.city}, "+
"#{chan.yweather_location.region} for #{chan.yweather_forecast.day}, "+
"#{chan.yweather_forecast.date} is:\n"+
"\t#{chan.yweather_forecast.text} with a high of
#{chan.yweather_forecast.high} "+
"and a low of #{chan.yweather_forecast.low}"
} if ARGV[1]=3D~/f/i
#catch errors
w.item(/not found/) { |item,chan| puts item.description }

#Alternate feed
#w2 =3D RSSFeeder.new("rss.weather.com/weather/rss/local/#{zipcode}?cm_ve=
n=3DLWO&cm_cat=3Drss&par=3DLWO_rss")
#w2.item(/Current Weather/){|item,rss|
# puts item.title,item.description.gsub(/&deg;/,248.chr)}
#w2.item(/10-Day Forecast/){|item,rss|
# puts item.title,item.description.gsub(/&deg;/,248.chr)} if ARGV[1]=3D=
~/f/i

end
 
H

Hal Fulton

Dave said:
I still haven't done last week's metakoans, but I thought to myself, "Dave,"
(because I always address myself by name in my thoughts). "Dave," I thought,
"a universal Ruby-based indicator of the current temperature that works in
any location on any Ruby platform would be a great boon not only to you, but
to the entire Ruby community. In fact, why stop at the temperature? Ruby has
the power to turn almost any device into a fully functional weather station,
measuring rain, wind and snow. The world will be amazed."

Amazing, Dave. A boon to hackers everywhere.

But what is this "Outside" you speak of? I don't think I have
that installed...


Hal
 
H

Harley Pebley

Write a Ruby program such that given a certain argument to the program it
will return the current temperature of that location. People living in
the United States may be interested in temperature by ZIP code:

$ ruby current_temp.rb 47201

I've been lurking for a bit over 2 months as I've been reading
here and there about Ruby.

Anyway my first quiz submission (which is also my second Ruby script):

require 'net/http'
require 'rexml/document'

zip = ARGV[0].to_s

if zip.length > 0 then
h = Net::HTTP.new('rss.weather.com', 80)
resp, data = h.get('/weather/rss/local/'+zip, nil)

doc = REXML::Document.new data
doc.elements.each('rss/channel/item/title') { |element|
if element.text[0,7] == 'Current' then
puts element.text
desc = element.get_elements('../description').to_s.strip
puts desc.slice(22,desc.length-57).sub('&deg;', 'degrees')
end
}
else
puts 'Need a ZIP code as a command line parameter.'
end
 
J

James Edward Gray II

I've been lurking for a bit over 2 months as I've been reading
here and there about Ruby.

Anyway my first quiz submission (which is also my second Ruby script):

Let me be one of the first to welcome you then! Script looks great
for the second on, by the way.

James Edward Gray II
 
H

Harley Pebley

Let me be one of the first to welcome you then! Script looks great
for the second on, by the way.

Thanks! It's lacking a bunch of error checking, input validation and the
like, but it suffices for now. And I can look at some of the other entries
to get some of those details.

The quiz is a great idea.

Regards,
Harley Pebley
 
G

gordon

Jay,

I liked your idea of searching for the location id, so I added it to my
original submission. If you use the -s option, it will give you a menu
of choices. If it only finds one choice it will just return the
temperature.

Examples:

c:\>ruby c:\current_temp.rb madrid -s
1. Madrid, Nebraska, United States
2. Madrid, New York, United States
3. Madrid, Iowa, United States
4. Madrid, Spain
5. General La Madrid, Argentina
6. New Madrid, Missouri, United States
Please choose your location 4

The current temperature in Madrid, is 37 degrees F.

c:\>ruby current_temp.rb "madrid, spain" -s

The current temperature in Madrid, is 37 degrees F.

# current_temp.rb

require 'net/http'
require 'rexml/document'
require 'optparse'
require "rubygems"
require "highline/import"
require 'cgi'

class LocationSearch
attr_reader :loc

def initialize(string)
city = CGI.escape(string)

h = Net::HTTP.new('weather.yahoo.com', 80)
resp, data = h.get("/search/weather2?p=#{city}", nil)

case resp
when Net::HTTPSuccess then @loc = location_menu(
parse_locations(data) )
when Net::HTTPRedirection then @loc =
get_location(resp['location'])
end
end

def location_menu(hash)
choose do |menu|
menu.prompt = "Please choose your location "
hash.each do |key,val|
menu.choice val do return key end
end
end
end

def parse_locations(data)
a = {}
data.split("\n").each do |i|
a[get_location(i)]=strip_html(i) if /a href="\/forecast/ =~ i
end
a
end

def strip_html(str)
str = str.strip || ''
str.gsub(/<(\/|\s)*[^>]*>/,'')
end

def get_location(string)
string.split(/\/|\./)[2]
end

end


class CurrentTemp
include REXML

def initialize(loc,u='f')
uri = "http://xml.weather.yahoo.com/forecastrss?p=#{loc}&u=#{u}"
@Doc = Document.new Net::HTTP.get(URI.parse(uri))
raise "Invalid city, \"#{loc}\"" if /error/i =~
@doc.elements["//description"].to_s
end

def method_missing(methodname)
XPath.match(@doc,"//*[starts-with(name(), 'yweather')]").each
do|elem|
return elem.attributes[methodname.to_s] if
elem.attributes[methodname.to_s]
end
Object.method_missing(methodname)
end

def unit
self.temperature
end

def state
self.region
end

def to_s
"The current temperature in #{self.city}, #{self.state} is
#{self.temp} degrees #{self.unit}."
end

end

begin

opts = OptionParser.new
opts.banner = "Usage:\n\n current_temp.rb city [-u unit]\n\n
"
opts.banner += "city should be a zip code, or a Yahoo Weather
location id.\n\n"
opts.on("-uARG", "--unit ARG","Should be f or c", String) {|val| @u
= val }
opts.on("-s", "--search","Search location") {@search = true}
opts.on("-h", "--help") {puts opts.to_s ; exit 0}

loc = opts.parse!.to_s
@u ||='f'

if @search
loc = LocationSearch.new(loc).loc
end

if loc.empty?
raise "Invalid city, \"#{loc}\""
else
puts
puts CurrentTemp.new(loc,@u)
end

rescue
puts $!
puts opts.to_s
exit 1
end
 
H

horndude77

Cool. I'd tried to do something like that, but couldn't quite get it to
work (because of the yahoo redirect). Looking at your code helped me
figure it out. I looked at net/http and then more at what open-uri
provides. Lo and behold it provides some meta-data that helps with the
redirect problem. Anyways thanks gordon! I added my now fixed function
below.

-----Jay Anderson

require 'rexml/document'
require 'open-uri'

LOC_MATCH = /\/forecast\/([^.]+)\.html/

#Searches Yahoo and returns an array of location ids
def yahoo_loc_search(loc)
return [loc] if loc =~ /\d/ #places usually don't have numbers in
their names
locs = []

open("http://weather.yahoo.com/search/weather2?p=#{URI.escape(loc)}")
do |http|
return [$1] if http.base_uri.to_s =~ LOC_MATCH
http.each {|line| locs << $1 if line =~ LOC_MATCH }
end
locs
end

#Returns a hash containing the location and temperature information
#Accepts US zip codes or Yahoo location id's
def yahoo_weather_query(loc_ids, units)
weather = []
loc_ids.each do |l|
h = {}

open("http://xml.weather.yahoo.com/forecastrss?p=#{l}&u=#{units}") do
|http|
response = http.read
doc = REXML::Document.new(response)
channel = doc.root.elements['channel']
title = channel.elements['title'].text
if title !~ /Error/ then
location = channel.elements['yweather:location']
h[:city] = location.attributes["city"]
h[:region] = location.attributes["region"]
h[:country] = location.attributes["country"]
h[:temp] =
channel.elements["item"].elements["yweather:condition"].attributes["temp"]
weather << h
end
end
end
weather
end

if ARGV.length < 1 then
puts "usage: #$0 <location> [f|c]"
exit
end
loc_id = ARGV[0]
units = (ARGV[1] || 'f').downcase
units = (units =~ /^(f|c)$/) ? units : 'f'

loc_ids = yahoo_loc_search(loc_id)
weather_info = yahoo_weather_query(loc_ids, units)

puts "No matches found" if weather_info.size == 0

weather_info.each do |w|
city = w[:city]
region = w[:region]
country = w[:country]
temp = w[:temp]

final_loc = "#{city}, #{region}#{', ' if region!="" and
country!=""}#{country}"
puts "The temperature in #{final_loc} is #{temp} degrees
#{units.upcase}"
end
 
P

Patrick Chanezon

------=_Part_8096_3979425.1141178336849
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

An answer just for the fun of it, showing that you write as obscure and
unmaintainable code in Ruby as in Perl. The challenge was to get all the
functionality in a one liner, in order to see how far you can stretch ruby
expressions. They seem to stretch pretty well:)

It uses Google, so it supports only zip codes as input.

require 'net/http'
puts ((ARGV.length !=3D 1) ? "Usage: #$0 <zip code>" : (["The temperature
in"] + (/Weather<\/b> for <b>(.*)<\/b>.*\D(\d+)&deg;F/.match(Net::HTTP.get(
URI.parse("http://www.google.com/search?hl=3Den&q=3Dtemperature+#{ARGV[0]}"=
)))[1,2].collect!
{|x| " is " + x})).to_s.gsub!(/in is /, "in ") + " degree F")

/temp.rb 94117
The temperature in San Francisco, CA is 57 degree F
/temp.rb
Usage: ./temp.rb <zip code>


The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz
until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps
everyone
on Ruby Talk follow the discussion.


-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-= =3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D=
-=3D-=3D-=3D

by Caleb Tennis

Write a Ruby program such that given a certain argument to the program it
will return the current temperature of that location. People living in
the United States may be interested in temperature by ZIP code:

$ ruby current_temp.rb 47201
The temperature in Columbus, Indiana is 32 degrees F.

Other locales may want to use their own mailing codes, or city names:

$ ruby current_temp.rb madrid
The temperature in Madrid, Spain is 12 degrees C.

Which arguments you support is up to you.


--
Patrick Chanezon, AdWords API evangelist
http://blog.chanezon.com/
http://www.google.com/apis/adwords/

------=_Part_8096_3979425.1141178336849--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top