Sha256: 4947d02ca58ca221aaf5f55cd6875f8712264ee4643106b460bc15f83d088e4a

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

module Elevate
module HTTP
  module URI
    def self.encode_www_form(enum)
      enum.map do |k,v|
        if v.nil?
          encode_www_form_component(k)
        elsif v.respond_to?(:to_ary)
          v.to_ary.map do |w|
            str = encode_www_form_component(k)

            if w.nil?
              str
            else
              str + "=" + encode_www_form_component(w)
            end
          end.join('&')
        else
          encode_www_form_component(k) + "=" + encode_www_form_component(v)
        end
      end.join('&')
    end

    def self.encode_www_form_component(str)
      # From AFNetworking :)
      CFURLCreateStringByAddingPercentEscapes(nil,
                                              str,
                                              "[].",
                                              ":/?&=;+!@\#$()~',*",
                                              CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding))
    end

    def self.encode_query(hash)
      return "" if hash.nil? || hash.empty?

      hash.map do |key, value|
        "#{URI.escape_query_component(key.to_s)}=#{URI.escape_query_component(value.to_s)}"
      end.join("&")
    end

    def self.escape_query_component(component)
      component.gsub(/([^ a-zA-Z0-9_.-]+)/) do
        '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
      end.tr(' ', '+')
    end
  end
end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
elevate-0.7.0 lib/elevate/http/uri.rb
elevate-0.6.0 lib/elevate/http/uri.rb