Skip to content

Commit

Permalink
Add max code length for Ruby library.
Browse files Browse the repository at this point in the history
  • Loading branch information
zongweil committed Feb 14, 2019
1 parent 0b1ec08 commit ded31e2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ruby/lib/plus_codes.rb
Expand Up @@ -14,6 +14,9 @@ module PlusCodes
# for identifying buildings. This excludes prefix and separator characters.
PAIR_CODE_LENGTH = 10

# Maximum number of digits to process in a plus code.
MAX_CODE_LENGTH = 15

# The character used to pad a code
PADDING = '0'.freeze

Expand Down
5 changes: 4 additions & 1 deletion ruby/lib/plus_codes/open_location_code.rb
Expand Up @@ -45,6 +45,9 @@ def encode(latitude, longitude, code_length = PAIR_CODE_LENGTH)
raise ArgumentError,
"Invalid Open Location Code(Plus+Codes) length: #{code_length}" if invalid_length?(code_length)

if code_length > MAX_CODE_LENGTH
code_length = MAX_CODE_LENGTH
end
latitude = clip_latitude(latitude)
longitude = normalize_longitude(longitude)
latitude -= precision_by_length(code_length) if latitude == 90
Expand Down Expand Up @@ -82,7 +85,7 @@ def decode(code)
lng_resolution = 400.to_r

digit = 0
while digit < code.length
while digit < [code.length, MAX_CODE_LENGTH].min
if digit < PAIR_CODE_LENGTH
lat_resolution /= 20
lng_resolution /= 20
Expand Down

0 comments on commit ded31e2

Please sign in to comment.