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.
60 61 62 63 64 65 |
# File 'lib/google_maps_service/convert.rb', line 60 def as_list(arg) if arg.kind_of?(Array) return arg end return [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'
129 130 131 132 133 134 135 136 137 |
# File 'lib/google_maps_service/convert.rb', line 129 def bounds(arg) if arg.kind_of?(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.
94 95 96 97 98 99 100 101 |
# File 'lib/google_maps_service/convert.rb', line 94 def components(arg) if arg.kind_of?(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.
50 51 52 |
# File 'lib/google_maps_service/convert.rb', line 50 def join_list(sep, arg) return as_list(arg).join(sep) end |
.latlng(arg) ⇒ String
Converts a lat/lon pair to a comma-separated string.
18 19 20 |
# File 'lib/google_maps_service/convert.rb', line 18 def latlng(arg) return "%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
andlng
- Array or list - e.g.
[-33, 151]
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/google_maps_service/convert.rb', line 32 def normalize_latlng(arg) if arg.kind_of?(Hash) lat = arg[:lat] || arg[:latitude] || arg["lat"] || arg["latitude"] lng = arg[:lng] || arg[:longitude] || arg["lng"] || arg["longitude"] return lat, lng elsif arg.kind_of?(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).
77 78 79 80 81 82 |
# File 'lib/google_maps_service/convert.rb', line 77 def time(arg) if arg.kind_of?(DateTime) arg = arg.to_time end return 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.
149 150 151 152 153 154 |
# File 'lib/google_maps_service/convert.rb', line 149 def waypoint(waypoint) if waypoint.kind_of?(String) return waypoint end return 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.
167 168 169 170 171 172 173 174 |
# File 'lib/google_maps_service/convert.rb', line 167 def waypoints(waypoints) if waypoints.kind_of?(Array) and waypoints.length == 2 and waypoints[0].kind_of?(Numeric) and waypoints[1].kind_of?(Numeric) waypoints = [waypoints] end waypoints = as_list(waypoints) return join_list('|', waypoints.map { |k| waypoint(k) }) end |