module Wwo

Constants

VERSION

Public Class Methods

connection() click to toggle source

Build or get an HTTP connection object.

# File lib/wwo.rb, line 128
def connection
  return @connection if @connection
  @connection = Faraday.new
end
connection=(connection) click to toggle source

Set an HTTP connection object.

@param connection Connection object to be used.

# File lib/wwo.rb, line 136
def connection=(connection)
  @connection = connection
end
date_range(start_date, end_date, latitude, longitude, forecast_compat = false) click to toggle source

Returns a daily breakdown for weather for the provided start and end data at the specified location.

# File lib/wwo.rb, line 20
def date_range(start_date, end_date, latitude, longitude, forecast_compat = false)
  start_date_string = start_date.strftime('%F')
  end_date_string   = end_date.strftime('%F')
  today_date_string = Time.now.strftime('%F')

  if start_date_string == today_date_string || range_in_future?(start_date, end_date)
    uri = "#{Wwo.api_endpoint}/weather.ashx?q=#{latitude},#{longitude}&format=json&num_of_days=7&date=today&cc=no&mca=no&tp=24&key=#{Wwo.api_key}"
    response = Hashie::Mash.new(api_call(uri))
    if forecast_compat
      return make_into_forecast_response(response)
    else
      return response
    end

  elsif starts_in_past_but_ends_in_future?(start_date, end_date)
    yesterday_date_string = (Time.now - (24 *(60 * 60))).strftime('%F')

    uri = "#{Wwo.api_endpoint}/past-weather.ashx?q=#{latitude},#{longitude}&format=json&extra=utcDateTime&date=#{start_date_string}&enddate=#{yesterday_date_string}&show_comments=no&tp=24&key=#{Wwo.api_key}&mca=false&show_comments=false"
    past_response = Hashie::Mash.new(api_call(uri))
    uri = "#{Wwo.api_endpoint}/weather.ashx?q=#{latitude},#{longitude}&format=json&num_of_days=7&cc=no&mca=no&tp=24&key=#{Wwo.api_key}"
    future_response = Hashie::Mash.new(api_call(uri))


    if forecast_compat
      past    = make_into_forecast_response(past_response)
      future  = make_into_forecast_response(future_response)
      past[:daily][:data] = (past[:daily][:data] + future[:daily][:data]).flatten.uniq

      return past
    else
      return past_response.deep_merge(future_response)
    end

  else
    uri = "#{Wwo.api_endpoint}/past-weather.ashx?q=#{latitude},#{longitude}&format=json&extra=utcDateTime&date=#{start_date_string}&enddate=#{end_date_string}&show_comments=no&tp=24&key=#{Wwo.api_key}&mca=false&show_comments=false"
    response = Hashie::Mash.new(api_call(uri))
    if forecast_compat
      return make_into_forecast_response(response)
    else
      return response
    end
  end
end
forecast(latitude, longitude, options = {}) click to toggle source

Provides API compatibility to forecast.io's rubygem - expects the same signature and a Unix Timestamp for :time, it will use the historic / now_or_later methods under the hood to actually do its work.

# File lib/wwo.rb, line 86
def forecast(latitude, longitude, options = {})
  if options[:time]
    date = Time.at(options[:time])
  else
    date = Time.now
  end

  if date.to_i < Time.now.to_i
    make_into_forecast_response(historic(latitude, longitude, date))
  else
    make_into_forecast_response(now_or_later(latitude, longitude, date))
  end
end
historic(latitude, longitude, date_or_timestamp) click to toggle source

Returns historic weather at the provided latitude and longitude coordinates, on a specific date.

@param latitude [String] Latitude. @param longitude [String] Longitude. @param date [Date] or [Integer] Date, or Unix Timestamp.

# File lib/wwo.rb, line 107
def historic(latitude, longitude, date_or_timestamp)
  date = date_or_timestamp.is_a?(Numeric) ? Time.at(date_or_timestamp).strftime("%F") : date_or_timestamp.strftime("%F")
  uri = "#{Wwo.api_endpoint}/past-weather.ashx?q=#{latitude},#{longitude}&date=#{date}&tp=24&format=json&key=#{Wwo.api_key}"
  api_call(uri)
end
now_or_later(latitude, longitude, date_or_timestamp = Date.today) click to toggle source

Returns historic weather at the provided latitude and longitude coordinates, on a specific date.

@param latitude [String] Latitude. @param longitude [String] Longitude. @param date [Date] or [Integer] Date, or Unix Timestamp.

# File lib/wwo.rb, line 120
def now_or_later(latitude, longitude, date_or_timestamp = Date.today)
  date = date_or_timestamp.is_a?(Numeric) ? Time.at(date_or_timestamp).strftime("%F") : date_or_timestamp.strftime("%F")
  uri = "#{Wwo.api_endpoint}/weather.ashx?q=#{latitude},#{longitude}&date=#{date}&num_of_days=1&tp=24&format=json&key=#{Wwo.api_key}"
  api_call(uri)
end
range_in_future?(start_date, end_date) click to toggle source
# File lib/wwo.rb, line 68
def range_in_future?(start_date, end_date)
  end_date.to_i >= Time.now.to_i && start_date.to_i >= Time.now.to_i
end
starts_in_past_but_ends_in_future?(start_date, end_date) click to toggle source
# File lib/wwo.rb, line 64
def starts_in_past_but_ends_in_future?(start_date, end_date)
  start_date.to_i < Time.now.to_i && end_date.to_i >= Time.now.to_i
end
today(latitude, longitude) click to toggle source

Returns an hourly breakdown for the weather “today” at the given location. We get the current time and then turn it into UTC. Returns a Hashie Mash with every hour of weather broken down.

# File lib/wwo.rb, line 76
def today(latitude, longitude)
  date = Time.now.utc.strftime("%F")
  uri = "#{Wwo.api_endpoint}/weather.ashx?q=#{latitude},#{longitude}&date=today&num_of_days=1&tp=1&format=json&key=#{Wwo.api_key}&mca=false&show_comments=false"
  api_call(uri)
end

Private Class Methods

api_call(uri) click to toggle source
# File lib/wwo.rb, line 142
def api_call(uri)
  api_response = get(uri)
  if api_response.success?
    return Hashie::Mash.new(MultiJson.load(api_response.body))
  else
    return {}
  end
end
get(path, params = {}) click to toggle source
# File lib/wwo.rb, line 151
def get(path, params = {})
  params = Wwo.default_params.merge(params || {})
  connection.get(path, params)
end
make_into_forecast_response(response) click to toggle source

Munges the repsonse into one like what we would expect from Forecast.io

# File lib/wwo.rb, line 158
def make_into_forecast_response(response)
  if response.is_a?(Hash) && response.empty?
    return { daily: { data: [] } }
  elsif ! response.data.weather.nil? && response.data.weather.any? && response.data.weather.size > 1
    data = { daily: { data: [] } }
    response.data.weather.each do |weather|
      icon = weather.hourly.first.weatherIconUrl.first.value
      condition =  icon.split('wsymbol_').last.split('_').first.to_i

      weatherCode = weather.hourly.first.weatherCode

      maxTemp = weather.maxtempC
      minTemp = weather.mintempC
      date = Time.parse("#{weather.date} 12:00:00")

      data[:daily][:data] << { icon: icon, "temperatureMax" => maxTemp, "temperatureMin" => minTemp, date: date, conditionCode: condition, weatherCode: weatherCode }
    end
    return data
  else
    data = { daily: { data: [ { icon: '', 'temperatureMax' => 0, 'temperatureMin' => 0  } ] }, alerts: nil }
    data[:daily][:data][0][:icon] = response.data.weather.first.hourly.first.weatherIconUrl.first.value
    data[:daily][:data][0]['temperatureMax'] = response.data.weather.first.maxtempC
    data[:daily][:data][0]['temperatureMin'] = response.data.weather.first.mintempC
    data[:daily][:data][0]["conditionCode"] = "999"
    data[:daily][:data][0]["weatherCode"] = weatherCode = response.data.weather.first.hourly.first.weatherCode

    return data
  end
end