Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: RuboCop lint Metrics/BlockLength #927

Merged
merged 4 commits into from Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 0 additions & 6 deletions .rubocop_todo.yml
Expand Up @@ -10,12 +10,6 @@
Metrics/AbcSize:
Max: 79

# Offense count: 2
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 27

# Offense count: 5
# Configuration parameters: CountComments.
Metrics/ClassLength:
Expand Down
140 changes: 74 additions & 66 deletions lib/faraday/encoders/nested_params_encoder.rb
Expand Up @@ -10,12 +10,14 @@ class << self
def_delegators :'Faraday::Utils', :escape, :unescape
end

extend self

# @param params [nil, Array, #to_hash] parameters to be encoded
#
# @return [String] the encoded params
#
# @raise [TypeError] if params can not be converted to a Hash
def self.encode(params)
def encode(params)
return nil if params.nil?

unless params.is_a?(Array)
Expand All @@ -33,42 +35,11 @@ def self.encode(params)
params.sort!
end

# Helper lambda
to_query = lambda do |parent, value|
if value.is_a?(Hash)
value = value.map do |key, val|
key = escape(key)
[key, val]
end
value.sort!
buffer = +''
value.each do |key, val|
new_parent = "#{parent}%5B#{key}%5D"
buffer << "#{to_query.call(new_parent, val)}&"
end
return buffer.chop
elsif value.is_a?(Array)
new_parent = "#{parent}%5B%5D"
return new_parent if value.empty?

buffer = +''
value.each do |val|
buffer << "#{to_query.call(new_parent, val)}&"
end
return buffer.chop
elsif value.nil?
return parent
else
encoded_value = escape(value)
return "#{parent}=#{encoded_value}"
end
end

# The params have form [['key1', 'value1'], ['key2', 'value2']].
buffer = +''
params.each do |parent, value|
encoded_parent = escape(parent)
buffer << "#{to_query.call(encoded_parent, value)}&"
buffer << "#{encode_pair(encoded_parent, value)}&"
end
buffer.chop
end
Expand All @@ -78,7 +49,7 @@ def self.encode(params)
# @return [Array<Array, String>] the decoded params
#
# @raise [TypeError] if the nesting is incorrect
def self.decode(query)
def decode(query)
return nil if query.nil?

params = {}
Expand All @@ -88,52 +59,89 @@ def self.decode(query)
key, value = pair.split('=', 2)
key = unescape(key)
value = unescape(value.tr('+', ' ')) if value

subkeys = key.scan(/[^\[\]]+(?:\]?\[\])?/)
context = params
subkeys.each_with_index do |subkey, i|
is_array = subkey =~ /[\[\]]+\Z/
subkey = $` if is_array
last_subkey = i == subkeys.length - 1

if !last_subkey || is_array
value_type = is_array ? Array : Hash
raise TypeError, format("expected #{value_type.name} (got #{context[subkey].class.name}) for param `#{subkey}'") if context[subkey] && !context[subkey].is_a?(value_type)

context = (context[subkey] ||= value_type.new)
end

if context.is_a?(Array) && !is_array
context << {} if !context.last.is_a?(Hash) || context.last.key?(subkey)
context = context.last
end

if last_subkey
if is_array
context << value
else
context[subkey] = value
end
end
end
decode_pair(key, value, params)
end

dehash(params, 0)
end

private

SUBKEYS_REGEX = /[^\[\]]+(?:\]?\[\])?/.freeze

# Internal: convert a nested hash with purely numeric keys into an array.
# FIXME: this is not compatible with Rack::Utils.parse_nested_query
# @!visibility private
def self.dehash(hash, depth)
hash.each do |key, value|
hash[key] = dehash(value, depth + 1) if value.is_a?(Hash)
end
def dehash(hash, depth)
hash.each { |key, value| hash[key] = dehash(value, depth + 1) if value.is_a?(Hash) }

if depth.positive? && !hash.empty? && hash.keys.all? { |k| k =~ /^\d+$/ }
hash.keys.sort.inject([]) { |all, key| all << hash[key] }
iMacTia marked this conversation as resolved.
Show resolved Hide resolved
else
hash
end
end

def encode_pair(parent, value)
if value.is_a?(Hash)
encode_hash(parent, value)
elsif value.is_a?(Array)
encode_array(parent, value)
elsif value.nil?
parent
else
encoded_value = escape(value)
"#{parent}=#{encoded_value}"
end
end

def encode_hash(parent, value)
value = value.map { |key, val| [escape(key), val] }.sort

buffer = +''
value.each do |key, val|
new_parent = "#{parent}%5B#{key}%5D"
buffer << "#{encode_pair(new_parent, val)}&"
end
buffer.chop
end

def encode_array(parent, value)
new_parent = "#{parent}%5B%5D"
return new_parent if value.empty?

buffer = +''
value.each { |val| buffer << "#{encode_pair(new_parent, val)}&" }
buffer.chop
end

def decode_pair(key, value, context)
subkeys = key.scan(SUBKEYS_REGEX)
subkeys.each_with_index do |subkey, i|
is_array = subkey =~ /[\[\]]+\Z/
subkey = $` if is_array
last_subkey = i == subkeys.length - 1

if !last_subkey || is_array
value_type = is_array ? Array : Hash
raise TypeError, format("expected #{value_type.name} (got #{context[subkey].class.name}) for param `#{subkey}'") if context[subkey] && !context[subkey].is_a?(value_type)
iMacTia marked this conversation as resolved.
Show resolved Hide resolved

context = (context[subkey] ||= value_type.new)
end

if context.is_a?(Array) && !is_array
context << {} if !context.last.is_a?(Hash) || context.last.key?(subkey)
context = context.last
end

if last_subkey
if is_array
context << value
else
context[subkey] = value
end
end
end
end
end
end