Véase la Documentación
[~/rubytesting]$ cat wheater.rb require 'open-uri' require 'json' require 'psych' city = 'san cristobal de la laguna,spain'.gsub(/\s/,'%20') request = "http://api.openweathermap.org/data/2.5/weather?q=#{city}&units=metric" response = open(request).readlines.join print Psych.dump( JSON.parse(response) )
[~/rubytesting]$ pry [1] pry(main)> require 'open-uri' => true [2] pry(main)> require 'json' => true [3] pry(main)> require 'psych' => true
[4] pry(main)> city = 'san cristobal de la laguna,spain'.gsub(/\s/,'%20') => "san%20cristobal%20de%20la%20laguna,spain"
[5] pry(main)> request = "http://api.openweathermap.org/data/2.5/weather?q=#{city}&units=metric" => "http://api.openweathermap.org/data/2.5/weather?q=san%20cristobal%20de%20la%20laguna,spain&units=metric"
[6] pry(main)> f = open(request) => #<StringIO:0x007fdee29fc4a0 @base_uri= #<URI::HTTP:0x007fdee29fc720 URL:http://api.openweathermap.org/data/2.5/weather?q=san%20cristobal%20de%20la%20laguna,spain&units=metric>, @meta= {"server"=>"nginx", "date"=>"Tue, 07 Oct 2014 10:30:15 GMT", "content-type"=>"application/json; charset=utf-8", "transfer-encoding"=>"chunked", "connection"=>"keep-alive", "x-source"=>"redis", "access-control-allow-origin"=>"*", "access-control-allow-credentials"=>"true", "access-control-allow-methods"=>"GET, POST"}, @metas= {"server"=>["nginx"], "date"=>["Tue, 07 Oct 2014 10:30:15 GMT"], "content-type"=>["application/json; charset=utf-8"], "transfer-encoding"=>["chunked"], "connection"=>["keep-alive"], "x-source"=>["redis"], "access-control-allow-origin"=>["*"], "access-control-allow-credentials"=>["true"], "access-control-allow-methods"=>["GET, POST"]}, @status=["200", "OK"]>
[7] pry(main)> a = f.readlines => ["{\"coord\":{\"lon\":-16.31,\"lat\":28.49},\"sys\":{\"type\":3,\"id\":5518,\"message\":0.1935,\"country\":\"Spain\",\"sunrise\":1412665299,\"sunset\":1412707453},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"base\":\"cmc stations\",\"main\":{\"temp\":24,\"pressure\":1018,\"humidity\":73,\"temp_min\":24,\"temp_max\":24},\"wind\":{\"speed\":4.1,\"deg\":80},\"clouds\":{\"all\":75},\"dt\":1412674200,\"id\":2511401,\"name\":\"San Cristóbal de la Laguna\",\"cod\":200}\n"]
Casiano Rodriguez León 2015-01-07