[SOLUTION] Current Temperature (#68)

Discussion in 'Ruby' started by semmons99@gmail.com, Feb 26, 2006.

  1. Guest

    # Author: Shane Emmons
    #
    # Allows retrieval of current temperature information. Pretty simple
    # and straight forward. Uses match to extract data from the xml
    document
    # that is returned. Sorry about the long lines, but I like putting as
    # much code in one line as possible.
    #
    # usage: ruby current_temp.rb [zipcode|other]
    #
    # zipcode: US zipcode
    # other: country code information. example: SPXX0050 for Madrid,
    Spain

    require 'net/http'

    begin
    weather_info = Net::HTTP.get( "xml.weather.yahoo.com",
    "/forecastrss?p=".concat( ARGV[ 0 ] ) )
    print "The temperature in ", weather_info.match( /Yahoo! Weather
    for (.*)</ )[ 1 ], " is ", weather_info.match(
    /<yweather:condition.*temp="(\d+)"/ )[ 1 ], " degrees ",
    weather_info.match( /<yweather:units temperature="(.)"/ )[ 1 ], ".\n"
    rescue
    print "Information for ", ARGV[ 0 ], " was not found or
    unavailable."
    end
     
    , Feb 26, 2006
    #1
    1. Advertising

  2. On Feb 26, 2006, at 7:53 AM, wrote:

    > Sorry about the long lines, but I like putting as much code in one
    > line as possible.


    Repeat after me:

    "Horizontal scrolling is bad... Horizontal scrolling is bad...
    Horizontal scrolling is bad..."

    :D

    Seriously, thanks you for the submission.

    James Edward Gray II
     
    James Edward Gray II, Feb 26, 2006
    #2
    1. Advertising

  3. Guest

    Re: Current Temperature (#68)

    # Author: Shane Emmons
    #
    # Allows retrieval of current temperature information. Pretty simple
    # and straight forward. Uses match to extract data from the xml
    # document that is returned. Adjusted for easier horizontal reading.
    ;-)
    #
    # usage: ruby current_temp.rb [zipcode|other]
    #
    # zipcode: US zipcode
    # other: country code information.
    # example: SPXX0050 for Madrid, Spain

    require 'net/http'

    ARGV[ 0 ] = '48601'

    begin
    info = Net::HTTP.get(
    "xml.weather.yahoo.com", "/forecastrss?p=".concat( ARGV[ 0 ] )
    )

    location = info.match( /Yahoo! Weather for (.*)</ )[ 1 ]
    temperature = info.match( /<yweather:condition.*temp="(\d+)"/ )[ 1
    ]
    measured_in = info.match( /<yweather:units temperature="(.)"/ )[ 1
    ]

    print "The temperature in ",
    location, " is ", temperature, " degrees ", measured_in, ".\n"
    rescue
    print "Information for #{ ARGV[ 0 ] } was not found or
    unavailable.\n"
    end
     
    , Feb 27, 2006
    #3
  4. Guest

    Re: Current Temperature (#68)

    ** Sorry had my test code still in there for my location

    # Author: Shane Emmons
    #
    # Allows retrieval of current temperature information. Pretty simple
    # and straight forward. Uses match to extract data from the xml
    # document that is returned. Adjusted for easier horizontal reading.
    ;-)
    #
    # usage: ruby current_temp.rb [zipcode|other]
    #
    # zipcode: US zipcode
    # other: country code information.
    # example: SPXX0050 for Madrid, Spain

    require 'net/http'

    begin
    info = Net::HTTP.get(
    "xml.weather.yahoo.com", "/forecastrss?p=".concat( ARGV[ 0 ] )
    )

    location = info.match( /Yahoo! Weather for (.*)</ )[ 1 ]
    temperature = info.match( /<yweather:condition.*temp="(\d+)"/ )[ 1
    ]
    measured_in = info.match( /<yweather:units temperature="(.)"/ )[ 1
    ]

    print "The temperature in ",
    location, " is ", temperature, " degrees ", measured_in, ".\n"
    rescue
    print "Information for #{ ARGV[ 0 ] } was not found or
    unavailable.\n"
    end
     
    , Feb 27, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ruby Quiz

    [QUIZ] Current Temperature (#68)

    Ruby Quiz, Feb 24, 2006, in forum: Ruby
    Replies:
    19
    Views:
    266
    Patrick Chanezon
    Mar 1, 2006
  2. Rudolfs Osins

    [SOLUTION] Current Temperature (#68)

    Rudolfs Osins, Feb 26, 2006, in forum: Ruby
    Replies:
    1
    Views:
    103
    Rudolfs Osins
    Feb 26, 2006
  3. David Tran
    Replies:
    0
    Views:
    87
    David Tran
    Feb 27, 2006
  4. David Tran
    Replies:
    2
    Views:
    89
  5. Ruby Quiz
    Replies:
    1
    Views:
    89
    Uncle D
    Mar 2, 2006
Loading...

Share This Page