开发者

Parsing Google Places API with Ruby JSON Gem

Firstly just a big Thanks to everyone on this site, I am using it daily and learning loads! However I have become a little stuck on a problem.

What I am trying to do is to Parse the Google Places API into my own array format. I am using JSON to do this. If I use the below command on the command prompt I can get the right response: (i have changed a few things for privacy)

curl -e http://www.yoursire.com 'https://maps.googleapis.com/maps/api/place/search/json?location=51.50401607915134,-0.10931970886235831&radius=500&types=food&sensor=false&key=AIzaSyCj58SthZHL75a5asdadsNCtbB6fdDe8'

If I then take this idea into ruby I have tried a few different things to grab the data. Firstly I have tried to do the following

require 'rubygems'
require 'open-uri'

result = open("https://maps.googleapis.com/maps/api/place/search/json?location=51.50401607915134,-0.10931970886235831&radius=500&types=food&sensor=false&key=AIzaSyCj58SthZHLsdfsdf5sY0sNCtbB6fdDe8'").read

puts result

I get a hash error that looks like this:

{ "html_attributions" : [ ],"results" : [ ], "status" : "REQUEST_DENIED" }

I am running this locally, which may be an issue, because the of curl -e, but I always get an error

The second option I have been trying is this:

require 'ruby-gems'
require 'open-uri'
require 'json'

result = JSON.parse(open("https://maps.googleapis.com/maps/api/place/details/json?location=51.50401607915134,-0.10931970886235831&radius=500&types=food&sensor=false&key=AIzaSyCj58SthZHL75a5RQd5sY0sNCtbB6fdDe8").read)

puts result

Now I can't even get this one to run as i get the error:

./json.rb:5: undefined method `search' for nil:NilClass (NoMethodError)
    from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /usr/开发者_开发问答lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from places.rb:3

So I cant even seem to get the JSON gem to work? I am trying to run this locally in code, make it work then port the idea over to the web using Rails, but I want to know exactly how it works (learning etc...) I think I know what to do with the data once I have it, but getting it and storing it seems a little problematic at the moment for me. I would appreciate any help any one can give me... even if its a "READ THIS".

Please note I am only using puts result at the end as a test to see if it works!

Thanks

Charlie


The error is coming from the include line. Are you sure you've got the correct JSON gem for your Ruby installation? Try deleting all the code and just require 'json'. If you still get the error (and I think you will) then that's your problem.

For the actual JSON parsing line, I'd recommend doing it like this - it's cleaner and more idiomatic:

url = 'https://maps.googleapis.com/maps/api/place/details/json?location=51.50401607915134,-0.10931970886235831&radius=500&types=food&sensor=false&key=AIzaSyCj58SthZHL75a5RQd5sY0sNCtbB6fdDe8'
result = open(url) do |file|
  JSON.parse(file.read)
end


For future readers: check this super simple client (Faraday based): https://gist.github.com/joost/e149404f645f33ba1939

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜