module StringBase

Constants

KEYS
MULTIPLIER

Public Instance Methods

decode(string) click to toggle source
# File lib/common/string_base.rb, line 21
def decode string
  ring = Hash[KEYS.chars.map.with_index.to_a]
  base = KEYS.length
  ret = string.reverse.chars.map.with_index.inject(0) do |sum,(char,i)|
    sum + ring[char] * (base**i)
  end
  raise 'Invalid decode base' if ret%MULTIPLIER>0
  ret/MULTIPLIER
end
encode(value) click to toggle source
# File lib/common/string_base.rb, line 9
def encode value
  value = value * MULTIPLIER
  ring = Hash[KEYS.chars.map.with_index.to_a.map(&:reverse)]
  base = KEYS.length
  result = []
  until value == 0
    result << ring[ value % base ]
    value /= base
  end
  result.reverse.join
end
extract(url_part) click to toggle source

extract ID from url

# File lib/common/string_base.rb, line 32
def extract(url_part)
  id_str = url_part.split('-').last
  return nil unless id_str
  StringBase.decode(id_str) rescue nil
end