module SoapyCake::Helper
Public Instance Methods
const_lookup(type, key)
click to toggle source
# File lib/soapy_cake/helper.rb, line 52 def const_lookup(type, key) Const::CONSTS.fetch(type).fetch(key) do raise ArgumentError, "#{key} is not a valid value for #{type}" end end
future_expiration_date()
click to toggle source
Some API calls require expiration dates. The default is to not expire campaigns/offers/etc., so we set this to far in the future. It cannot be that far in the future though because it causes a datetime overflow in the steam powered rusty black box they call a database server.
# File lib/soapy_cake/helper.rb, line 62 def future_expiration_date Date.current + (365 * 30) end
require_params(opts, params)
click to toggle source
# File lib/soapy_cake/helper.rb, line 22 def require_params(opts, params) params.each do |param| raise Error, "Parameter '#{param}' missing!" if opts[param].nil? end end
translate_booleans(opts)
click to toggle source
# File lib/soapy_cake/helper.rb, line 28 def translate_booleans(opts) opts.transform_values do |v| case v when true then 'on' when false then 'off' else v end end end
translate_values(opts)
click to toggle source
# File lib/soapy_cake/helper.rb, line 38 def translate_values(opts) opts.map do |k, 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
validate_id(opts, key)
click to toggle source
# File lib/soapy_cake/helper.rb, line 18 def validate_id(opts, key) raise Error, "Parameter '#{key}' must be > 0!" if opts[key].to_i < 1 end
walk_tree(obj, key = nil) { |obj, key| ... }
click to toggle source
# File lib/soapy_cake/helper.rb, line 5 def walk_tree(obj, key = nil, &block) return nil if obj == {} case obj when Hash, Saxerator::Builder::HashElement obj.map { |hk, hv| [hk, walk_tree(hv, hk, &block)] }.to_h when Array, Saxerator::Builder::ArrayElement obj.map { |av| walk_tree(av, &block) } else yield(obj, key) end end