Class: Weatherbot::API

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

Constant Summary collapse

@@locations =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Helper

degToCompass, toCelsius

Constructor Details

#initializeAPI

Returns a new instance of API.



7
8
9
10
# File 'lib/api.rb', line 7

def initialize
  @location = location
  @@locations << self
end

Instance Attribute Details

#cloudinessObject

Returns the value of attribute cloudiness.



3
4
5
# File 'lib/api.rb', line 3

def cloudiness
  @cloudiness
end

#cloudiness24Object

Returns the value of attribute cloudiness24.



3
4
5
# File 'lib/api.rb', line 3

def cloudiness24
  @cloudiness24
end

#cloudiness48Object

Returns the value of attribute cloudiness48.



3
4
5
# File 'lib/api.rb', line 3

def cloudiness48
  @cloudiness48
end

#cloudiness72Object

Returns the value of attribute cloudiness72.



3
4
5
# File 'lib/api.rb', line 3

def cloudiness72
  @cloudiness72
end

#conditionObject

Returns the value of attribute condition.



3
4
5
# File 'lib/api.rb', line 3

def condition
  @condition
end

#condition24Object

Returns the value of attribute condition24.



3
4
5
# File 'lib/api.rb', line 3

def condition24
  @condition24
end

#condition48Object

Returns the value of attribute condition48.



3
4
5
# File 'lib/api.rb', line 3

def condition48
  @condition48
end

#condition72Object

Returns the value of attribute condition72.



3
4
5
# File 'lib/api.rb', line 3

def condition72
  @condition72
end

#coordinatesObject

Returns the value of attribute coordinates.



3
4
5
# File 'lib/api.rb', line 3

def coordinates
  @coordinates
end

#countryObject

Returns the value of attribute country.



3
4
5
# File 'lib/api.rb', line 3

def country
  @country
end

#google_mapsObject

Returns the value of attribute google_maps.



3
4
5
# File 'lib/api.rb', line 3

def google_maps
  @google_maps
end

Returns the value of attribute google_maps_link.



3
4
5
# File 'lib/api.rb', line 3

def google_maps_link
  @google_maps_link
end

#hr24_dtObject

Returns the value of attribute hr24_dt.



3
4
5
# File 'lib/api.rb', line 3

def hr24_dt
  @hr24_dt
end

#hr48_dtObject

Returns the value of attribute hr48_dt.



3
4
5
# File 'lib/api.rb', line 3

def hr48_dt
  @hr48_dt
end

#hr72_dtObject

Returns the value of attribute hr72_dt.



3
4
5
# File 'lib/api.rb', line 3

def hr72_dt
  @hr72_dt
end

#humidityObject

Returns the value of attribute humidity.



3
4
5
# File 'lib/api.rb', line 3

def humidity
  @humidity
end

#humidity24Object

Returns the value of attribute humidity24.



3
4
5
# File 'lib/api.rb', line 3

def humidity24
  @humidity24
end

#humidity48Object

Returns the value of attribute humidity48.



3
4
5
# File 'lib/api.rb', line 3

def humidity48
  @humidity48
end

#humidity72Object

Returns the value of attribute humidity72.



3
4
5
# File 'lib/api.rb', line 3

def humidity72
  @humidity72
end

#locationObject

Returns the value of attribute location.



3
4
5
# File 'lib/api.rb', line 3

def location
  @location
end

#location_nameObject

Returns the value of attribute location_name.



3
4
5
# File 'lib/api.rb', line 3

def location_name
  @location_name
end

#report_timeObject

Returns the value of attribute report_time.



3
4
5
# File 'lib/api.rb', line 3

def report_time
  @report_time
end

#response_codeObject

Returns the value of attribute response_code.



3
4
5
# File 'lib/api.rb', line 3

def response_code
  @response_code
end

#sunriseObject

Returns the value of attribute sunrise.



3
4
5
# File 'lib/api.rb', line 3

def sunrise
  @sunrise
end

#sunsetObject

Returns the value of attribute sunset.



3
4
5
# File 'lib/api.rb', line 3

def sunset
  @sunset
end

#temp24Object

Returns the value of attribute temp24.



3
4
5
# File 'lib/api.rb', line 3

def temp24
  @temp24
end

#temp48Object

Returns the value of attribute temp48.



3
4
5
# File 'lib/api.rb', line 3

def temp48
  @temp48
end

#temp72Object

Returns the value of attribute temp72.



3
4
5
# File 'lib/api.rb', line 3

def temp72
  @temp72
end

#temp_avgObject

Returns the value of attribute temp_avg.



3
4
5
# File 'lib/api.rb', line 3

def temp_avg
  @temp_avg
end

#temp_celsiusObject

Returns the value of attribute temp_celsius.



3
4
5
# File 'lib/api.rb', line 3

def temp_celsius
  @temp_celsius
end

#wind_directionObject

Returns the value of attribute wind_direction.



3
4
5
# File 'lib/api.rb', line 3

def wind_direction
  @wind_direction
end

#wind_direction24Object

Returns the value of attribute wind_direction24.



3
4
5
# File 'lib/api.rb', line 3

def wind_direction24
  @wind_direction24
end

#wind_direction48Object

Returns the value of attribute wind_direction48.



3
4
5
# File 'lib/api.rb', line 3

def wind_direction48
  @wind_direction48
end

#wind_direction72Object

Returns the value of attribute wind_direction72.



3
4
5
# File 'lib/api.rb', line 3

def wind_direction72
  @wind_direction72
end

#wind_speedObject

Returns the value of attribute wind_speed.



3
4
5
# File 'lib/api.rb', line 3

def wind_speed
  @wind_speed
end

#wind_speed24Object

Returns the value of attribute wind_speed24.



3
4
5
# File 'lib/api.rb', line 3

def wind_speed24
  @wind_speed24
end

#wind_speed48Object

Returns the value of attribute wind_speed48.



3
4
5
# File 'lib/api.rb', line 3

def wind_speed48
  @wind_speed48
end

#wind_speed72Object

Returns the value of attribute wind_speed72.



3
4
5
# File 'lib/api.rb', line 3

def wind_speed72
  @wind_speed72
end

Class Method Details

.current_weather(location) ⇒ Object

Takes user input to enter into URL query & gets current weather conditions in imperial units



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/api.rb', line 17

def self.current_weather(location)
  # query sample: 'https://api.openweathermap.org/data/2.5/weather?q=new+york&appid=3207703ee5d0d14e6b6a53d10071018f&units=imperial'
  response = HTTParty.get("https://api.openweathermap.org/data/2.5/weather?q=#{location}&appid=3207703ee5d0d14e6b6a53d10071018f&units=imperial")
  parsed = response.parsed_response
  @current = self.new
  @current.response_code = parsed["cod"]

  # Check for invalid entry
  if @current.response_code === "404"
    puts "\n\nInvalid location, please enter a valid location.\n\n"
    return
  else
    # Assign attributes to current weather object
  @current.coordinates = parsed["coord"].values.reverse.join(", ")
  @current.location_name = parsed["name"]
  @current.report_time = Time.at(parsed["dt"])
  @current.temp_avg = parsed["main"]["temp"]
  @current.condition = parsed["weather"].first["description"]
  @current.cloudiness = parsed["clouds"]["all"]
  # @current.pressure = parsed["main"]["pressure"]
  @current.humidity = parsed["main"]["humidity"]
  @current.wind_speed = parsed["wind"]["speed"]
  @current.sunrise = Time.at(parsed["sys"]["sunrise"])
  @current.sunset = Time.at(parsed["sys"]["sunset"])
  @current.wind_direction = degToCompass(parsed["wind"]["deg"])
  @current.temp_celsius = toCelsius(parsed["main"]["temp"])

  # Open query in browser to Google Maps
  @current.google_maps = "https://www.google.com/maps/place/#{@current.coordinates.gsub(" ", "")}"
  @google_maps_link = @current.google_maps

  # Check for odd locations with no country key
  if parsed.fetch("sys").has_key?("country")
    @current.country = parsed["sys"]["country"]
  else
    @current.country = nil
  end


  end

  @current
end

.forecast(location) ⇒ Object

Takes user input to enter into URL query for 3 day forecast in imperial units



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/api.rb', line 63

def self.forecast(location)
  # query sample: https://api.openweathermap.org/data/2.5/forecast?q=new+york&appid=3207703ee5d0d14e6b6a53d10071018f&units=imperial
  response = HTTParty.get("https://api.openweathermap.org/data/2.5/forecast?q=#{location}&appid=3207703ee5d0d14e6b6a53d10071018f&units=imperial")
  parsed = response.parsed_response
  forecast = self.new

  forecast.location_name = @current.location_name
  # 24/48/72hr date
  forecast.hr24_dt = parsed["list"][8]["dt_txt"]
  forecast.hr48_dt = parsed["list"][16]["dt_txt"]
  forecast.hr72_dt = parsed["list"][24]["dt_txt"]
  # 24/48/72hr temp
  forecast.temp24 = parsed["list"][8]["main"]["temp"]
  forecast.temp48 = parsed["list"][16]["main"]["temp"]
  forecast.temp72 = parsed["list"][24]["main"]["temp"]
  # 24/48/72hr condition
  forecast.condition24 = parsed["list"][8]["weather"][0]["description"]
  forecast.condition48 = parsed["list"][16]["weather"][0]["description"]
  forecast.condition72 = parsed["list"][24]["weather"][0]["description"]
  # 24/48/72hr cloudiness
  forecast.cloudiness24 = parsed["list"][8]["clouds"]["all"]
  forecast.cloudiness48 = parsed["list"][16]["clouds"]["all"]
  forecast.cloudiness72 = parsed["list"][24]["clouds"]["all"]
  # 24/48/72hr humidity
  forecast.humidity24 = parsed["list"][8]["main"]["humidity"]
  forecast.humidity48 = parsed["list"][16]["main"]["humidity"]
  forecast.humidity72 = parsed["list"][24]["main"]["humidity"]
  # 24/48/72hr wind speed & direction
  forecast.wind_speed24 = parsed["list"][8]["wind"]["speed"]
  forecast.wind_speed48 = parsed["list"][16]["wind"]["speed"]
  forecast.wind_speed72 = parsed["list"][24]["wind"]["speed"]
  forecast.wind_direction24 = degToCompass(parsed["list"][8]["wind"]["deg"])
  forecast.wind_direction48 = degToCompass(parsed["list"][16]["wind"]["deg"])
  forecast.wind_direction72 = degToCompass(parsed["list"][24]["wind"]["deg"])

  # Open query in browser to Google Maps
  forecast.google_maps = "https://www.google.com/maps/place/#{@current.coordinates.gsub(" ", "")}"
  @google_maps_link = @current.google_maps

  # Output 3 day forecast
  puts "\n-------------------------------\n"
  puts "\n\nIn 24 Hours:"
  puts "\nReport Time:      #{forecast.hr24_dt}"
  puts "Location:         #{forecast.location_name},  #{@current.country}"
  puts "Google Maps:      #{forecast.google_maps}"
  puts "\nTemperature:      #{forecast.temp24}ºF / #{toCelsius(forecast.temp24)}ºC"
  puts "Condition:        #{forecast.condition24.capitalize}"
  puts "Cloudiness:       #{forecast.cloudiness24}%"
  puts "\nHumidity:         #{forecast.humidity24}%"
  puts "Wind Speed:       #{forecast.wind_speed24} mph"
  puts "Wind Direction:   #{forecast.wind_direction24}"

  puts "\n-------------------------------\n"
  puts "\n\nIn 48 Hours:"
  puts "\nReport Time:      #{forecast.hr48_dt}"
  puts "Location:         #{forecast.location_name},  #{@current.country}"
  puts "Google Maps:      #{forecast.google_maps}"
  puts "\nTemperature:      #{forecast.temp48}ºF / #{toCelsius(forecast.temp48)}ºC"
  puts "Condition:        #{forecast.condition48.capitalize}"
  puts "Cloudiness:       #{forecast.cloudiness48}%"
  puts "\nHumidity:         #{forecast.humidity48}%"
  puts "Wind Speed:       #{forecast.wind_speed48} mph"
  puts "Wind Direction:   #{forecast.wind_direction48}"

  puts "\n-------------------------------\n"
  puts "\n\nIn 72 Hours:"
  puts "\nReport Time:      #{forecast.hr72_dt}"
  puts "Location:         #{forecast.location_name}, #{@current.country}"
  puts "Google Maps:      #{forecast.google_maps}"
  puts "\nTemperature:      #{forecast.temp72}ºF / #{toCelsius(forecast.temp72)}ºC"
  puts "Condition:        #{forecast.condition72.capitalize}"
  puts "Cloudiness:       #{forecast.cloudiness72}%"
  puts "\nHumidity:         #{forecast.humidity72}%"
  puts "Wind Speed:       #{forecast.wind_speed72} mph"
  puts "Wind Direction:   #{forecast.wind_direction72}"

end

.locationsObject



12
13
14
# File 'lib/api.rb', line 12

def self.locations
  @@locations
end

Method to open link in Google Maps depending on OS



142
143
144
145
146
147
148
149
150
151
# File 'lib/api.rb', line 142

def self.open_link
  link = @google_maps_link
  if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
    system "start #{link}"
  elsif RbConfig::CONFIG['host_os'] =~ /darwin/
    system "open #{link}"
  elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
    system "xdg-open #{link}"
  end
end