Sha256: ff9c6b6b68e49ac48a5135b82d95588580f333e559ef6a1133e1726fa420fea3

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require 'typhoeus'
require 'json'

module Darksky
  class API
    DARKSKY_API_URL = 'https://api.darkskyapp.com/v1'

    # Create a new instance of the Darksky::API using your API key.
    #
    # @param api_key [String] Dark Sky API key.
    def initialize(api_key)
      @api_key = api_key
    end

    # Returns a forecast for the next hour at a given location.
    #
    # @param latitude [String] Latitude in decimal degrees.
    # @param longitude [String] Longitude in decimal degrees.
    def forecast(latitude, longitude)
      response = Typhoeus::Request.get("#{DARKSKY_API_URL}/forecast/#{@api_key}/#{latitude},#{longitude}")
      JSON.parse(response.body) if response.code == 200
    end

    # Returns forecasts for a collection of arbitrary points.
    #
    # @param latitudes_longitudes_times [Array] Triplets of latitude, longitude and time. Example: ['42.7','-73.6',1325607100,'42.0','-73.0',1325607791]
    def precipitation(latitudes_longitudes_times)
      return if latitudes_longitudes_times.size % 3 != 0
      response = Typhoeus::Request.get("#{DARKSKY_API_URL}/precipitation/#{@api_key}/#{latitudes_longitudes_times.join(',')}")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
darksky-1.0.0 lib/darksky/api.rb