class SimpleOpenWeatherMap::Weather

Attributes

icon_path[RW]

Public Instance Methods

current(config) click to toggle source
# File lib/simple_open_weather_map/weather.rb, line 9
def current(config)
  raise "SimpleOpenWeatherMap::Config only!" unless config.is_a?(SimpleOpenWeatherMap::Config)

  responseJson = http_connection(SimpleOpenWeatherMap::PATH_CURRENT).get_contents(current_params(config))
  return nil if responseJson.nil? || responseJson.empty?

  @current = ::JSON.parse(responseJson)

  if config.save_icon? then
    save_current_icon(@current["weather"][0]["icon"], config.save_icon_dir)
  end

  return @current
end
forecast(config) click to toggle source
# File lib/simple_open_weather_map/weather.rb, line 24
def forecast(config)
  raise "SimpleOpenWeatherMap::Config only!" unless config.is_a?(SimpleOpenWeatherMap::Config)

  responseJson = http_connection(SimpleOpenWeatherMap::PATH_FORECAST).get_contents(forecast_params(config))
  return nil if responseJson.nil? || responseJson.empty?

  ::JSON.parse(responseJson)
end

Protected Instance Methods

current_params(config) click to toggle source
# File lib/simple_open_weather_map/weather.rb, line 39
def current_params(config)
  {
    APPID: config.app_id,
    units: config.unit,
    id:    config.city_id,
  }
end
forecast_params(config) click to toggle source
# File lib/simple_open_weather_map/weather.rb, line 47
def forecast_params(config)
  {
    APPID: config.app_id,
    units: config.unit,
    id:    config.city_id,
    cnt:   config.forecast_days,
  }
end
http_connection(path) click to toggle source
# File lib/simple_open_weather_map/weather.rb, line 35
def http_connection(path)
  SimpleOpenWeatherMap::HttpConnection.new(SimpleOpenWeatherMap::BASE_URL + path)
end
save_current_icon(icon_name, dir) click to toggle source
# File lib/simple_open_weather_map/weather.rb, line 56
def save_current_icon(icon_name, dir)
  return nil if icon_name.nil? || icon_name.empty?

  icon_url = SimpleOpenWeatherMap::ICON_URL_TEMPLATE % [icon_name]
  @icon_path = "%s/%s" % [dir, "weather_icon.png"]

  ::FileUtils.mkdir_p(dir) unless ::FileTest.exist?(dir)

  open(@icon_path, 'wb') do |output|
    open(icon_url) do |data|
      output.write(data.read)
    end
  end

end