Class: OmniFiles::UrlShortener

Inherits:
Object
  • Object
show all
Defined in:
lib/omnifiles/shortener.rb

Instance Method Summary collapse

Constructor Details

#initialize(salt, bytes_used) ⇒ UrlShortener

Returns a new instance of UrlShortener.



9
10
11
12
13
14
# File 'lib/omnifiles/shortener.rb', line 9

def initialize salt, bytes_used
  @salt = salt
  @bytes_used = bytes_used
  # base62
  @symbols = (0..9).to_a.map(&:to_s) + ('a'..'z').to_a + ('A'..'Z').to_a
end

Instance Method Details

#shorten(url) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/omnifiles/shortener.rb', line 16

def shorten url
  dig = Digest::MD5.digest(url+@salt.to_s)
  dig_ints = dig.unpack('N*').first(@bytes_used)
  dig_value = (0...dig_ints.length)
    .map { |i| (dig_ints[i] << ((dig_ints.length-i-1)*32)) }
    .reduce(:+)
  encode dig_value
end