Class: ApnClient::NamedArgs
- Inherits:
-
Object
- Object
- ApnClient::NamedArgs
- Defined in:
- lib/apn_client/named_args.rb
Class Method Summary collapse
- .assert_allowed!(arguments, allowed_keys) ⇒ Object
- .assert_present!(arguments, required_keys) ⇒ Object
- .assert_valid!(arguments, options) ⇒ Object
-
.symbolize_keys!(arguments) ⇒ Object
Destructively convert all keys to symbols, as long as they respond to
to_sym
.
Class Method Details
.assert_allowed!(arguments, allowed_keys) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/apn_client/named_args.rb', line 11 def self.assert_allowed!(arguments, allowed_keys) invalid_keys = arguments.keys.select { |key| !allowed_keys.include?(key) } unless invalid_keys.empty? raise "Invalid arguments: #{invalid_keys.join(', ')}. Must be one of #{allowed_keys.join(', ')}" end end |
.assert_present!(arguments, required_keys) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/apn_client/named_args.rb', line 18 def self.assert_present!(arguments, required_keys) missing_keys = required_keys.select { |key| !arguments.keys.include?(key) } unless missing_keys.empty? raise "Missing required arguments: #{missing_keys.join(', ')}" end end |
.assert_valid!(arguments, options) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/apn_client/named_args.rb', line 3 def self.assert_valid!(arguments, ) arguments ||= {} [:optional] ||= [] [:required] ||= [] assert_allowed!(arguments, [:optional] + [:required]) assert_present!(arguments, [:required]) end |
.symbolize_keys!(arguments) ⇒ Object
Destructively convert all keys to symbols, as long as they respond to to_sym
.
27 28 29 30 31 32 |
# File 'lib/apn_client/named_args.rb', line 27 def self.symbolize_keys!(arguments) arguments.keys.each do |key| arguments[(key.to_sym rescue key) || key] = arguments.delete(key) end arguments end |