Class: MapsApi::Google::UrlGenerator
- Inherits:
-
AddressGeocoder::UrlGenerator
- Object
- AddressGeocoder::UrlGenerator
- MapsApi::Google::UrlGenerator
- Defined in:
- lib/maps_api/google/url_generator.rb
Overview
Class for generatoring URLs to call Google Maps API
Constant Summary collapse
- GOOGLE_TITLES =
Google’s attribute names for our address variables
{ country: 'country', postal_code: 'postal_code', city: 'locality', state: 'administrative_area' }.freeze
- URL =
The URL of Google Maps’ Geocoding API
'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
{ base: [1, 5], no_street: [2, 6], no_city: [3, 7], no_state: [4] }.freeze
Instance Attribute Summary collapse
-
#level ⇒ Integer
The level at which to generate the URL.
Attributes inherited from AddressGeocoder::UrlGenerator
Instance Method Summary collapse
-
#generate_url ⇒ String
Generates a URL with which to call Google Maps’ Geocoding API.
-
#initialize(args = {}) ⇒ UrlGenerator
constructor
A new instance of UrlGenerator.
-
#levels ⇒ Array<Integer>
Generates layers of calls to make, starting with a base layer that calls all valid fields, and removing a layer each call.
Constructor Details
#initialize(args = {}) ⇒ UrlGenerator
Returns a new instance of UrlGenerator.
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
#level ⇒ Integer
Returns 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_url ⇒ String
Generates a URL with which to call Google Maps’ Geocoding API
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 |
#levels ⇒ Array<Integer>
Generates layers of calls to make, starting with a base layer that calls all valid fields, and removing a layer each call
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 |