Module: Forecast::IO

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

Defined Under Namespace

Modules: Configuration

Constant Summary collapse

VERSION =

Current Forecast::IO version

'1.1.0'

Constants included from Configuration

Configuration::DEFAULT_FORECAST_IO_API_ENDPOINT

Instance Attribute Summary

Attributes included from Configuration

#api_endpoint, #api_key

Class Method Summary collapse

Methods included from Configuration

configure

Class Method Details

.connectionObject

Build or get an HTTP connection object.



34
35
36
# File 'lib/forecast_io.rb', line 34

def connection
  @connection ||= Faraday.new
end

.connection=(connection) ⇒ Object

Set an HTTP connection object.

Parameters:

  • connection

    Connection object to be used.



41
42
43
# File 'lib/forecast_io.rb', line 41

def connection=(connection)
  @connection = connection
end

.forecast(latitude, longitude, options = {}) ⇒ Object

Retrieve the forecast for a given latitude and longitude.

Parameters:

  • latitude (String)

    Latitude.

  • longitude (String)

    Longitude.

  • options (String) (defaults to: {})

    Optional parameters. Valid options are ‘:time` and `:params`.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/forecast_io.rb', line 18

def forecast(latitude, longitude, options = {})
  forecast_url = "#{Forecast::IO.api_endpoint}/forecast/#{Forecast::IO.api_key}/#{latitude},#{longitude}"
  forecast_url += ",#{options[:time]}" if options[:time]

  forecast_response = if options[:params]
    connection.get(forecast_url, options[:params])
  else
    connection.get(forecast_url)
  end

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