Sha256: ee8fde810927401d64f94d9d04255d39a7d3bb1187205c52d6814fc9edb9964f

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

require 'httparty'
require 'json'
require 'timeout'
require 'httparty'

module WeatherForecaster
	class WeatherForecastApi
		def initialize			
			@client = "http://api.openweathermap.org/data/2.5/forecast/daily"				
		end

		def query(city, count)
			result = {}
			begin
				status = Timeout::timeout(5){
					result = HTTParty.get(@client, :query => {:q=> city, :cnt => count})				    
		    }
			rescue Timeout::Error => e				
				puts "Taking too long time", :status => 500
				return
			end		
			return WeatherForecastResponse.new(result) if result
		end	
	end	

	class WeatherForecastResponse
		attr_reader :city, :day_temp, :pressure, :country, :list_details, :latitude, :longitude, :status

		def initialize(result)
			detail = result.parsed_response
			@city = detail['city']['name']
			@country = detail['city']['country']
			@latitude = detail['city']['lat']
			@longitude = detail['city']['lon']
      @status = "ok"
			@list_details = [ ]
			detail['list'].each do |list|
			  @list_details << { 
				  :date => DateTime.strptime(list['dt'].to_s,'%s'),
					:day_temp => list['temp']['day'],
					:night_temp => list['temp']['night'],
					:weather_description => list['weather'][0]['description'],
					:pressure => list['pressure'],
					:humidity => list['humidity']
				}
			end		
		end	
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
weather_forecaster-0.1.0 lib/weather_forecaster/weather_forecast_api.rb