class Paramedic::Encoder

Attributes

params[R]

Public Class Methods

new(params) click to toggle source
# File lib/paramedic/encoder.rb, line 5
def initialize(params)
  @params = params
end

Public Instance Methods

to_s() click to toggle source
# File lib/paramedic/encoder.rb, line 9
def to_s
  params.collect { |k, v| "#{k}=#{encode(v)}" }.join('&')
end

Private Instance Methods

encode(value) click to toggle source
# File lib/paramedic/encoder.rb, line 17
def encode(value)
  URI.escape(value.to_s).gsub(Regexp.new(terms)) { |match| replacements[match] }
end
replacements() click to toggle source
# File lib/paramedic/encoder.rb, line 21
def replacements
  {
    '?' => '%3F',
    '=' => '%3D',
    '/' => '%2F',
    '{' => '%7B',
    '}' => '%7D',
    ':' => '%3A',
    ',' => '%2C',
    ';' => '%3B',
    '#' => '%23',
    '&' => '%26',
    '@' => '%40',
    #'%' => '%25',    # Surprisingly, this one doesn't appear to be replaced. We'll need to confirm.
    '+' => '%2B',
    '$' => '%24',
    '<' => '%3C',
    '>' => '%3E',
    #'~' => '%25',    # Tilde too.
    '^' => '%5E',
    '`' => '%60',
    ?\ => '%5C',
    '[' => '%5B',
    ']' => '%5D',
    '|' => '%7C',
    '"' => '%22',
    '%0A' => '%0D%0A' # This converts back to Microsoft's carriage return line feed funness.
  }
end
terms() click to toggle source
# File lib/paramedic/encoder.rb, line 51
def terms
  replacements.keys.collect { |term| Regexp.escape(term) }.join('|')
end