Sha256: e2c9fe6ed7be734e2279f281542dcb5f380759a291d2f8de7aa12e1149716cfd

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

module OpenWeatherAPI
  class API

    attr_accessor :api_key, :default_language, :default_country_code, :default_units

    def initialize(options = {})
      @api_key = options[:api_key] || options['api_key']
      @default_language = options[:default_language] || options['default_language'] || 'en'
      @default_country_code = options[:default_country_code] || options['default_country_code']
      @default_units =  options[:default_units] || options['default_units'] || 'metric'
    end

    def current(**args, &block)
      fetch_current.execute(**args, &block)
    end

    # Not yet implemented
    def forecast(type = :hourly, **args, &block)
      raise ArgumentError, "Invalid #{type} forecast type."

      self.send("fetch_forecast_#{type}").execute(**args, &block)
    end

    def icon_url(icon_code)
      "http://openweathermap.org/img/w/#{icon_code}.png"
    end

    private

    VALID_FORECAST_TYPES = [:hourly, :daily]

    def valid_forecast_type?(type)
      VALID_FORECAST_TYPES.include? type.to_sym
    end

    def fetch_current
      @current ||= Resources::Current.new self
    end

    def fetch_forecast_hourly
      @forecast_hourly ||= Resources::ForecastHourly.new self
    end

    def fetch_forecast_daily
      @forecast_daily ||= Resources::ForecastHourly.new self
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
open-weather-api-0.0.4 lib/open-weather-api/api.rb
open-weather-api-0.0.3 lib/open-weather-api/api.rb