weather.rb
· 638 B · Ruby
Bruto
require 'net/http'
require 'uri'
require 'json'
def get_location(city)
url = URI("https://nominatim.openstreetmap.org/search?format=json&q=#{city}&limit=1")
response = Net::HTTP.get(URI(url))
JSON.parse(response)[0]
end
def get_weather(latitude, longitude)
url = URI("https://api.open-meteo.com/v1/forecast?latitude=#{latitude}&longitude=#{longitude}¤t=temperature_2m&hourly=temperature_2m")
response = Net::HTTP.get(URI(url))
JSON.parse(response)["current"]["temperature_2m"]
end
location = get_location(ARGV[0])
latitude = location['lat'].to_s
longitude = location['lon'].to_s
puts get_weather(latitude,longitude)
| 1 | require 'net/http' |
| 2 | require 'uri' |
| 3 | require 'json' |
| 4 | |
| 5 | def get_location(city) |
| 6 | url = URI("https://nominatim.openstreetmap.org/search?format=json&q=#{city}&limit=1") |
| 7 | |
| 8 | response = Net::HTTP.get(URI(url)) |
| 9 | JSON.parse(response)[0] |
| 10 | end |
| 11 | |
| 12 | def get_weather(latitude, longitude) |
| 13 | url = URI("https://api.open-meteo.com/v1/forecast?latitude=#{latitude}&longitude=#{longitude}¤t=temperature_2m&hourly=temperature_2m") |
| 14 | |
| 15 | response = Net::HTTP.get(URI(url)) |
| 16 | JSON.parse(response)["current"]["temperature_2m"] |
| 17 | end |
| 18 | |
| 19 | location = get_location(ARGV[0]) |
| 20 | latitude = location['lat'].to_s |
| 21 | longitude = location['lon'].to_s |
| 22 | puts get_weather(latitude,longitude) |