Module: LZString::URI

Defined in:
lib/lzstring/uri.rb

Overview

Module for URI encoding/decoding

Class Method Summary collapse

Class Method Details

.compress_to_encoded_uri_component(input) ⇒ String

Compress a string to uri encoding

Parameters:

  • input (String)

    String to compress

Returns:

  • (String)

    URI encoded compressed string



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lzstring/uri.rb', line 7

def self.compress_to_encoded_uri_component(input)
  return "" if input.nil? || input.empty?

  begin
    # Force input to UTF-8 encoding
    input = input.to_s.dup.force_encoding(Encoding::UTF_8)

    # Get the keyStr for URI component encoding
    keyStr = key_str_uri_safe

    # Use the _compress function with URI parameters
    LZString._compress(input, 6) do |a|
      keyStr[a]
    end
  rescue
    # Log error and return empty string on failure
    # STDERR.puts "URI compression error: #{e.message}"
    ""
  end
end

.decompress_from_encoded_uri_component(input) ⇒ String?

Decompress a string from uri encoding

Parameters:

  • input (String)

    URI encoded compressed string

Returns:

  • (String, nil)

    Decompressed string or nil if decompression fails



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lzstring/uri.rb', line 31

def self.decompress_from_encoded_uri_component(input)
  return "" if input.nil? || input.empty?

  begin
    # Ensure input is properly encoded
    input = input.to_s.dup.force_encoding(Encoding::UTF_8)

    # Replace spaces with plus signs (if needed)
    input = input.gsub(" ", "+") if input.include?(" ")

    # Get the keyStr for URI component encoding
    keyStr = key_str_uri_safe

    # Use the _decompress function with URI parameters
    LZString._decompress(input.length, 32) do |index|
      if index >= input.length
        0
      else
        get_base_value(keyStr, input[index])
      end
    end
  rescue
    # Log error and return nil on failure
    # STDERR.puts "URI decompression error: #{e.message}"
    nil
  end
end

.get_base_value(alphabet, character) ⇒ Integer

Get the base value for a character

Parameters:

  • alphabet (String)

    The alphabet string to use

  • character (String)

    The character to get the base value for

Returns:

  • (Integer)

    The base value



69
70
71
# File 'lib/lzstring/uri.rb', line 69

def self.get_base_value(alphabet, character)
  alphabet.index(character) || 0
end

.key_str_uri_safeString

Get the key string for URI safe encoding

Returns:

  • (String)

    URI safe key string



61
62
63
# File 'lib/lzstring/uri.rb', line 61

def self.key_str_uri_safe
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$"
end