Diogo Schneider
Diogo Schneider

Reputation: 349

Google Places ActiveResource

I'm trying to write an ActiveResources model to interact with the Google Places API - https://developers.google.com/places/web-service/search#PlaceSearchRequests

I see the API requires a key and some parameters in order to fetch results. So my question is: how do I properly add the API key to the request, because right now I have no idea if it is actually going through and also how do I set up the model in order to pass the parameters when later I want to do something like:

GooglePlace.where(query: 'whatever')

What I have so far is just the default boilerplate for an ActiveResource, with no clue as how to add the missing information:

class GooglePlace < ActiveResource::Base
  self.site = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=pool'
  @key = API_KEY
end

And my index action method in the controller looks like this:

def index
  @users = GooglePlace.where(query: 'pool')
end

In the console, all I get is this:

Started GET "/google_places" for ::1 at 2016-02-27 16:41:11 -0300
Processing by GooglePlacesController#index as HTML
  Rendered google_places/index.html.erb within layouts/application (61.0ms)
Completed 500 Internal Server Error in 2027ms (ActiveRecord: 0.0ms)

Any help will be much appreciated. Thank you for your time!

Upvotes: 3

Views: 86

Answers (2)

Diogo Schneider
Diogo Schneider

Reputation: 349

According to this piece of code, one must override a lot of ActiveResource's methods in order to make it even work with a plain non-Rails REST API. I sincerely don't think it's worth it, so maybe ActiveResource is really for Rails-to-Rails communication. Thanks to everybody who tried to help me.

Upvotes: 1

Baronz
Baronz

Reputation: 677

It sounds like you're just asking: Where do I get my own API key?

https://console.developers.google.com/home/dashboard

From there, in the upper right, choose "Select a Project" > "Start a project" and you can choose which Google APIs you want to use, and generate your key for them.

Note that the key is for YOUR application only. Each application should have it's own unique key.

Upvotes: 0

Related Questions