Class: DynamicLinks::ShorteningStrategies::MD5Strategy
- Inherits:
-
BaseStrategy
- Object
- BaseStrategy
- DynamicLinks::ShorteningStrategies::MD5Strategy
- Defined in:
- lib/dynamic_links/shortening_strategies/md5_strategy.rb
Constant Summary
Constants inherited from BaseStrategy
BaseStrategy::BASE62_CHARS, BaseStrategy::MIN_LENGTH
Instance Method Summary collapse
-
#shorten(url, min_length: MIN_LENGTH) ⇒ Object
Shortens the given URL using an MD5 hash.
Instance Method Details
#shorten(url, min_length: MIN_LENGTH) ⇒ Object
Shortens the given URL using an MD5 hash
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/dynamic_links/shortening_strategies/md5_strategy.rb', line 7 def shorten(url, min_length: MIN_LENGTH) # Create an MD5 hash of the URL hashed_url = Digest::MD5.hexdigest(url) # Convert a portion of the MD5 hash into a Base62 string short_url = base62_encode(hashed_url[0...10].to_i(16)) # Ensure the short URL is at least #{min_length} characters long short_url.ljust(min_length, '0') end |