class School::AccuweatherController < ApplicationController
  def show
    if weather
      render_success :ok, json: weather_props(weather)
    else
      render_error :unprocessable_entity, message: 'Weather unavailable'
    end
  end

  private
    def weather
      @weather ||= current_school.weather
    end

    def weather_props(weather)
      weather_format = current_school.weather_format
      {}.tap do |prop|
        prop[:text] = weather['WeatherText']
        prop[:icon] = weather['WeatherIcon']
        prop[:format] = weather_format
        prop[:temp] = if weather_format == 'C'
          weather['Temperature']['Metric']['Value']
        else
          weather['Temperature']['Imperial']['Value']
        end
      end
    end
end
