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

Commit 6e9f3ba

Browse files
committed
added weather script
1 parent 472e498 commit 6e9f3ba

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

weather/weather.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env ruby
2+
# CLI app that brings in current location weather
3+
4+
require 'rest-client'
5+
require 'json'
6+
7+
def weather_search
8+
9+
api_key = "12345678"
10+
11+
# Uses IP to get current city
12+
url = "http://ip-api.com/json"
13+
response = RestClient.get(url)
14+
parsed = JSON.parse(response)
15+
location = parsed["city"]
16+
17+
# Uses city to fetch weather
18+
url = "https://api.apixu.com/v1/current.json?key=#{api_key}=#{location}"
19+
response = RestClient.get(url)
20+
parsed = JSON.parse(response)
21+
22+
# Assigning values to variables
23+
location_name = parsed["location"]["name"]
24+
temp = parsed["current"]["temp_f"]
25+
wind_speed = parsed["current"]["wind_mph"]
26+
humidity = parsed["current"]["humidity"]
27+
feels_like = parsed["current"]["feelslike_f"]
28+
visability = parsed["current"]["vis_miles"]
29+
30+
puts ""
31+
puts "======================"
32+
puts "| City: #{location_name}"
33+
puts "| Temp: #{temp}°"
34+
puts "| Feels Like: #{feels_like}°"
35+
puts "| Humidity: #{humidity}%"
36+
puts "| Wind Speed: #{wind_speed} mph"
37+
puts "| Visability: #{visability} mi"
38+
puts "======================"
39+
puts ""
40+
end
41+
42+
weather_search

0 commit comments

Comments
 (0)