Skip to content

Instantly share code, notes, and snippets.

@mnishiguchi
Last active December 14, 2017 15:20
Show Gist options
  • Save mnishiguchi/0ce03ede437bb714a947149348017a15 to your computer and use it in GitHub Desktop.
Save mnishiguchi/0ce03ede437bb714a947149348017a15 to your computer and use it in GitHub Desktop.
Rails google places client

Rails google places client

Gemfile

gem 'google_places'

Make a PORO

class GooglePlacesClient
  def initialize
    @client = GooglePlaces::Client.new ENV["GOOGLE_MAPS_API_KEY"]
  end

  # Returns search results for schools in array of hashes.
  # Radius is in meters.
  def fetch_schools(lat:, lon:, radius: 10_000)
    results = @client.spots(lat, lon, types: [:school, :university], radius: radius)
    format_results_json(results)
  end

  private

  def format_results_json(results)
    results.map do |result|
      {
        name: result.name,
        place_id: result.place_id,
        reference: result.reference,
        types: result.types,
        formatted_address: result.formatted_address,
        lat: result.lat,
        lng: result.lng,
        viewport: result.viewport,
      }
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment