Dernière activité 1724760187

weather.rb Brut
1require 'net/http'
2require 'uri'
3require 'json'
4
5def 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]
10end
11
12def get_weather(latitude, longitude)
13 url = URI("https://api.open-meteo.com/v1/forecast?latitude=#{latitude}&longitude=#{longitude}&current=temperature_2m&hourly=temperature_2m")
14
15 response = Net::HTTP.get(URI(url))
16 JSON.parse(response)["current"]["temperature_2m"]
17end
18
19location = get_location(ARGV[0])
20latitude = location['lat'].to_s
21longitude = location['lon'].to_s
22puts get_weather(latitude,longitude)