Skip to content

Commit 6f807b4

Browse files
authored
Merge pull request #86 from google/drinckes-patch-2
ruby code length validation
2 parents c434a9c + 90353d5 commit 6f807b4

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

ruby/lib/plus_codes.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ module PlusCodes
99
# The max number of characters can be placed before the separator.
1010
SEPARATOR_POSITION = 8
1111

12+
# Maxiumum code length using lat/lng pair encoding. The area of such a
13+
# code is approximately 13x13 meters (at the equator), and should be suitable
14+
# for identifying buildings. This excludes prefix and separator characters.
15+
PAIR_CODE_LENGTH = 10
16+
1217
# The character used to pad a code
1318
PADDING = '0'.freeze
1419

ruby/lib/plus_codes/open_location_code.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def full?(code)
4141
# @param longitude [Numeric] a longitude in degrees
4242
# @param code_length [Integer] the number of characters in the code, this excludes the separator
4343
# @return [String] a plus+codes
44-
def encode(latitude, longitude, code_length = 10)
44+
def encode(latitude, longitude, code_length = PAIR_CODE_LENGTH)
4545
raise ArgumentError,
4646
"Invalid Open Location Code(Plus+Codes) length: #{code_length}" if invalid_length?(code_length)
4747

@@ -83,7 +83,7 @@ def decode(code)
8383

8484
digit = 0
8585
while digit < code.length
86-
if digit < 10
86+
if digit < PAIR_CODE_LENGTH
8787
lat_resolution /= 20
8888
lng_resolution /= 20
8989
south_latitude += lat_resolution * DECODE[code[digit].ord]
@@ -180,7 +180,7 @@ def narrow_region(digit, latitude, longitude)
180180
if digit == 0
181181
latitude /= 20
182182
longitude /= 20
183-
elsif digit < 10
183+
elsif digit < PAIR_CODE_LENGTH
184184
latitude *= 20
185185
longitude *= 20
186186
else
@@ -193,7 +193,7 @@ def narrow_region(digit, latitude, longitude)
193193
def build_code(digit_count, code, latitude, longitude)
194194
lat_digit = latitude.to_i
195195
lng_digit = longitude.to_i
196-
if digit_count < 10
196+
if digit_count < PAIR_CODE_LENGTH
197197
code << CODE_ALPHABET[lat_digit]
198198
code << CODE_ALPHABET[lng_digit]
199199
[digit_count + 2, latitude - lat_digit, longitude - lng_digit]
@@ -230,18 +230,18 @@ def valid_character?(code)
230230
end
231231

232232
def invalid_length?(code_length)
233-
code_length < 2 || (code_length < SEPARATOR_POSITION && code_length.odd?)
233+
code_length < 2 || (code_length < PAIR_CODE_LENGTH && code_length.odd?)
234234
end
235235

236236
def padded(code)
237237
code << PADDING * (SEPARATOR_POSITION - code.length) << SEPARATOR
238238
end
239239

240240
def precision_by_length(code_length)
241-
if code_length <= 10
241+
if code_length <= PAIR_CODE_LENGTH
242242
precision = 20 ** ((code_length / -2).to_i + 2)
243243
else
244-
precision = (20 ** -3) / (5 ** (code_length - 10))
244+
precision = (20 ** -3) / (5 ** (code_length - PAIR_CODE_LENGTH))
245245
end
246246
precision.to_r
247247
end

ruby/test/plus_codes_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ def test_exceptions
6363
assert_raise ArgumentError do
6464
@olc.encode(20, 30, 1)
6565
end
66+
assert_raise ArgumentError do
67+
@olc.encode(20, 30, 9)
68+
end
6669
assert_raise ArgumentError do
6770
@olc.recover_nearest('9C3W9QCJ-2VX', 51.3708675, -1.217765625)
6871
end

0 commit comments

Comments
 (0)