class MapsApi::Google::UrlGenerator
Class for generatoring URLs to call Google
Maps API
Constants
- CYCLE
The call levels to cycle through
- GOOGLE_TITLES
Google’s attribute names for our address variables
- LANGUAGES
Google
accepted language codes @see developers.google.com/maps/faq#languagesupport- URL
Attributes
@!attribute level @return [Integer] the level at which to generate the URL
Public Class Methods
AddressGeocoder::UrlGenerator::new
# File lib/maps_api/google/url_generator.rb, line 27 def initialize(args = {}) @level = args[:level] super args end
Public Instance Methods
Generates a URL
with which to call Google
Maps’ Geocoding API @return (see AddressGeocoder::UrlGenerator#generate_url
)
# 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
Generates layers of calls to make, starting with a base layer that calls all valid fields, and removing a layer each call @return [Array<Integer>] a list of calls to determine what values are
used in the call to Google Maps' API
# 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
Private Instance Methods
Parses a key and value from a hash into a query @return [String] a query to be used in the URL
# File lib/maps_api/google/url_generator.rb, line 88 def add(key, value) str = hash_to_query(GOOGLE_TITLES[key] => value) "#{str}|" end
Removes attributes from the address that don’t fit with the level @return [Hash] an address object to add to the call
# File lib/maps_api/google/url_generator.rb, line 75 def prune_address address = (@address.select { |_k, v| v }).to_h address[:country] = address[:country][:alpha2] @street = address.delete(:street) address.delete(:postal_code) if @level > 4 address.delete(:city) if ([3, 4, 7] & [@level]).any? address.delete(:state) if @level == 4 address end