Module: URITemplate::Utils::Escaping::Pure

Included in:
URITemplate::Utils
Defined in:
lib/uri_template/utils.rb

Overview

A pure escaping module, which implements escaping methods in pure ruby. The performance is acceptable, but could be better with escape_utils.

Constant Summary collapse

URL_ESCAPED =
/([^A-Za-z0-9\-\._])/.freeze
URI_ESCAPED =
/([^A-Za-z0-9!$&'()*+,.\/:;=?@\[\]_~])/.freeze
PCT =
/%([0-9a-fA-F]{2})/.freeze

Instance Method Summary collapse

Instance Method Details

#escape_uri(s) ⇒ Object



174
175
176
177
178
# File 'lib/uri_template/utils.rb', line 174

def escape_uri(s)
  to_utf8(s.to_s).gsub(URI_ESCAPED){
    '%'+$1.unpack('H2'*$1.bytesize).join('%').upcase
  }
end

#escape_url(s) ⇒ Object



168
169
170
171
172
# File 'lib/uri_template/utils.rb', line 168

def escape_url(s)
  to_utf8(s.to_s).gsub(URL_ESCAPED){
    '%'+$1.unpack('H2'*$1.bytesize).join('%').upcase
  }
end

#unescape_uri(s) ⇒ Object



186
187
188
189
190
# File 'lib/uri_template/utils.rb', line 186

def unescape_uri(s)
  force_utf8( to_ascii(s.to_s).gsub(PCT){
    $1.to_i(16).chr
  })
end

#unescape_url(s) ⇒ Object



180
181
182
183
184
# File 'lib/uri_template/utils.rb', line 180

def unescape_url(s)
  force_utf8( to_ascii(s.to_s).gsub('+',' ').gsub(PCT){
    $1.to_i(16).chr
  } )
end

#using_escape_utils?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/uri_template/utils.rb', line 192

def using_escape_utils?
  false
end