lib/gooddata/helpers/global_helpers_params.rb in gooddata-0.6.50 vs lib/gooddata/helpers/global_helpers_params.rb in gooddata-0.6.51
- old
+ new
@@ -55,30 +55,10 @@
# Decodes params as they came from the platform.
# @params Parameter hash need to be decoded
# @option options [Boolean] :resolve_reference_params Resolve reference parameters in gd_encoded_params or not
# @return [Hash] Decoded parameters
def decode_params(params, options = {})
- convert_secure_params = lambda do |args|
- args = args.select { |k, _| k.include? "|" }
- lines = args.keys.map do |key|
- hash = {}
- last_a = nil
- last_e = nil
- key.split("|").reduce(hash) do |a, e|
- last_a = a
- last_e = e
- a[e] = {}
- end
- last_a[last_e] = args[key]
- hash
- end
-
- lines.reduce({}) do |a, e|
- a.deep_merge(e)
- end
- end
-
key = ENCODED_PARAMS_KEY.to_s
hidden_key = ENCODED_HIDDEN_PARAMS_KEY.to_s
data_params = params[key] || '{}'
hidden_data_params = if params.key?(hidden_key) && params[hidden_key].nil?
"{\"#{hidden_key}\" : null}"
@@ -101,10 +81,12 @@
]
elsif v.is_a? Array
v.map do |v2|
resolve_reference.call(v2)
end
+ elsif !v.is_a?(String)
+ v
else
v.gsub(regexps) do |match|
if match =~ /\\\\/
data_params.is_a?(Hash) ? '\\' : '\\\\' # rubocop: disable Metrics/BlockNesting
elsif match =~ /\\\$/
@@ -128,25 +110,51 @@
end
begin
parsed_data_params = data_params.is_a?(Hash) ? data_params : JSON.parse(data_params)
parsed_hidden_data_params = hidden_data_params.is_a?(Hash) ? hidden_data_params : JSON.parse(hidden_data_params)
- rescue JSON::ParserError => e
- raise e.class, "Error reading json from '#{key}' or '#{hidden_key}', reason: #{e.message}"
+ rescue JSON::ParserError => exception
+ raise exception.class, "Error reading json from '#{key}' or '#{hidden_key}', reason: #{exception.message}"
end
# Add the nil on ENCODED_HIDDEN_PARAMS_KEY
# if the data was retrieved from API You will not have the actual values so encode -> decode is not losless. The nil on the key prevents the server from deleting the key
parsed_hidden_data_params[ENCODED_HIDDEN_PARAMS_KEY] = nil unless parsed_hidden_data_params.empty?
- secure_params = convert_secure_params.call(params)
- params.delete_if do |k, _|
- k.include?('|')
- end
params.delete(key)
params.delete(hidden_key)
- params.deep_merge(parsed_data_params).deep_merge(parsed_hidden_data_params).deep_merge(secure_params)
+ params = params.deep_merge(parsed_data_params).deep_merge(parsed_hidden_data_params)
+
+ if options[:convert_pipe_delimited_params]
+ convert_pipe_delimited_params = lambda do |args|
+ args = args.select { |k, _| k.include? "|" }
+ lines = args.keys.map do |k|
+ hash = {}
+ last_a = nil
+ last_e = nil
+ k.split("|").reduce(hash) do |a, e|
+ last_a = a
+ last_e = e
+ a[e] = {}
+ end
+ last_a[last_e] = args[k]
+ hash
+ end
+
+ lines.reduce({}) do |a, e|
+ a.deep_merge(e)
+ end
+ end
+
+ pipe_delimited_params = convert_pipe_delimited_params.call(params)
+ params.delete_if do |k, _|
+ k.include?('|')
+ end
+ params = params.deep_merge(pipe_delimited_params)
+ end
+
+ params
end
# A helper which allows you to diff two lists of objects. The objects
# can be arbitrary objects as long as they respond to to_hash because
# the diff is eventually done on hashes. It allows you to specify
@@ -230,9 +238,28 @@
lookup[key] = [] unless lookup.key?(key)
lookup[key] << e
end
end
lookup
+ end
+
+ def stringify_values(value)
+ case value
+ when nil
+ value
+ when Hash
+ Hash[
+ value.map do |k, v|
+ [k, stringify_values(v)]
+ end
+ ]
+ when Array
+ value.map do |v|
+ stringify_values(v)
+ end
+ else
+ value.to_s
+ end
end
end
end
end