Class: DynamicLinks::ShorteningStrategies::CRC32Strategy

Inherits:
BaseStrategy
  • Object
show all
Defined in:
lib/dynamic_links/shortening_strategies/crc32_strategy.rb

Constant Summary

Constants inherited from BaseStrategy

BaseStrategy::BASE62_CHARS, BaseStrategy::MIN_LENGTH

Instance Method Summary collapse

Instance Method Details

#shorten(url, min_length: MIN_LENGTH) ⇒ Object

Parameters:

  • url (String)

    The URL to shorten

  • min_length (Integer) (defaults to: MIN_LENGTH)

    The minimum length of the short URL



6
7
8
9
10
11
12
13
14
15
# File 'lib/dynamic_links/shortening_strategies/crc32_strategy.rb', line 6

def shorten(url, min_length: MIN_LENGTH)
  # Create a CRC32 hash of the URL
  hashed_url = Zlib.crc32(url).to_s(16)

  # Convert the CRC32 hash into a Base62 string
  short_url = base62_encode(hashed_url.to_i(16))

  # Ensure the short URL is at least #{min_length} characters long
  short_url.ljust(min_length, '0')
end