Sha256: af2f77c97b7a55fa6dac42bbdef2bc0e661253ac8a2a1848634c5bba689a7eb1

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require "net/https"
require "uri"

module GmapsTz
  class Client
    include TimeZone

    FORMAT = "/json"
    HOST = "maps.googleapis.com"
    BASE_PATH = "/maps/api"

    attr_accessor :key

    def initialize(options = {})
      @key = options[:key]
      @basic_params = { sensor: false }
      @basic_params[:key] = options[:key] if options[:key]
    end

    def get(path, query_params)
      uri = generate_uri(path, query_params)

      response = Net::HTTP.get_response(uri)
      if JSON.parse(response.body)["status"] == "OK"
       response
      else
        raise Error.from_response(uri, response)
      end
    end

    private

    def generate_uri(path, query_params)
      URI::HTTPS.build(
        :host  => HOST,
        :path  => BASE_PATH + path + FORMAT,
        :query => generate_query(@basic_params.merge(query_params))
      )
    end

    def generate_query(query_params)
      query_params.map do |k,v|
        [CGI.escape(k.to_s), "=", CGI.escape(v.to_s)]
      end.map(&:join)
         .join("&")
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gmaps_tz-0.0.1 lib/gmaps_tz/client.rb