Sha256: 5b59435167c2f5f08a052a714883be62902986f2bc1f091564a2a80d23f8b4ba

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require "uri"
require "faraday"

module DarkSky; end
class DarkSky::API

  # Get the Dark Sky API credentials via typical Rails appplication credentials.
  #
  # @return [Hash] the credentials
  #
  def self.credentials
    @credentials ||= Rails.application.credentials.env[Rails.env.to_sym][:dark_sky][:api]
  end

  # Get the Dark Sky API secret key via the typical Rails appplication credentials.
  #
  # @return [String] the secret key
  #
  def self.secret_key
    @secret_key ||= credentials[:secret_key]
  end

  # Craft a Dark Sky API URL suitable to get a weather forecast for a given place.
  #
  # @param [Decimal] latitude
  # @param [Decimal] longitude
  # @return [URI] URI of the request
  #
  def self.uri(latitude:, longitude:)
    URI("https://api.darksky.net/forecast/#{secret_key}/#{latitude},#{longitude}")
  end

  # Get a Dark Sky API via Faraday
  #
  # @param [Decimal] latitude
  # @param [Decimal] longitude
  # @return [Faraday::Response] response
  #
  def self.get(latitude:, longitude:)
    Faraday.get uri(latitude: latitude, longitude: longitude)
  end

  def self.icon_keys
    [
        "clear-day",
        "clear-night",
        "rain",
        "snow",
        "sleet",
        "wind",
        "fog",
        "cloudy",
        "partly-cloudy-day",
        "partly-cloudy-night",
    ]
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sixarm_ruby_rails_dark_sky_api-1.1.0 lib/sixarm_ruby_rails_dark_sky_api/dark_sky/api.rb