Module: GoogleMapsService::Convert
- Defined in:
- lib/google_maps_service/convert.rb
Overview
Converts Ruby types to string representations suitable for Maps API server.
Class Method Summary collapse
-
.as_list(arg) ⇒ Array
Coerces arg into a list.
-
.bounds(arg) ⇒ String
Converts a lat/lon bounds to a comma- and pipe-separated string.
-
.components(arg) ⇒ String
Converts a dict of components to the format expected by the Google Maps server.
-
.join_list(sep, arg) ⇒ String
If arg is list-like, then joins it with sep.
-
.latlng(arg) ⇒ String
Converts a lat/lon pair to a comma-separated string.
-
.normalize_latlng(arg) ⇒ Array
Take the various lat/lng representations and return a tuple.
-
.time(arg) ⇒ String
Converts the value into a unix time (seconds since unix epoch).
-
.waypoint(waypoint) ⇒ String
Converts a waypoints to the format expected by the Google Maps server.
-
.waypoints(waypoints) ⇒ String
Converts an array of waypoints (path) to the format expected by the Google Maps server.
Class Method Details
.as_list(arg) ⇒ Array
Coerces arg into a list. If arg is already list-like, returns arg. Otherwise, returns a one-element list containing arg.
59 60 61 62 63 64 |
# File 'lib/google_maps_service/convert.rb', line 59 def as_list(arg) if arg.is_a?(Array) return arg end [arg] end |
.bounds(arg) ⇒ String
Converts a lat/lon bounds to a comma- and pipe-separated string.
Accepts two representations:
-
String: pipe-separated pair of comma-separated lat/lon pairs.
-
Hash with two entries - “southwest” and “northeast”. See latlng
for information on how these can be represented.
For example:
>> sydney_bounds = {
?> "northeast": {
?> "lat": -33.4245981,
?> "lng": 151.3426361
?> },
?> "southwest": {
?> "lat": -34.1692489,
?> "lng": 150.502229
?> }
?> }
>> GoogleMapsService::Convert.bounds(sydney_bounds)
=> '-34.169249,150.502229|-33.424598,151.342636'
127 128 129 130 131 132 133 134 135 |
# File 'lib/google_maps_service/convert.rb', line 127 def bounds(arg) if arg.is_a?(Hash) southwest = arg[:southwest] || arg["southwest"] northeast = arg[:northeast] || arg["northeast"] return "#{latlng(southwest)}|#{latlng(northeast)}" end raise ArgumentError, "Expected a bounds (southwest/northeast) Hash, but got #{arg.class}" end |
.components(arg) ⇒ String
Converts a dict of components to the format expected by the Google Maps server.
92 93 94 95 96 97 98 99 |
# File 'lib/google_maps_service/convert.rb', line 92 def components(arg) if arg.is_a?(Hash) arg = arg.sort.map { |k, v| "#{k}:#{v}" } return arg.join("|") end raise ArgumentError, "Expected a Hash for components, but got #{arg.class}" end |
.join_list(sep, arg) ⇒ String
If arg is list-like, then joins it with sep.
49 50 51 |
# File 'lib/google_maps_service/convert.rb', line 49 def join_list(sep, arg) as_list(arg).join(sep) end |
.latlng(arg) ⇒ String
Converts a lat/lon pair to a comma-separated string.
17 18 19 |
# File 'lib/google_maps_service/convert.rb', line 17 def latlng(arg) "%f,%f" % normalize_latlng(arg) end |
.normalize_latlng(arg) ⇒ Array
Take the various lat/lng representations and return a tuple.
Accepts various representations:
-
Hash with two entries - ‘lat` and `lng`
-
Array or list - e.g. ‘[-33, 151]`
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/google_maps_service/convert.rb', line 31 def normalize_latlng(arg) if arg.is_a?(Hash) lat = arg[:lat] || arg[:latitude] || arg["lat"] || arg["latitude"] lng = arg[:lng] || arg[:longitude] || arg["lng"] || arg["longitude"] return lat, lng elsif arg.is_a?(Array) return arg[0], arg[1] end raise ArgumentError, "Expected a lat/lng Hash or Array, but got #{arg.class}" end |
.time(arg) ⇒ String
Converts the value into a unix time (seconds since unix epoch).
75 76 77 78 79 80 |
# File 'lib/google_maps_service/convert.rb', line 75 def time(arg) if arg.is_a?(DateTime) arg = arg.to_time end arg.to_i.to_s end |
.waypoint(waypoint) ⇒ String
Converts a waypoints to the format expected by the Google Maps server.
Accept two representation of waypoint:
-
String: Name of place or comma-separated lat/lon pair.
-
Hash/Array: Lat/lon pair.
147 148 149 150 151 152 |
# File 'lib/google_maps_service/convert.rb', line 147 def waypoint(waypoint) if waypoint.is_a?(String) return waypoint end GoogleMapsService::Convert.latlng(waypoint) end |
.waypoints(waypoints) ⇒ String
Converts an array of waypoints (path) to the format expected by the Google Maps server.
Accept two representation of waypoint:
-
String: Name of place or comma-separated lat/lon pair.
-
Hash/Array: Lat/lon pair.
165 166 167 168 169 170 171 172 |
# File 'lib/google_maps_service/convert.rb', line 165 def waypoints(waypoints) if waypoints.is_a?(Array) && (waypoints.length == 2) && waypoints[0].is_a?(Numeric) && waypoints[1].is_a?(Numeric) waypoints = [waypoints] end waypoints = as_list(waypoints) join_list("|", waypoints.map { |k| waypoint(k) }) end |