Class: Darksky::API

Inherits:
Object
  • Object
show all
Defined in:
lib/darksky/api.rb

Constant Summary collapse

DARKSKY_API_URL =
'https://api.darkskyapp.com/v1'
DEFAULT_OPTIONS =
{
  :disable_ssl_peer_verification => true
}

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ API

Create a new instance of the Darksky::API using your API key.

Parameters:

  • api_key (String)

    Dark Sky API key.



14
15
16
# File 'lib/darksky/api.rb', line 14

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

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

Returns a forecast for the next hour at a given location.

Parameters:

  • latitude (String)

    Latitude in decimal degrees.

  • longitude (String)

    Longitude in decimal degrees.



22
23
24
25
# File 'lib/darksky/api.rb', line 22

def forecast(latitude, longitude, options = {})
  response = Typhoeus::Request.get("#{DARKSKY_API_URL}/forecast/#{@api_key}/#{latitude},#{longitude}", DEFAULT_OPTIONS.dup.merge(options))
  JSON.parse(response.body) if response.code == 200
end

#precipitation(latitudes_longitudes_times, options = {}) ⇒ Object

Returns forecasts for a collection of arbitrary points.

Parameters:

  • latitudes_longitudes_times (Array)

    Triplets of latitude, longitude and time. Example: [‘42.7’,‘-73.6’,1325607100,‘42.0’,‘-73.0’,1325607791]



30
31
32
33
34
# File 'lib/darksky/api.rb', line 30

def precipitation(latitudes_longitudes_times, options = {})
  return if latitudes_longitudes_times.size % 3 != 0
  response = Typhoeus::Request.get("#{DARKSKY_API_URL}/precipitation/#{@api_key}/#{latitudes_longitudes_times.join(',')}", DEFAULT_OPTIONS.dup.merge(options))
  JSON.parse(response.body) if response.code == 200
end