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.1.0"

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.



72
73
74
75
# File 'lib/wwo.rb', line 72

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.



80
81
82
# File 'lib/wwo.rb', line 80

def connection=(connection)
  @connection = connection
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.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wwo.rb', line 21

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
    historic(latitude, longitude, date)
  else
    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.



42
43
44
45
46
47
48
49
50
51
# File 'lib/wwo.rb', line 42

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_response = get(uri)

  if api_response.success?
    return Hashie::Mash.new(MultiJson.load(api_response.body))
  end
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.



60
61
62
63
64
65
66
67
68
69
# File 'lib/wwo.rb', line 60

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_response = get(uri)

  if api_response.success?
    return Hashie::Mash.new(MultiJson.load(api_response.body))
  end
end