class Accuweather::LocationService < ApplicationService
  def initialize(school)
    @school = school.decorate
    @path = 'http://dataservice.accuweather.com/locations/v1'
    @api_key = Rails.application.secrets.accuweather_key
  end

  def call
    params = { apikey: @api_key }
    if @school.zip
      @path += '/postalcodes/search'
      params[:q] = @school.zip
    elsif @school.city
      @path += "/cities/search?q=#{@school.city}"
      params[:q] = `#{@school.city}, #{@school.state} #{@school.country_name}`
    end
    resp = RestClient.get(@path, params: params)
    key = JSON.parse(resp.body).first['Key']
    @school.update(weather_id: key)
  end
end
