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

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.

Parameters:

  • arg (Object)

Returns:

  • (Array)


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:

  1. String: pipe-separated pair of comma-separated lat/lon pairs.

  2. 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'

Parameters:

  • arg (Hash)

    The bounds.

Returns:

  • (String)

Raises:

  • (ArgumentError)


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.

Examples:

>> GoogleMapsService::Convert.components({"country": "US", "postal_code": "94043"})
=> "country:US|postal_code:94043"

Parameters:

  • arg (Hash)

    The component filter.

Returns:

  • (String)

Raises:

  • (ArgumentError)


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.

Parameters:

  • sep (String)

    Separator string.

  • arg (Array, String)

    Value to coerce into a list.

Returns:

  • (String)


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.

Examples:

>> GoogleMapsService::Convert.latlng({"lat": -33.8674869, "lng": 151.2069902})
=> "-33.867487,151.206990"

Parameters:

  • arg (Hash, Array)

    The lat/lon hash or array pair.

Returns:

  • (String)

    Comma-separated lat/lng.

Raises:

  • (ArgumentError)

    When argument is not lat/lng hash or array.



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:

  1. Hash with two entries - ‘lat` and `lng`

  2. Array or list - e.g. ‘[-33, 151]`

Parameters:

  • arg (Hash, Array)

    The lat/lon hash or array pair.

Returns:

  • (Array)

    Pair of lat and lng array.

Raises:

  • (ArgumentError)


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).

Examples:

>> GoogleMapsService::Convert.time(datetime.now())
=> "1409810596"

Parameters:

  • arg (Time, Date, DateTime, Integer)

    The time.

Returns:

  • (String)

    String representation of epoch time



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:

  1. String: Name of place or comma-separated lat/lon pair.

  2. Hash/Array: Lat/lon pair.

Parameters:

  • waypoint (Array, String, Hash)

    Path.

Returns:

  • (String)


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:

  1. String: Name of place or comma-separated lat/lon pair.

  2. Hash/Array: Lat/lon pair.

Parameters:

  • waypoints (Array, String, Hash)

    Path.

Returns:

  • (String)


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