Last active
March 10, 2016 21:02
-
-
Save xavdid/ad35093e5d4b2443487c to your computer and use it in GitHub Desktop.
Convert Salesforce 15 digit id to 18
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InvalidIdError < RuntimeError | |
end | |
def convert_to_18(id) | |
if id.nil? | |
return nil | |
elsif id.size != 15 | |
puts "INVALID ID: #{id}" | |
raise InvalidIdError | |
end | |
suffix = '' | |
3.times do |i| | |
flags = 0 | |
# 3 chunks of 5 chars | |
5.times do |j| | |
c = id[(i * 5) + j] | |
# add flag if c is uppercase | |
if /[[:upper:]]/.match(c) | |
flags += (1 << j) | |
end | |
end | |
suffix += ['A'..'Z', '0'..'5'].map(&:to_a).flatten[flags] | |
end | |
id + suffix | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment