|
5 | 5 | require 'json'
|
6 | 6 |
|
7 | 7 | 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" |
10 | 10 |
|
11 |
| - # Uses IP to get current city |
12 |
| - begin |
| 11 | + # Uses IP to get current city |
| 12 | + begin |
13 | 13 | url = "http://ip-api.com/json"
|
14 | 14 | response = RestClient.get(url)
|
15 | 15 | parsed = JSON.parse(response)
|
16 | 16 | 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 |
21 | 21 |
|
22 |
| - # Uses city to fetch weather |
23 |
| - begin |
| 22 | + # Uses city to fetch weather |
24 | 23 | url = "https://api.apixu.com/v1/current.json?key=#{api_key}=#{location}"
|
25 | 24 | response = RestClient.get(url)
|
26 | 25 | parsed = JSON.parse(response)
|
27 | 26 |
|
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"] |
39 | 37 |
|
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 |
50 | 70 | end
|
51 | 71 |
|
52 | 72 | weather_search
|
0 commit comments