Sha256: 194c0ec339938bcda7dffcfaec9ce9d4c14cf75a0f7110a274ebe9cf2a937da5

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

require 'net/http'
require 'json'
require 'map'

require 'weather-api/astronomy'
require 'weather-api/atmosphere'
require 'weather-api/condition'
require 'weather-api/forecast'
require 'weather-api/image'
require 'weather-api/location'
require 'weather-api/response'
require 'weather-api/units'
require 'weather-api/utils'
require 'weather-api/version'
require 'weather-api/wind'

module Weather
  class << self
    # Yahoo! Weather info endpoint
    ROOT = "http://query.yahooapis.com/v1/public/yql"

    # Public: Looks up current weather information using WOEID
    #
    # woeid - Int - Where On Earth IDentifier -- unique ID for
    #         location to get weather data for. To find
    #         a WOEID, refer to Yahoo!'s documentation
    #         at http://developer.yahoo.com/weather/
    #
    # units - String - whether to retrieve data in Farenheit
    #         or Celsius. Defaults to Farenheit
    #
    # Returns a Weather::Response object containing forecast
    def lookup woeid, units = 'f'
      url = ROOT
      url += "?q=select%20*%20from%20weather.forecast%20"
      url += "where%20woeid%3D#{woeid}%20and%20u%3D'#{units}'&format=json"

      doc = get_response url
      Response.new woeid, url, doc
    end

    private
    def get_response url
      begin
        response = Net::HTTP.get_response(URI.parse url).body.to_s
      rescue => e
        raise "Failed to get weather [url=#{url}, e=#{e}]."
      end

      response = Map.new(JSON.parse(response))[:query][:results][:channel]

      if response.nil?
        raise "Failed to get weather [url=#{url}]."
      end

      response
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
weather-api-1.1.1 lib/weather-api.rb
weather-api-1.1.0 lib/weather-api.rb