lib/soapy_cake/helper.rb in soapy_cake-2.1.0 vs lib/soapy_cake/helper.rb in soapy_cake-2.1.1

- old
+ new

@@ -3,13 +3,13 @@ module Helper def walk_tree(obj, key = nil, &block) return nil if obj == {} case obj - when Hash + when Hash, Saxerator::Builder::HashElement obj.map { |hk, hv| [hk, walk_tree(hv, hk, &block)] }.to_h - when Array + when Array, Saxerator::Builder::ArrayElement obj.map { |av| walk_tree(av, &block) } else yield(obj, key) end end @@ -18,11 +18,11 @@ raise Error, "Parameter '#{key}' must be > 0!" if opts[key].to_i < 1 end def require_params(opts, params) params.each do |param| - raise Error, "Parameter '#{param}' missing!" unless opts.key?(param) + raise Error, "Parameter '#{param}' missing!" if opts[param].nil? end end def translate_booleans(opts) opts.transform_values do |v| @@ -32,20 +32,25 @@ else v end end end - def translate_values(opts, params) + def translate_values(opts) opts.map do |k, v| - [ - k, - params.include?(k) ? const_lookup(k, v) : v - ] + id_key = :"#{k}_id" + + if Const::CONSTS.key?(id_key) + [id_key, const_lookup(id_key, v)] + elsif Const::CONSTS.key?(k) && !v.is_a?(Integer) + [k, const_lookup(k, v)] + else + [k, v] + end end.to_h end def const_lookup(type, key) - Const::CONSTS[type].fetch(key) do + Const::CONSTS.fetch(type).fetch(key) do raise ArgumentError, "#{key} is not a valid value for #{type}" end end # Some API calls require expiration dates.