require"forwardable"moduleFaradaymoduleNestedParamsEncoderclass<<selfextendForwardabledef_delegators:'Faraday::Utils',:escape,:unescapeenddefself.encode(params)returnnilifparams==nilif!params.is_a?(Array)if!params.respond_to?(:to_hash)raiseTypeError,"Can't convert #{params.class} into Hash."endparams=params.to_hashparams=params.mapdo|key,value|key=key.to_sifkey.kind_of?(Symbol)[key,value]end# Useful default for OAuth and caching.# Only to be used for non-Array inputs. Arrays should preserve order.params.sort!end# Helper lambdato_query=lambdado|parent,value|ifvalue.is_a?(Hash)value=value.mapdo|key,val|key=escape(key)[key,val]endvalue.sort!buffer=""value.eachdo|key,val|new_parent="#{parent}%5B#{key}%5D"buffer<<"#{to_query.call(new_parent,val)}&"endreturnbuffer.chopelsifvalue.is_a?(Array)buffer=""value.each_with_indexdo|val,i|new_parent="#{parent}%5B%5D"buffer<<"#{to_query.call(new_parent,val)}&"endreturnbuffer.chopelsifvalue.nil?returnparentelseencoded_value=escape(value)return"#{parent}=#{encoded_value}"endend# The params have form [['key1', 'value1'], ['key2', 'value2']].buffer=''params.eachdo|parent,value|encoded_parent=escape(parent)buffer<<"#{to_query.call(encoded_parent,value)}&"endreturnbuffer.chopenddefself.decode(query)returnnilifquery==nilparams={}query.split("&").eachdo|pair|nextifpair.empty?key,value=pair.split("=",2)key=unescape(key)value=unescape(value.gsub(/\+/,' '))ifvaluesubkeys=key.scan(/[^\[\]]+(?:\]?\[\])?/)context=paramssubkeys.each_with_indexdo|subkey,i|is_array=subkey=~/[\[\]]+\Z/subkey=$`ifis_arraylast_subkey=i==subkeys.length-1if!last_subkey||is_arrayvalue_type=is_array?Array:Hashifcontext[subkey]&&!context[subkey].is_a?(value_type)raiseTypeError,"expected %s (got %s) for param `%s'"%[value_type.name,context[subkey].class.name,subkey]endcontext=(context[subkey]||=value_type.new)endifcontext.is_a?(Array)&&!is_arrayif!context.last.is_a?(Hash)||context.last.has_key?(subkey)context<<{}endcontext=context.lastendiflast_subkeyifis_arraycontext<<valueelsecontext[subkey]=valueendendendenddehash(params,0)end# Internal: convert a nested hash with purely numeric keys into an array.# FIXME: this is not compatible with Rack::Utils.parse_nested_querydefself.dehash(hash,depth)hash.eachdo|key,value|hash[key]=dehash(value,depth+1)ifvalue.kind_of?(Hash)endifdepth>0&&!hash.empty?&&hash.keys.all?{|k|k=~/^\d+$/}hash.keys.sort.inject([]){|all,key|all<<hash[key]}elsehashendendendmoduleFlatParamsEncoderclass<<selfextendForwardabledef_delegators:'Faraday::Utils',:escape,:unescapeenddefself.encode(params)returnnilifparams==nilif!params.is_a?(Array)if!params.respond_to?(:to_hash)raiseTypeError,"Can't convert #{params.class} into Hash."endparams=params.to_hashparams=params.mapdo|key,value|key=key.to_sifkey.kind_of?(Symbol)[key,value]end# Useful default for OAuth and caching.# Only to be used for non-Array inputs. Arrays should preserve order.params.sort!end# The params have form [['key1', 'value1'], ['key2', 'value2']].buffer=''params.eachdo|key,value|encoded_key=escape(key)value=value.to_sifvalue==true||value==falseifvalue==nilbuffer<<"#{encoded_key}&"elsifvalue.kind_of?(Array)value.eachdo|sub_value|encoded_value=escape(sub_value)buffer<<"#{encoded_key}=#{encoded_value}&"endelseencoded_value=escape(value)buffer<<"#{encoded_key}=#{encoded_value}&"endendreturnbuffer.chopenddefself.decode(query)empty_accumulator={}returnnilifquery==nilsplit_query=(query.split('&').mapdo|pair|pair.split('=',2)ifpair&&!pair.empty?end).compactreturnsplit_query.inject(empty_accumulator.dup)do|accu,pair|pair[0]=unescape(pair[0])pair[1]=trueifpair[1].nil?ifpair[1].respond_to?(:to_str)pair[1]=unescape(pair[1].to_str.gsub(/\+/," "))endifaccu[pair[0]].kind_of?(Array)accu[pair[0]]<<pair[1]elsifaccu[pair[0]]accu[pair[0]]=[accu[pair[0]],pair[1]]elseaccu[pair[0]]=pair[1]endaccuendendendend