class Flayyer::FlayyerHash

A compatible qs stringify/parse (github.com/ljharb/qs)

Public Class Methods

new(hash) click to toggle source
# File lib/flayyer.rb, line 148
def initialize(hash)
  @hash = hash
end

Public Instance Methods

to_query(key = nil) click to toggle source
# File lib/flayyer.rb, line 167
def to_query(key = nil)
  URI.encode_www_form(self.to_query_hash(key))
end
to_query_hash(key) click to toggle source
# File lib/flayyer.rb, line 152
def to_query_hash(key)
  @hash.reduce({}) do |h, (k, v)|
    new_key = key.nil? ? k : "#{key}[#{k}]"
    v = Hash[v.each_with_index.to_a.map(&:reverse)] if v.is_a?(Array)
    if v.is_a?(Hash)
      h.merge!(FlayyerHash.new(v).to_query_hash(new_key))
    elsif v.nil?
      # skip null values
    else
      h[new_key] = v
    end
    h
  end
end