Kong / unirest-ruby

Unirest in Ruby: Simplified, lightweight HTTP client library.
http://unirest.io/ruby
MIT License
364 stars 81 forks source link

Response body / parsing to a ruby Hash #31

Open dbeslic opened 9 years ago

dbeslic commented 9 years ago

Hi. When I pull the API data into a Ruby on Rails web app, I think it should be a Hash object, but for some reason, I'm unable to run any Hash methods on it.

Example for Mexico is below. Despite the square brackets, It doesn't seem to be an Array either. I tried running both Hash methods and Array methods on the parsed body, and simply got a Nil Class error: undefined method `[]' for nil:NilClass

Any idea how I might be able to resolve? I would like to get the parsed response body, and then run several Hash methods on it in my rails program.Thanks!

[{ "name"=>"Mexico", "capital"=>"Mexico City", "altSpellings"=>["MX", "Mexicanos", "United Mexican States", "Estados Unidos Mexicanos"], "relevance"=>"1.5", "region"=>"Americas", "subregion"=>"Central America", "translations"=>{"de"=>"Mexiko", "es"=>"México", "fr"=>"Mexique", "ja"=>"メキシコ", "it"=>"Messico"}, "population"=>121740000, "latlng"=>[23.0, -102.0], "demonym"=>"Mexican", "area"=>1964375.0, "gini"=>47.0, "timezones"=>["UTC−08:00", "UTC−07:00", "UTC−06:00"], "borders"=>["BLZ", "GTM", "USA"], "nativeName"=>"México", "callingCodes"=>["52"], "topLevelDomain"=>[".mx"], "alpha2Code"=>"MX", "alpha3Code"=>"MEX", "currencies"=>["MXN"], "languages"=>["es"] }]