Module: HWSStrings

Defined in:
lib/hws_strings.rb

Constant Summary

DEFAULT_ILLEGAL_CHARS =

These constants are used as the default configuration for the component_encode and component_decode methods.

['~', '/', ' ']
DEFAULT_ESCAPE =
'~'

Class Method Summary (collapse)

Class Method Details

+ (Object) component_decode(str, escape = DEFAULT_ESCAPE, illegal_characters = DEFAULT_ILLEGAL_CHARS, base = 16)

Decodes a string encoded with component_encode.



29
30
31
32
33
34
35
36
# File 'lib/hws_strings.rb', line 29

def self.component_decode(str, escape = DEFAULT_ESCAPE,
    illegal_characters = DEFAULT_ILLEGAL_CHARS, base=16)
  escape_map = Hash[illegal_characters.map{ |c| [c, "#{escape}#{c.ord.to_s(base)}"] }]
  escape_map.each do |ch, escaped|
    str = str.gsub(escaped, ch)
  end
  str
end

+ (Object) component_encode(str, illegal_characters: DEFAULT_ILLEGAL_CHARS)

Encode the string to make it usable as a single path in a URL.

This a simple scheme that should allow you to match the string in a single path variable easily, in a way that should not be messed with by your web framework.

Many characters that are legal as file names by the Helix Versioning Engine can make URL handling difficult. The most basic is the forward slash '/'.

This does NOT guarantee that the string is URL safe! You should still percent-encode any string as a URL.



18
19
20
21
22
23
24
25
26
# File 'lib/hws_strings.rb', line 18

def self.component_encode(str, illegal_characters: DEFAULT_ILLEGAL_CHARS)
  str.chars.map do |ch|
    if illegal_characters.include?(ch)
      '~' + ch.ord.to_s(16)
    else
      ch
    end
  end.join('')
end