Browse Source

Adding cli_weather app

dyurk 1 year ago
parent
commit
e0591167ef
1 changed files with 26 additions and 0 deletions
  1. 26 0
      cli_weather.rb

+ 26 - 0
cli_weather.rb

@@ -0,0 +1,26 @@
1
+#!/usr/bin/env ruby
2
+# CLI WEATHER by DYURK
3
+# Simple script to interact with OpenWeatherMap's API
4
+require 'rubygems'
5
+require 'net/http'
6
+require 'json'
7
+
8
+#OpenWeatherMap API KEY
9
+api_key = "1bb6937ed4a086a1464db0ab005f1cd6"
10
+#Zip code from the first argument
11
+zip_code = ARGV[0]
12
+if ARGV.empty?
13
+  puts "Please enter the Zip code for the city you wish to get weather information"
14
+  zip_code = gets.chomp
15
+end
16
+#URL RESTapi endpoint
17
+api = "http://api.openweathermap.org/data/2.5/weather?zip=#{zip_code},us&APPID=#{api_key}&units=imperial"
18
+begin
19
+  get_call = Net::HTTP.get_response(URI.parse(api))
20
+rescue RuntimeError => e
21
+  puts e
22
+end
23
+#Parse JSON 
24
+data = JSON.parse(get_call.body)
25
+#print out weather information
26
+puts "#{data["name"]} Weather | Current Temp: #{data["main"]["temp"]} °F | #{data["weather"][0]["description"]}"