Module: SoapyCake::Helper
- Included in:
- Admin, AdminAddedit, AdminTrack, Response
- Defined in:
- lib/soapy_cake/helper.rb
Instance Method Summary collapse
- #const_lookup(type, key) ⇒ Object
-
#future_expiration_date ⇒ Object
Some API calls require expiration dates.
- #require_params(opts, params) ⇒ Object
- #translate_booleans(opts) ⇒ Object
- #translate_values(opts, params) ⇒ Object
- #validate_id(opts, key) ⇒ Object
- #walk_tree(obj, key = nil, &block) ⇒ Object
Instance Method Details
#const_lookup(type, key) ⇒ Object
46 47 48 49 50 |
# File 'lib/soapy_cake/helper.rb', line 46 def const_lookup(type, key) Const::CONSTS[type].fetch(key) do raise ArgumentError, "#{key} is not a valid value for #{type}" end end |
#future_expiration_date ⇒ Object
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.
56 57 58 |
# File 'lib/soapy_cake/helper.rb', line 56 def future_expiration_date Date.today + (365 * 30) end |
#require_params(opts, params) ⇒ Object
21 22 23 24 25 |
# File 'lib/soapy_cake/helper.rb', line 21 def require_params(opts, params) params.each do |param| raise Error, "Parameter '#{param}' missing!" unless opts.key?(param) end end |
#translate_booleans(opts) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/soapy_cake/helper.rb', line 27 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, params) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/soapy_cake/helper.rb', line 37 def translate_values(opts, params) opts.map do |k, v| [ k, params.include?(k) ? const_lookup(k, v) : v ] end.to_h end |
#validate_id(opts, key) ⇒ Object
17 18 19 |
# File 'lib/soapy_cake/helper.rb', line 17 def validate_id(opts, key) raise Error, "Parameter '#{key}' must be > 0!" if opts[key].to_i < 1 end |
#walk_tree(obj, key = nil, &block) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/soapy_cake/helper.rb', line 4 def walk_tree(obj, key = nil, &block) return nil if obj == {} case obj when Hash obj.map { |hk, hv| [hk, walk_tree(hv, hk, &block)] }.to_h when Array obj.map { |av| walk_tree(av, &block) } else yield(obj, key) end end |