Module: StringBase
Constant Summary collapse
- KEYS =
'bcdghjklmnpqrstvwxyz'
- MULTIPLIER =
99
Instance Method Summary collapse
- #decode(string) ⇒ Object
- #encode(value) ⇒ Object
-
#extract(url_part) ⇒ Object
extract ID from url.
Instance Method Details
#decode(string) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# 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) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# 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) ⇒ Object
extract ID from url
32 33 34 35 36 |
# 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 |