Class: Ush::Adapter

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

Class Method Summary collapse

Class Method Details

.parse_url(url) ⇒ Object

Strips a url of basic badchars



35
36
37
38
39
40
# File 'lib/ush.rb', line 35

def self.parse_url(url)
  url.gsub!(/\s/, '')
  url.gsub!(/\?.*/, '')
  url.gsub!(/\#.*/, '')
  Addressable::URI.parse(url).to_s
end

.shorten(url) ⇒ Object

Shortens a url to a given 6 character string Adapted from github.com/technoweenie/guillotine

  1. SHA256 hash the url to hexdigest

  2. Convert to Bignum

  3. Pack it to a bitstring (big-endian int)

  4. b64encode the bitstring removeing trailing junk

  5. xor final string with a random value TODO



30
31
32
# File 'lib/ush.rb', line 30

def self.shorten(url)
  Base64.urlsafe_encode64([Digest::SHA256.hexdigest(url).to_i(16)].pack("N")).sub(/==\n?$/, '')
end