Class: Zoom::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/zoom/utils.rb

Class Method Summary collapse

Class Method Details

.argument_error(name) ⇒ Object



6
7
8
# File 'lib/zoom/utils.rb', line 6

def argument_error(name)
  name ? ArgumentError.new("You must provide #{name}") : ArgumentError.new
end

.define_bang_methods(klass) ⇒ Object

Dynamically defines bang methods for Actions modules



45
46
47
48
49
50
51
52
# File 'lib/zoom/utils.rb', line 45

def define_bang_methods(klass)
  klass.instance_methods.each do |m|
    klass.send(:define_method, "#{m}!") do |*args|
      response = send(m, *args)
      Utils.raise_if_error!(response)
    end
  end
end

.exclude_argument_error(name) ⇒ Object



10
11
12
# File 'lib/zoom/utils.rb', line 10

def exclude_argument_error(name)
  name ? ArgumentError.new("Unrecognized parameter #{name}") : ArgumentError.new
end

.extract_options!(array) ⇒ Object



54
55
56
# File 'lib/zoom/utils.rb', line 54

def extract_options!(array)
  array.last.is_a?(::Hash) ? array.pop : {}
end

.parse_response(http_response) ⇒ Object



25
26
27
28
# File 'lib/zoom/utils.rb', line 25

def parse_response(http_response)
  response = http_response.parsed_response
  response.nil? ? http_response.code : JSON.parse(response)
end

.permit_params(params, options) ⇒ Object



37
38
39
40
41
42
# File 'lib/zoom/utils.rb', line 37

def permit_params(params, options)
  params = [params] unless params.is_a? Array
  options.keys.each do |key|
    raise exclude_argument_error(key.to_s) unless params.include?(key)
  end
end

.process_datetime_params!(params, options) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/zoom/utils.rb', line 63

def process_datetime_params!(params, options)
  params = [params] unless params.is_a? Array
  params.each do |param|
    if options[param] && options[param].kind_of?(Time)
      options[param] = options[param].strftime('%FT%TZ')
    end
  end
  options
end

.raise_if_error!(response) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/zoom/utils.rb', line 14

def raise_if_error!(response)
  if response['code'] == 300
    error_hash = { base: response['message']}
    raise Error, error_hash unless response['errors']
    error_hash[response['message']] = response['errors']
    raise Error, error_hash
  else
    response
  end
end

.require_params(params, options) ⇒ Object



30
31
32
33
34
35
# File 'lib/zoom/utils.rb', line 30

def require_params(params, options)
  params = [params] unless params.is_a? Array
  params.each do |param|
    raise argument_error(param.to_s) unless options[param]
  end
end

.validate_password(password) ⇒ Object

Raises:



58
59
60
61
# File 'lib/zoom/utils.rb', line 58

def validate_password(password)
  password_regex = /\A[a-zA-Z0-9@-_*]{0,10}\z/
  raise(Error , 'Invalid Password') unless password[password_regex].nil?
end