Class: MapsApi::Google::UrlGenerator

Inherits:
AddressGeocoder::UrlGenerator show all
Defined in:
lib/maps_api/google/url_generator.rb

Overview

Class for generatoring URLs to call Google Maps API

Since:

  • 0.0.1

Constant Summary collapse

GOOGLE_TITLES =

Google’s attribute names for our address variables

Since:

  • 0.0.1

{
  country: 'country',
  postal_code: 'postal_code',
  city: 'locality',
  state: 'administrative_area'
}.freeze
URL =

The URL of Google Maps’ Geocoding API

Since:

  • 0.0.1

'https://maps.googleapis.com/maps/api/geocode/json?'.freeze
LANGUAGES =

Google accepted language codes

['zh-CN', 'ja', 'es', 'ko', 'ru', 'de', 'fr'].freeze
CYCLE =

The call levels to cycle through

Since:

  • 0.0.1

{ base: [1, 5], no_street: [2, 6], no_city: [3, 7],
no_state: [4] }.freeze

Instance Attribute Summary collapse

Attributes inherited from AddressGeocoder::UrlGenerator

#address, #api_key, #language

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ UrlGenerator

Returns a new instance of UrlGenerator.

Since:

  • 0.0.1



27
28
29
30
# File 'lib/maps_api/google/url_generator.rb', line 27

def initialize(args = {})
  @level = args[:level]
  super args
end

Instance Attribute Details

#levelInteger

Returns the level at which to generate the URL.

Returns:

  • (Integer)

    the level at which to generate the URL



25
26
27
# File 'lib/maps_api/google/url_generator.rb', line 25

def level
  @level
end

Instance Method Details

#generate_urlString

Generates a URL with which to call Google Maps’ Geocoding API

Returns:

  • (String)

    a URL to use in calling a maps API

Since:

  • 0.0.1



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/maps_api/google/url_generator.rb', line 34

def generate_url
  params = prune_address.map { |key, value| add(key, value) }
  params = params.join.tr('\=', ':').chop

  if ([1, 5] & [@level]).any?
    street = hash_to_query('address' => @street) + '&'
  end

  params << "&key=#{@api_key}" unless @api_key.empty?

  language = "&language=#{@language}" if LANGUAGES.include? @language

  "#{URL}#{street}components=#{params}#{language}"
end

#levelsArray<Integer>

Generates layers of calls to make, starting with a base layer that calls all valid fields, and removing a layer each call

Returns:

  • (Array<Integer>)

    a list of calls to determine what values are used in the call to Google Maps’ API

Since:

  • 0.0.1



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/maps_api/google/url_generator.rb', line 53

def levels
  levels  = []
  # Assign base levels unless no street
  levels += CYCLE[:base]      if @address[:street]
  # Assign levels that don't use street if valid city
  levels += CYCLE[:no_street] if @address[:city]
  # Assign levels that don't use street,city if valid state
  levels += CYCLE[:no_city]   if @address[:state]
  if @address[:postal_code]
    # Assign the level that doesn't use street,city,state
    levels += CYCLE[:no_state]
  else
    # Remove all levels that included postal code
    levels -= [5, 6, 7]
  end
  levels.sort
end