Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Commit e1fb17e

Browse files
committed
added support for metric weather output
1 parent 4f78976 commit e1fb17e

File tree

1 file changed

+51
-31
lines changed

1 file changed

+51
-31
lines changed

weather/weather.rb

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,68 @@
55
require 'json'
66

77
def weather_search
8-
# This key is functional, please don't abuse it
9-
api_key = "6510b92495fd472ca30155709172803&q"
8+
# This key is functional, please don't abuse it
9+
api_key = "6510b92495fd472ca30155709172803&q"
1010

11-
# Uses IP to get current city
12-
begin
11+
# Uses IP to get current city
12+
begin
1313
url = "http://ip-api.com/json"
1414
response = RestClient.get(url)
1515
parsed = JSON.parse(response)
1616
location = parsed["city"]
17-
rescue
18-
puts "No IP Response"
19-
exit(1)
20-
end
17+
rescue
18+
puts "No IP Address"
19+
exit(1)
20+
end
2121

22-
# Uses city to fetch weather
23-
begin
22+
# Uses city to fetch weather
2423
url = "https://api.apixu.com/v1/current.json?key=#{api_key}=#{location}"
2524
response = RestClient.get(url)
2625
parsed = JSON.parse(response)
2726

28-
# Assigning values to variables
29-
location_name = parsed["location"]["name"]
30-
temp = parsed["current"]["temp_f"]
31-
wind_speed = parsed["current"]["wind_mph"]
32-
humidity = parsed["current"]["humidity"]
33-
feels_like = parsed["current"]["feelslike_f"]
34-
visability = parsed["current"]["vis_miles"]
35-
rescue
36-
puts "No Weather API Response"
37-
exit(1)
38-
end
27+
country = parsed["location"]["country"]
28+
29+
if country == "United States of America"
30+
# Assigning values to variables
31+
location_name = parsed["location"]["name"]
32+
temp = parsed["current"]["temp_f"]
33+
wind_speed = parsed["current"]["wind_mph"]
34+
humidity = parsed["current"]["humidity"]
35+
feels_like = parsed["current"]["feelslike_f"]
36+
visability = parsed["current"]["vis_miles"]
3937

40-
puts
41-
puts "======================"
42-
puts "| City: #{location_name}"
43-
puts "| Temp: #{temp}°"
44-
puts "| Feels Like: #{feels_like}°"
45-
puts "| Humidity: #{humidity}%"
46-
puts "| Wind Speed: #{wind_speed} mph"
47-
puts "| Visability: #{visability} mi"
48-
puts "======================"
49-
puts
38+
# Output for United States
39+
puts
40+
puts "======================"
41+
puts "| City: #{location_name}"
42+
puts "| Temp: #{temp}°F"
43+
puts "| Feels Like: #{feels_like}°F"
44+
puts "| Humidity: #{humidity}%"
45+
puts "| Wind Speed: #{wind_speed} mph"
46+
puts "| Visability: #{visability} mi"
47+
puts "======================"
48+
puts
49+
else
50+
# Assigning values to variables
51+
location_name = parsed["location"]["name"]
52+
temp = parsed["current"]["temp_c"]
53+
wind_speed = parsed["current"]["wind_kph"]
54+
humidity = parsed["current"]["humidity"]
55+
feels_like = parsed["current"]["feelslike_c"]
56+
visability = parsed["current"]["vis_km"]
57+
58+
# Output for Metric countries
59+
puts
60+
puts "======================"
61+
puts "| City: #{location_name}"
62+
puts "| Temp: #{temp}°C"
63+
puts "| Feels Like: #{feels_like}°C"
64+
puts "| Humidity: #{humidity}%"
65+
puts "| Wind Speed: #{wind_speed} kph"
66+
puts "| Visability: #{visability} km"
67+
puts "======================"
68+
puts
69+
end
5070
end
5171

5272
weather_search

0 commit comments

Comments
 (0)