Sha256: 27c89165d4114b304b48c342e56113a73645afcf8ffc352d139ff2f934405c74

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

# represents an hour result from Stormglass
# VALUES represent callable methods
class Stormglass::Hour

  VALUES = [:air_temperature,:cloud_cover,:current_direction,:current_speed,:gust,:humidity,
            :precipitation,:pressure,:sea_level,:swell_direction,:swell_height,:swell_period,
            :visibility,:water_temperature,:wave_direction,:wave_height,:wave_period,
            :wind_direction,:wind_speed,:wind_wave_direction,:wind_wave_height,:wind_wave_period]

  def initialize(src)
    @src = src
  end

  def src
    @src
  end

  def time
    Time.parse(src["time"])
  end

  def values
    src.keys.collect(&:underscore)
  end

  def method_missing(method, *args)
    if VALUES.include?(method)
      get_value(method, args)
    else
      super
    end
  end

  def respond_to_missing?(method_name, include_private = false)
    VALUES.include?(method_name) || super
  end

  def inspect
    "#<#{self.class.to_s} time='#{time}'> "
  end

  # handler for each VALUES method (such as air_temperature)
  # takes two optional arguments:
  # data_source: - data source to use. (default 'sg')
  # unit_type:   - preferred unit type (default API result)
  def get_value(attribute,args)
    vals = fetch_value(args.first ? {attribute: attribute}.merge(args.first) : {attribute: attribute})
    @src.keys.collect(&:underscore).zip(vals).to_h[attribute.to_s]
  end

  def fetch_value(data_source: nil, attribute:, unit_type: nil)
    data_source ||= Stormglass.settings.source
    @src.values.collect do |val|
      if val.is_a?(String)
        val
      else
        Stormglass::Value.new(attribute, val, data_source, unit_type)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stormglass-0.2.0 lib/stormglass/hour.rb
stormglass-0.1.1 lib/stormglass/hour.rb