Module: SkullIsland::Validations::APIClient

Included in:
APIClientBase
Defined in:
lib/skull_island/validations/api_client.rb

Overview

APIClient validation methods

Instance Method Summary collapse

Instance Method Details

#validate_creds(opts, exception) ⇒ Object

Raises:

  • (exception)


7
8
9
10
# File 'lib/skull_island/validations/api_client.rb', line 7

def validate_creds(opts, exception)
  auto_opts_size = [opts[:username], opts[:password]].compact.size
  raise(exception, 'Invalid Auth Provided') if auto_opts_size == 1
end

#validate_opts(opts) ⇒ Object

Validate the options passed on initialize

Raises:

  • (e)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/skull_island/validations/api_client.rb', line 13

def validate_opts(opts)
  e = Exceptions::InvalidOptions
  raise(e, 'Is not Hash-like') unless opts.respond_to?(:[]) && opts.respond_to?(:key?)

  validate_server(opts[:server])
  if opts.key?(:base_uri)
    begin
      URI.parse(opts[:base_uri])
    rescue URI::InvalidURIError => e
      raise(e, "Invalid base_uri: #{e}")
    end
  end

  validate_creds(opts, e)
  true
end

#validate_server(name) ⇒ Object

Validate the server name provided



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/skull_island/validations/api_client.rb', line 31

def validate_server(name)
  return true unless name # if no name is provided, just go with the defaults

  valid = name.is_a? String
  begin
    u = URI.parse(name)
    valid = false unless u.is_a?(URI::HTTP) || u.is_a?(URI::HTTPS)
  rescue URI::InvalidURIError
    valid = false
  end
  valid || raise(Exceptions::InvalidOptions, 'Invalid server')
end