qpowell / google_places

A Ruby wrapper around the Google Places API
MIT License
382 stars 188 forks source link

how to store result sets for future use? #119

Closed josh-m-sharpe closed 5 years ago

josh-m-sharpe commented 5 years ago

I tried to marshal a result set so that I could store it in a DB with Marshal.dump(GOOGLE_PLACES.spots_by_query(my_query)) - but that resulted in: ArgumentError (string contains null byte)

So, after poking around GooglePlaces::Spot.initialize I see that json_result_object isn't stored on the Spot anywhere, which I suppose, if it were available, could be used to reconstitute the object later on, along with the api key. e.g. GooglePlaces::Spot.new(result_json_object_from_database, my_api_key)

I could submit a PR to make the raw results a bit more accessible, but curious to get the maintainers thoughts first.

Thanks!

edwinwills commented 5 years ago

Ah interesting, so you're thinking it would be handy to keep the json_result_object instance as an attribute on the GooglePlaces::Spot class, rather than just assigning out the instance variables for each individual attribute.

This sounds like a handy API method to me - maybe having a method like attributes or to_h on the Spot object, that returns the complete set of attributes. Only question from me would be whether you'd want to return the raw response from Google, or only the attributes that are being set within the spot class - unsure which I'd prefer, but slightly edging towards the attributes that are assigned in the Spot class, so they're whitelisted.

josh-m-sharpe commented 5 years ago

Well, I think if you expose json_result_object then you can reimport it with the already existing method new without having to create something new. So, something like this should work:

spots = client.spots_by_query(query)
recreated_spot = GooglePlaces::Spot.new(spots[0].raw_json, api_key)

Truthfully, that feels a little bit hacky since I'm actually trying to cache the requests to Google in order to minimize api usage... and that feels like something that would be an extension of the gem instead of punting it to the application. Or maybe just an extension of HTTParty. Seems like there's a bunch of unmaintained httparty cache gems dry_ice cachebar

All that say, I think exposing json_result_object is probably sufficient - gonna give that a shot.