Module: Wwo

Extended by:
Configuration
Defined in:
lib/wwo.rb,
lib/wwo/version.rb,
lib/wwo/configuration.rb

Defined Under Namespace

Modules: Configuration

Constant Summary collapse

VERSION =
"0.3.10"

Constants included from Configuration

Configuration::DEFAULT_FREE_ENDPOINT, Configuration::DEFAULT_PREMIUM_ENDPOINT

Instance Attribute Summary

Attributes included from Configuration

#api_cache_store, #api_endpoint, #api_key, #default_params, #use_premium_api

Class Method Summary collapse

Methods included from Configuration

configure

Class Method Details

.connectionObject

Build or get an HTTP connection object.



128
129
130
131
# File 'lib/wwo.rb', line 128

def connection
  return @connection if @connection
  @connection = Faraday.new
end

.connection=(connection) ⇒ Object

Set an HTTP connection object.

Parameters:

  • connection

    Connection object to be used.



136
137
138
# File 'lib/wwo.rb', line 136

def connection=(connection)
  @connection = connection
end

.date_range(start_date, end_date, latitude, longitude, forecast_compat = false) ⇒ Object

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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 = {}) ⇒ Object

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.



86
87
88
89
90
91
92
93
94
95
96
97
98
# 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) ⇒ Object

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

Parameters:

  • latitude (String)

    Latitude.

  • longitude (String)

    Longitude.

  • date (Date)

    or [Integer] Date, or Unix Timestamp.



107
108
109
110
111
# 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) ⇒ Object

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

Parameters:

  • latitude (String)

    Latitude.

  • longitude (String)

    Longitude.

  • date (Date)

    or [Integer] Date, or Unix Timestamp.



120
121
122
123
124
# 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) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# 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) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# 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) ⇒ Object

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.



76
77
78
79
80
# 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