Class: WeatherAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/getweather.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zip) ⇒ WeatherAPI

Returns a new instance of WeatherAPI.



5
6
7
8
9
10
# File 'lib/getweather.rb', line 5

def initialize(zip)
  @zip = zip.to_i
  @url = "http://api.openweathermap.org/data/2.5/weather?zip=#{@zip},us&units=imperial&APPID=#{WEATHER_KEY}"
  @info = {}
  apicall
end

Instance Attribute Details

#infoObject

Returns the value of attribute info.



4
5
6
# File 'lib/getweather.rb', line 4

def info
  @info
end

#zipObject

Returns the value of attribute zip.



4
5
6
# File 'lib/getweather.rb', line 4

def zip
  @zip
end

Instance Method Details

#apicallObject



12
13
14
15
16
17
# File 'lib/getweather.rb', line 12

def apicall
  openweather_json = RestClient.get(@url)
  @parsed = JSON.parse(openweather_json)
  rescue
    "bad link"
end

#returnhashObject



19
20
21
22
23
24
# File 'lib/getweather.rb', line 19

def returnhash
  @info[:city] = @parsed["name"]
  @info[:id] = @parsed["weather"][0]["id"]
  @info[:temp] = @parsed["main"]["temp"]
  @info
end