Class: Ush::Adapter
- Inherits:
-
Object
- Object
- Ush::Adapter
- Defined in:
- lib/ush.rb
Class Method Summary collapse
-
.parse_url(url) ⇒ Object
Strips a url of basic badchars.
-
.shorten(url) ⇒ Object
Shortens a url to a given 6 character string Adapted from github.com/technoweenie/guillotine.
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
-
SHA256 hash the url to hexdigest
-
Convert to Bignum
-
Pack it to a bitstring (big-endian int)
-
b64encode the bitstring removeing trailing junk
-
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 |