Class: Meteoservice::Predict

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/meteoservice/predict.rb

Constant Summary

Constants included from Constants

Constants::BASE_URL, Constants::CLOUDINESS, Constants::DAYS_OF_WEEK, Constants::KEYS_DATA, Constants::KEYS_LIMIT, Constants::KEYS_PHENOMENA, Constants::PRECIPITATION, Constants::RPOWER, Constants::SPOWER, Constants::WIND_DIRECTION

Instance Method Summary collapse

Constructor Details

#initialize(forecasts) ⇒ Predict

Returns a new instance of Predict.



5
6
7
8
9
10
11
12
13
# File 'lib/meteoservice/predict.rb', line 5

def initialize(forecasts)
  @date = forecasts.data
  @phenomena = forecasts.phenomena
  @pressure = forecasts.pressure
  @temperature = forecasts.temperature
  @wind = forecasts.wind
  @relwet = forecasts.relwet
  @heat = forecasts.heat
end

Instance Method Details

#day_weekObject

Какой день недели?



16
17
18
19
20
21
22
# File 'lib/meteoservice/predict.rb', line 16

def day_week
  firstday_wday = Date.new(@date[0], @date[1], @date[2]).wday

  firstday_wday = 7 if firstday_wday.zero?

  DAYS_OF_WEEK[firstday_wday - 1]
end

#temperature_range_stringObject

Добавим знак “+” к температуре, если она положительная



53
54
55
56
57
58
59
60
# File 'lib/meteoservice/predict.rb', line 53

def temperature_range_string
  result = ''
  result << '+' if @temperature[0].positive?
  result << "#{@temperature[0]}.."
  result << '+' if @temperature[1].positive?
  result << @temperature[1].to_s
  result
end

#to_sObject

Вывод результата в терминал



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/meteoservice/predict.rb', line 25

def to_s
  times_day = 'ночью' if Range.new(*[0, 5].map(&:to_i)).include? @date[3]
  times_day = 'утром' if Range.new(*[6, 12].map(&:to_i)).include? @date[3]
  times_day = 'днём' if Range.new(*[13, 17].map(&:to_i)).include? @date[3]
  times_day = 'вечером' if Range.new(*[18, 23].map(&:to_i)).include? @date[3]

  result = if today?
             "Сегодня во #{day_week}, #{times_day}\n" if day_week == "вторник"
             "Сегодня в #{day_week}, #{times_day}\n" unless day_week == "вторник"
           else
             "#{DateTime.new(@date[0], @date[1], @date[2], @date[3], 0, 0)
      .strftime('%F')} в #{day_week}, #{times_day}\n"
           end

  result << "Температура: #{temperature_range_string} °С\n" \
    "Ветер: #{@wind[0]}..#{@wind[1]} м/с, #{WIND_DIRECTION[@wind[2].to_i]}\n" \
    "Атмосферное давление: #{@pressure[0]}..#{@pressure[1]} мм.рт.ст.\n" \
    "Облачность: #{CLOUDINESS[@phenomena[0].to_i]}\n" \
    "Осадки: #{PRECIPITATION[@phenomena[1].to_i]}\n" \
    "Интенсивность осадков: #{RPOWER[@phenomena[2].to_i]}\n" \
    "Вероятность грозы: #{SPOWER[@phenomena[3].to_i]}\n" \
    "Относительная влажность воздуха: #{@relwet[0]}..#{@relwet[1]} %\n" \
    "Комфорт: #{@heat[0]}..#{@heat[1]} °С"

  result
end

#today?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/meteoservice/predict.rb', line 62

def today?
  Date.today.strftime("%d") == @date[2].to_s
end