|
1 | 1 | #!/usr/bin/env ruby
|
2 | 2 | # CLI app that brings in current location weather.
|
3 | 3 |
|
4 |
| -require 'rest-client' |
5 | 4 | require 'json'
|
| 5 | +require 'uri' |
| 6 | +require 'net/http' |
6 | 7 |
|
7 |
| -def weather_search |
8 |
| - # This key is functional, please don't abuse it |
| 8 | +def weather |
9 | 9 | api_key = ENV['APIXU_API_KEY'] || "6510b92495fd472ca30155709172803&q"
|
10 | 10 |
|
11 |
| - # Uses IP to get current city |
12 |
| - begin |
13 |
| - url = "http://ip-api.com/json" |
14 |
| - response = RestClient.get(url) |
15 |
| - parsed = JSON.parse(response) |
16 |
| - location = ARGV || parsed["city"] |
17 |
| - rescue RestClient::BadRequest |
18 |
| - puts "No IP address found" |
19 |
| - exit(1) |
| 11 | + ip = get_ip |
| 12 | + city = ARGV[0] || ip["city"] |
| 13 | + state = ARGV[1] || ip["region"] |
| 14 | + country = ip["countryCode"] |
| 15 | + |
| 16 | + weather = get_weather(api_key, city, state) |
| 17 | + |
| 18 | + if country == "US" |
| 19 | + us_printer(weather) |
| 20 | + else |
| 21 | + intl_printer(weather) |
20 | 22 | end
|
| 23 | +end |
21 | 24 |
|
22 |
| - # Uses city to fetch weather |
23 |
| - begin |
24 |
| - url = "https://api.apixu.com/v1/current.json?key=#{api_key}=#{location}" |
25 |
| - response = RestClient.get(url) |
26 |
| - rescue RestClient::BadRequest |
27 |
| - puts "Location not found" |
28 |
| - exit(1) |
29 |
| - end |
30 |
| - parsed = JSON.parse(response) |
| 25 | +def get_ip |
| 26 | + uri = URI("http://ip-api.com/json") |
| 27 | + response = Net::HTTP.get(uri) |
| 28 | + JSON.parse(response) |
| 29 | +end |
31 | 30 |
|
32 |
| - country = parsed["location"]["country"] |
| 31 | +def get_weather(api_key, city, state) |
| 32 | + uri = URI("https://api.apixu.com/v1/current.json?key=#{api_key}=#{city}+#{state}") |
| 33 | + response = Net::HTTP.get(uri) |
| 34 | + JSON.parse(response) |
| 35 | +end |
| 36 | + |
| 37 | +def us_printer(parsed) |
| 38 | + # Printer for US users |
| 39 | + location_name = parsed["location"]["name"] |
| 40 | + temp = parsed["current"]["temp_f"] |
| 41 | + wind_speed = parsed["current"]["wind_mph"] |
| 42 | + humidity = parsed["current"]["humidity"] |
| 43 | + feels_like = parsed["current"]["feelslike_f"] |
| 44 | + visibility = parsed["current"]["vis_miles"] |
33 | 45 |
|
34 |
| - if country == "United States of America" |
35 |
| - # Assigning values to variables |
36 |
| - location_name = parsed["location"]["name"] |
37 |
| - temp = parsed["current"]["temp_f"] |
38 |
| - wind_speed = parsed["current"]["wind_mph"] |
39 |
| - humidity = parsed["current"]["humidity"] |
40 |
| - feels_like = parsed["current"]["feelslike_f"] |
41 |
| - visibility = parsed["current"]["vis_miles"] |
| 46 | + puts |
| 47 | + puts "======================" |
| 48 | + puts "City: #{location_name}" |
| 49 | + puts "Temp: #{temp}°F" |
| 50 | + puts "Feels Like: #{feels_like}°F" |
| 51 | + puts "Humidity: #{humidity}%" |
| 52 | + puts "Wind Speed: #{wind_speed} mph" |
| 53 | + puts "Visibility: #{visibility} mi" |
| 54 | + puts "======================" |
| 55 | + puts |
| 56 | +end |
42 | 57 |
|
43 |
| - # Output for United States |
44 |
| - puts |
45 |
| - puts "======================" |
46 |
| - puts "| City: #{location_name}" |
47 |
| - puts "| Temp: #{temp}°F" |
48 |
| - puts "| Feels Like: #{feels_like}°F" |
49 |
| - puts "| Humidity: #{humidity}%" |
50 |
| - puts "| Wind Speed: #{wind_speed} mph" |
51 |
| - puts "| Visibility: #{visibility} mi" |
52 |
| - puts "======================" |
53 |
| - puts |
54 |
| - else |
55 |
| - # Assigning values to variables |
56 |
| - location_name = parsed["location"]["name"] |
57 |
| - temp = parsed["current"]["temp_c"] |
58 |
| - wind_speed = parsed["current"]["wind_kph"] |
59 |
| - humidity = parsed["current"]["humidity"] |
60 |
| - feels_like = parsed["current"]["feelslike_c"] |
61 |
| - visibility = parsed["current"]["vis_km"] |
| 58 | +def intl_printer(parsed) |
| 59 | + # Printer for metric users |
| 60 | + location_name = parsed["location"]["name"] |
| 61 | + temp = parsed["current"]["temp_c"] |
| 62 | + wind_speed = parsed["current"]["wind_kph"] |
| 63 | + humidity = parsed["current"]["humidity"] |
| 64 | + feels_like = parsed["current"]["feelslike_c"] |
| 65 | + visibility = parsed["current"]["vis_km"] |
62 | 66 |
|
63 |
| - # Output for Metric countries |
64 |
| - puts |
65 |
| - puts "======================" |
66 |
| - puts "| City: #{location_name}" |
67 |
| - puts "| Temp: #{temp}°C" |
68 |
| - puts "| Feels Like: #{feels_like}°C" |
69 |
| - puts "| Humidity: #{humidity}%" |
70 |
| - puts "| Wind Speed: #{wind_speed} kph" |
71 |
| - puts "| Visibility: #{visibility} km" |
72 |
| - puts "======================" |
73 |
| - puts |
74 |
| - end |
| 67 | + puts |
| 68 | + puts "======================" |
| 69 | + puts "City: #{location_name}" |
| 70 | + puts "Temp: #{temp}°C" |
| 71 | + puts "Feels Like: #{feels_like}°C" |
| 72 | + puts "Humidity: #{humidity}%" |
| 73 | + puts "Wind Speed: #{wind_speed} kph" |
| 74 | + puts "Visibility: #{visibility} km" |
| 75 | + puts "======================" |
| 76 | + puts |
75 | 77 | end
|
76 | 78 |
|
77 |
| -weather_search |
| 79 | +weather |
0 commit comments