Class: Stormglass::Hour

Inherits:
Object
  • Object
show all
Defined in:
lib/stormglass/hour.rb

Overview

represents an hour result from Stormglass VALUES represent callable methods

Constant Summary collapse

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]

Instance Method Summary collapse

Constructor Details

#initialize(src) ⇒ Hour

Returns a new instance of Hour.



10
11
12
# File 'lib/stormglass/hour.rb', line 10

def initialize(src)
  @src = src
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/stormglass/hour.rb', line 26

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

Instance Method Details

#fetch_value(data_source: nil, attribute:, unit_type: nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/stormglass/hour.rb', line 51

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

#get_value(attribute, args) ⇒ Object

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)



46
47
48
49
# File 'lib/stormglass/hour.rb', line 46

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

#inspectObject



38
39
40
# File 'lib/stormglass/hour.rb', line 38

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

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/stormglass/hour.rb', line 34

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

#srcObject



14
15
16
# File 'lib/stormglass/hour.rb', line 14

def src
  @src
end

#timeObject



18
19
20
# File 'lib/stormglass/hour.rb', line 18

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

#valuesObject



22
23
24
# File 'lib/stormglass/hour.rb', line 22

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