Class: Supreme::URI

Inherits:
Object
  • Object
show all
Defined in:
lib/supreme/uri.rb

Constant Summary collapse

TBLENCWWWCOMP_ =

Borrowed from uri/common.rb, with modifications to support 1.8.x github.com/ruby/ruby/blob/trunk/lib/uri/common.rb

{}
TBLDECWWWCOMP_ =

:nodoc:

{}

Class Method Summary collapse

Class Method Details

.decode(value) ⇒ Object



67
68
69
# File 'lib/supreme/uri.rb', line 67

def self.decode(value)
  Supreme::URI.decode_www_form_component(value)
end

.decode_www_form_component(str, enc = nil) ⇒ Object

Decode given str of URL-encoded form data.

This decods + to SP.

See URI.encode_www_form_component, URI.decode_www_form

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/supreme/uri.rb', line 46

def self.decode_www_form_component(str, enc=nil)
  if TBLDECWWWCOMP_.empty?
    tbl = {}
    256.times do |i|
      h, l = i>>4, i&15
      tbl['%%%X%X' % [h, l]] = i.chr
      tbl['%%%x%X' % [h, l]] = i.chr
      tbl['%%%X%x' % [h, l]] = i.chr
      tbl['%%%x%x' % [h, l]] = i.chr
    end
    tbl['+'] = ' '
    begin
      TBLDECWWWCOMP_.replace(tbl)
      TBLDECWWWCOMP_.freeze
    rescue
    end
  end
  raise ArgumentError, "invalid %-encoding (#{str})" unless /\A(?:%[0-9a-fA-F]{2}|[^%])*\z/ =~ str
  str.gsub(/\+|%[0-9a-fA-F]{2}/) {|m| TBLDECWWWCOMP_[m]}
end

.encode(value) ⇒ Object



71
72
73
# File 'lib/supreme/uri.rb', line 71

def self.encode(value)
  Supreme::URI.encode_www_form_component(value).gsub("%7E", '~').gsub("+", "%20")
end

.encode_www_form_component(s) ⇒ Object

Encode given s to URL-encoded form data.

This method doesn’t convert *, -, ., 0-9, A-Z, _, a-z, but does convert SP (ASCII space) to + and converts others to %XX.

This is an implementation of www.w3.org/TR/html5/forms.html#url-encoded-form-data

See URI.decode_www_form_component, URI.encode_www_form



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/supreme/uri.rb', line 18

def self.encode_www_form_component(s)
  str = s.to_s
  if RUBY_VERSION < "1.9" && $KCODE =~ /u/i
    str.gsub(/([^ a-zA-Z0-9_.-]+)/) do
      '%' + $1.unpack('H2' * $1.length).join('%').upcase
    end.tr(' ', '+')
  else
    if TBLENCWWWCOMP_.empty?
      tbl = {}
      256.times do |i|
        tbl[i.chr] = '%%%02X' % i
      end
      tbl[' '] = '+'
      begin
        TBLENCWWWCOMP_.replace(tbl)
        TBLENCWWWCOMP_.freeze
      rescue
      end
    end
    str.gsub(/[^*\-.0-9A-Z_a-z]/) {|m| TBLENCWWWCOMP_[m]}
  end
end

.generate_query(pairs) ⇒ Object



82
83
84
85
86
# File 'lib/supreme/uri.rb', line 82

def self.generate_query(pairs)
  pairs.inject([]) do |parts, (key, value)|
    parts << "#{encode(key)}=#{encode(value)}"
  end.join('&')
end

.parse_query(query_string) ⇒ Object



75
76
77
78
79
80
# File 'lib/supreme/uri.rb', line 75

def self.parse_query(query_string)
  return [] if query_string.to_s.strip == ''
  query_string.strip.split('&').inject([]) do |parsed, pair|
    parsed << pair.split('=', 2).map { |x| decode(x) }
  end
end