Class: Tropical::OpenWeatherMap

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

Constant Summary collapse

BASE_URL =
"https://api.openweathermap.org/data/2.5/forecast?".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ OpenWeatherMap

Returns a new instance of OpenWeatherMap.



14
15
16
17
18
19
# File 'lib/tropical.rb', line 14

def initialize(params)
  @params  = params
  response = post(request_params)

  load_data(response)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/tropical.rb', line 12

def data
  @data
end

#paramsObject (readonly)

Returns the value of attribute params.



12
13
14
# File 'lib/tropical.rb', line 12

def params
  @params
end

#statusObject (readonly)

Returns the value of attribute status.



12
13
14
# File 'lib/tropical.rb', line 12

def status
  @status
end

Instance Method Details

#city_nameObject



21
22
23
# File 'lib/tropical.rb', line 21

def city_name
  data["city"]["name"]
end

#coordObject



33
34
35
# File 'lib/tropical.rb', line 33

def coord
  data["city"]["coord"].transform_keys(&:to_sym)
end

#countryObject



25
26
27
# File 'lib/tropical.rb', line 25

def country
  data["city"]["country"]
end

#current_dateObject



37
38
39
# File 'lib/tropical.rb', line 37

def current_date
  list.first[:dt]
end

#current_tempObject



41
42
43
# File 'lib/tropical.rb', line 41

def current_temp
  list.first[:temp]
end

#current_weatherObject



45
46
47
# File 'lib/tropical.rb', line 45

def current_weather
  list.first[:description]
end

#full_sumaryObject



58
59
60
61
62
# File 'lib/tropical.rb', line 58

def full_sumary
  "#{sumary_current_day} "\
  "Média para os próximos dias: "\
  "#{sumary_days_forecast}"
end

#populationObject



29
30
31
# File 'lib/tropical.rb', line 29

def population
  data["city"]["population"]
end

#scaleObject



49
50
51
52
53
54
55
56
# File 'lib/tropical.rb', line 49

def scale
  units = params[:units]

  return "°C" if units == "metric"
  return "°F" if units == "imperial"

  "°K"
end

#sumary_current_dayObject



64
65
66
67
# File 'lib/tropical.rb', line 64

def sumary_current_day
  "#{current_temp.round}#{scale} e #{current_weather} em "\
  "#{city_name} em #{current_date.strftime("%d/%m")}."
end

#sumary_days_forecastObject



69
70
71
72
73
74
75
# File 'lib/tropical.rb', line 69

def sumary_days_forecast
  list = average_temp_by_days.map do |x|
    "#{x[:average]}#{scale} em #{x[:day].strftime("%d/%m")}"
  end

  "#{list.to_sentence(words_connector: ", ", last_word_connector: " e ")}."
end