Class: OrbDef::FireWeatherData

Inherits:
Object
  • Object
show all
Defined in:
app/services/orb_def/fire_weather_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fire) ⇒ FireWeatherData

Returns a new instance of FireWeatherData.



7
8
9
# File 'app/services/orb_def/fire_weather_data.rb', line 7

def initialize(fire)
  @fire = fire
end

Instance Attribute Details

#fireObject (readonly)

Returns the value of attribute fire.



5
6
7
# File 'app/services/orb_def/fire_weather_data.rb', line 5

def fire
  @fire
end

Instance Method Details

#create_station_and_readingObject



21
22
23
24
25
26
27
28
29
30
# File 'app/services/orb_def/fire_weather_data.rb', line 21

def create_station_and_reading
  reading_json = OpenWeatherApi::WeatherByCoordinates.fetch(latitude: fire.latitude, longitude: fire.longitude)

  return unless reading_json

  weather_station = WeatherStation.find_or_create(reading_json)
  weather_reading = WeatherReading.find_or_create_by_reading(reading_json, weather_station.id)

  return weather_station, weather_reading
end

#find_or_create_weatherObject



11
12
13
14
15
16
17
18
19
# File 'app/services/orb_def/fire_weather_data.rb', line 11

def find_or_create_weather
  weather_station = WeatherStation.within(10, origin: [fire.latitude, fire.longitude]).first

  if weather_station.present? && weather_reading_within_limits?(weather_station)
    return weather_station, weather_station.weather_readings.last
  else
    create_station_and_reading
  end
end

#weather_reading_within_limits?(weather_station) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/services/orb_def/fire_weather_data.rb', line 32

def weather_reading_within_limits?(weather_station)
  weather_station.weather_readings&.last.reading_at.between?(fire.detected_at - 30.minutes, fire.detected_at + 30.minutes)
end