Sha256: 0898d0c44707bb9398fdb890647c7f0916c3859c403105a6c6071d131a2b1cbd

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require "gogo_maps/version"
require 'faraday'
require 'json'

module GogoMaps
   # @param Hash opts - to support below Ruby1.9x.
  def self.get(opts={})
    fail 'Should provide either address or latlng' unless opts[:address] || opts[:latlng]

    GoogleMapClient.call(
      { language: :ja, sensor: false }.merge(opts),
      opts[:address] ? :to_latlng : :to_address
    )
  end

  class GoogleMapClient
    ENDPOINT = 'http://maps.googleapis.com'
    @@_conn ||= Faraday.new(url: ENDPOINT) do |faraday|
      faraday.request  :url_encoded
      faraday.adapter  Faraday.default_adapter
    end

    def self.call(params, sym)
      response = @@_conn.get do |req|
        req.url '/maps/api/geocode/json'
        req.params = params
      end

      unless location = JSON.parse(response.body)['results'][0]
        fail 'Something wrong with Google API or your params'
      end

      case sym
        when :to_latlng  then location['geometry']['location']
        when :to_address then location['formatted_address']
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gogo_maps-0.0.2 lib/gogo_maps.rb