Sha256: fb2450e5bb0ede0cc6ccdb5ac7e78e5163510b438afbe5a271cc0bec53596f89

Contents?: true

Size: 717 Bytes

Versions: 8

Compression:

Stored size: 717 Bytes

Contents

require "cgi"

module Digicert
  class Util
    def to_query(query)
      build_query(query).flatten.join("&")
    end

    def self.to_query(query)
      new.to_query(query)
    end

    private

    def build_query(value, key = nil)
      if value.is_a?(Hash)
        build_recursive_query(value, key)
      else
        build_escaped_key_value_pair(key, value)
      end
    end

    def build_recursive_query(query, namespace = nil)
      query.map do |key, value|
        query_key = namespace ? "#{namespace}[#{key}]" : key
        build_query(value, query_key)
      end
    end

    def build_escaped_key_value_pair(key, value)
      [CGI.escape(key.to_s), CGI.escape(value.to_s)].join("=")
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
digicert-1.0.0 lib/digicert/util.rb
digicert-0.5.0 lib/digicert/util.rb
digicert-0.4.1 lib/digicert/util.rb
digicert-0.4.0 lib/digicert/util.rb
digicert-0.3.1 lib/digicert/util.rb
digicert-0.3.0 lib/digicert/util.rb
digicert-0.2.0 lib/digicert/util.rb
digicert-0.1.2 lib/digicert/util.rb