Class: DynamicLinks::ShorteningStrategies::SHA256Strategy
- Inherits:
-
BaseStrategy
- Object
- BaseStrategy
- DynamicLinks::ShorteningStrategies::SHA256Strategy
- Defined in:
- lib/dynamic_links/shortening_strategies/sha256_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
we use this parameter to increase the length of the short URL in order to avoid collisions.
Instance Method Details
#shorten(url, min_length: MIN_LENGTH) ⇒ Object
we use this parameter to increase the length of the short URL in order to avoid collisions
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/dynamic_links/shortening_strategies/sha256_strategy.rb', line 8 def shorten(url, min_length: MIN_LENGTH) # Create a hash of the URL hashed_url = Digest::SHA256.hexdigest(url) # Convert the first few characters of the 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 short_url.ljust(min_length, '0') end |