Class: OpenMeteoResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/open_meteo_client/open_meteo_response.rb

Instance Method Summary collapse

Constructor Details

#initialize(relevant_attributes) ⇒ OpenMeteoResponse

Returns a new instance of OpenMeteoResponse.



5
6
7
8
# File 'lib/open_meteo_client/open_meteo_response.rb', line 5

def initialize(relevant_attributes)
  @json_response = ''
  @relevant_attributes = relevant_attributes
end

Instance Method Details

#append_line(line) ⇒ Object



10
11
12
# File 'lib/open_meteo_client/open_meteo_response.rb', line 10

def append_line(line)
  @json_response += line unless line.nil?
end

#as_hashObject



14
15
16
17
# File 'lib/open_meteo_client/open_meteo_response.rb', line 14

def as_hash
  complete_hash = JSON.parse @json_response, :symbolize_names => true
  complete_hash.keep_if {|key, value| @relevant_attributes.include?(key)}
end

#as_listObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/open_meteo_client/open_meteo_response.rb', line 23

def as_list
  response_hash = as_hash
  forecasts = []

  response_hash.each_pair { |key, value|
    if value.is_a?(Array)
      value.each_with_index do |val, i|
        forecasts[i] ||= {}
        forecasts[i][key] = val
      end
    end
  }

  forecasts
end

#as_raw_jsonObject



19
20
21
# File 'lib/open_meteo_client/open_meteo_response.rb', line 19

def as_raw_json
  @json_response
end