Skip to content

Commit 90353d5

Browse files
authored
Use code length constant
1 parent 61b91cb commit 90353d5

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

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

0 commit comments

Comments
 (0)