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 =
{
}

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.



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

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

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

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

Parameters:

  • latitude (String)

    Latitude in decimal degrees.

  • longitude (String)

    Longitude in decimal degrees.

  • option (Hash)

    (Optional) Options to be passed to the Typhoeus::Request



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

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

#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.

  • option (Hash)

    (Optional) Options to be passed to the Typhoeus::Request



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

#interesting(options = {}) ⇒ Object

Returns a list of interesting storms happening right now.

Parameters:

  • option (Hash)

    (Optional) Options to be passed to the Typhoeus::Request



54
55
56
57
# File 'lib/darksky/api.rb', line 54

def interesting(options = {})
  response = Typhoeus::Request.get("#{DARKSKY_API_URL}/interesting/#{@api_key}", 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]

  • option (Hash)

    (Optional) Options to be passed to the Typhoeus::Request



41
42
43
44
45
46
47
48
49
# File 'lib/darksky/api.rb', line 41

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