# Current Temperature (#68)

**URL:** <https://www.ruby-forum.com/t/current-temperature-68/54152>\
**Category:** Ruby\
**Created:** [February 24, 2006, 3:15pm UTC](https://www.ruby-forum.com/t/current-temperature-68/54152 "2006-02-24T15:15:44Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [March 1, 2006, 3:18am UTC](https://www.ruby-forum.com/t/current-temperature-68/54152/21 "2006-03-01T03:18:34Z")

</div>

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 != 1) ? “Usage: #$0 ” : ([“The temperature  
in”] + (/Weather\</b\> for  
**(._)\</b\>._\D(\d+)°F/.match(Net::HTTP.get(  
URI.parse(“[temperature - Google Search](http://www.google.com/search?hl=en&q=temperature+#%7BARGV%5B0%5D%7D)”)))[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

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [February 28, 2006, 7:10am UTC](https://www.ruby-forum.com/t/current-temperature-68/54152/22 "2006-02-28T07:10:21Z")

</div>

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

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(“[Santa Clara, United States - Weather Forecasts | Maps | News - Yahoo Weather](http://weather.yahoo.com/search/weather2?p=#%7BURI.escape(loc)%7D)”)  
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}](http://xml.weather.yahoo.com/forecastrss?p=#%7Bl%7D&u=#%7Bunits%7D)”) 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 [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

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [May 18, 2016, 8:50pm UTC](https://www.ruby-forum.com/t/current-temperature-68/54152/23 "2016-05-18T20:50:01Z")

</div>

Hi all!

Is there any way one of you could modify your web scraping application  
so that it functions for historical weather data? I want to be able to  
enter a zip code and get the hourly weather for every day of 2015. Any  
ideas? =)

Thanks!

[Previous page](https://www.ruby-forum.com/t/current-temperature-68/54152.md?page=1)
