Sha256: 28ed7ecde71f9387799ee985bd70ac6dc90dcfdb7f330e98a814c9b1f03f5cb8

Contents?: true

Size: 1004 Bytes

Versions: 19

Compression:

Stored size: 1004 Bytes

Contents

module Deas

  module Cgi
    # taken from http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/index.html
    # => not requiring 'cgi' to save on memory usage

    def self.escape(string)
      string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
        '%' + $1.unpack('H2' * $1.size).join('%').upcase
      end.gsub(' ', '%20')
    end

    def self.http_query(value, key_ns = nil)
      # optimized version taken from:
      # http://github.com/kelredd/useful/blob/master/lib/useful/ruby_extensions/hash.rb
      value.sort{|a,b| a[0].to_s <=> b[0].to_s}.collect do |key_val|
        key, val = key_val
        key_s = key_ns ? "#{key_ns}[#{key_val[0].to_s}]" : key_val[0].to_s
        if key_val[1].kind_of?(::Array)
          key_val[1].sort.collect{|i| "#{key_s}[]=#{Deas::Cgi.escape(i.to_s)}"}.join('&')
        elsif key_val[1].kind_of?(::Hash)
          Deas::Cgi.http_query(key_val[1], key_s)
        else
          "#{key_s}=#{Deas::Cgi.escape(key_val[1].to_s)}"
        end
      end.join('&')
    end

  end

end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
deas-0.34.0 lib/deas/cgi.rb
deas-0.33.0 lib/deas/cgi.rb
deas-0.32.0 lib/deas/cgi.rb
deas-0.31.0 lib/deas/cgi.rb
deas-0.30.0 lib/deas/cgi.rb
deas-0.29.0 lib/deas/cgi.rb
deas-0.28.0 lib/deas/cgi.rb
deas-0.27.0 lib/deas/cgi.rb
deas-0.26.0 lib/deas/cgi.rb
deas-0.25.0 lib/deas/cgi.rb
deas-0.24.1 lib/deas/cgi.rb
deas-0.24.0 lib/deas/cgi.rb
deas-0.23.4 lib/deas/cgi.rb
deas-0.23.3 lib/deas/cgi.rb
deas-0.23.2 lib/deas/cgi.rb
deas-0.23.1 lib/deas/cgi.rb
deas-0.23.0 lib/deas/cgi.rb
deas-0.22.1 lib/deas/cgi.rb
deas-0.22.0 lib/deas/cgi.rb