Skip to content

Instantly share code, notes, and snippets.

@joemsak
Created October 4, 2014 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemsak/06fe11ad6c02efccc6c3 to your computer and use it in GitHub Desktop.
Save joemsak/06fe11ad6c02efccc6c3 to your computer and use it in GitHub Desktop.
class ShortUrl
def self.create(url)
url.shortened_hash = first_five_of_sha256(url)
url.save
end
def self.for(url)
ENV['HOSTNAME'] + url.shortened_hash
end
private
def self.first_five_of_sha256(url)
digest = Digest::SHA256.hexdigest(url.long)
digest[0..4]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment