Skip to content

Instantly share code, notes, and snippets.

@yu-orz
Last active August 29, 2015 14:26
Show Gist options
  • Save yu-orz/d2adf493c0308f247682 to your computer and use it in GitHub Desktop.
Save yu-orz/d2adf493c0308f247682 to your computer and use it in GitHub Desktop.
weather.rb
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require 'json'
require 'pp'
country = ARGV[0] # jp
location = ARGV[1] # Tokyo
if country.nil? or location.nil? then
raise "error"
end
uri = URI.parse("http://api.openweathermap.org/data/2.5/weather?q=#{location},#{country}&units=metric")
json = Net::HTTP.get(uri)
res = JSON.parse(json)
# {"coord"=>{"lon"=>139.69, "lat"=>35.69},
# "weather"=>
# [{"id"=>500, "main"=>"Rain", "description"=>"light rain", "icon"=>"10d"}],
# "base"=>"stations",
# "main"=>
# {"temp"=>32.05,
# "pressure"=>1007,
# "humidity"=>66,
# "temp_min"=>28.89,
# "temp_max"=>34.44},
# "visibility"=>10000,
# "wind"=>{"speed"=>4.6, "deg"=>130},
# "rain"=>{"1h"=>0.76},
# "clouds"=>{"all"=>75},
# "dt"=>1438143429,
# "sys"=>
# {"type"=>1,
# "id"=>7612,
# "message"=>0.0139,
# "country"=>"JP",
# "sunrise"=>1438112785,
# "sunset"=>1438163315},
# "id"=>1850147,
# "name"=>"Tokyo",
# "cod"=>200}
#
temp = res["main"]["temp"]
pres = res["main"]["pressure"]
p temp
p pres
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment