2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby で Salesforce の15桁IDを18桁IDに変換する

Posted at

salesforceのIDは15桁版と18桁版が有ります。
rubyで15桁から18桁に変換するコードが見つからなかったので以下のURLからruby版を作ってみました。

# salesforce1518.rb

class Salesforce1518

  def self.cvt1518(id)
    raise ArgumentError if id.nil?
    return id if id.size == 18
    raise ArgumentError if id.size != 15

    triplet = [id[0, 5], id[5, 5], id[10, 5]]
    suffix = triplet.map do |value|
      str = value.reverse.chars.map do |char|
        /[[:upper:]]/ === char ? '1' : '0'
      end.join
      binary_id_lookup[str]
    end.join
    id + suffix
  end

  private

  def self.binary_id_lookup
    {
        '00000' => 'A',
        '00001' => 'B',
        '00010' => 'C',
        '00011' => 'D',
        '00100' => 'E',
        '00101' => 'F',
        '00110' => 'G',
        '00111' => 'H',
        '01000' => 'I',
        '01001' => 'J',
        '01010' => 'K',
        '01011' => 'L',
        '01100' => 'M',
        '01101' => 'N',
        '01110' => 'O',
        '01111' => 'P',
        '10000' => 'Q',
        '10001' => 'R',
        '10010' => 'S',
        '10011' => 'T',
        '10100' => 'U',
        '10101' => 'V',
        '10110' => 'W',
        '10111' => 'X',
        '11000' => 'Y',
        '11001' => 'Z',
        '11010' => '0',
        '11011' => '1',
        '11100' => '2',
        '11101' => '3',
        '11110' => '4',
        '11111' => '5',
    }
  end
end

ダイジョブぽい

# specにて
expect(Salesforce1518.cvt1518('a06O000000TL5ci')).to eq('a06O000000TL5ciIAD')

TODO: gem化

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?