Class: ApnClient::NamedArgs

Inherits:
Object
  • Object
show all
Defined in:
lib/apn_client/named_args.rb

Class Method Summary collapse

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, options)
  arguments ||= {}
  options[:optional] ||= []
  options[:required] ||= []
  assert_allowed!(arguments, options[:optional] + options[:required])
  assert_present!(arguments, options[: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