Module: KnuVerse::Knufactor::Validations::APIClient

Included in:
APIClientBase
Defined in:
lib/knuverse/knufactor/validations/api_client.rb

Overview

APIClient validation methods

Instance Method Summary collapse

Instance Method Details

#validate_creds(opts, exception) ⇒ Object

Raises:

  • (exception)


6
7
8
# File 'lib/knuverse/knufactor/validations/api_client.rb', line 6

def validate_creds(opts, exception)
  raise(exception, 'Must Provide Auth') unless opts.key?(:apikey) && opts.key?(:secret)
end

#validate_opts(opts) ⇒ Object

Validate the options passed on initialize

Raises:

  • (e)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/knuverse/knufactor/validations/api_client.rb', line 11

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



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/knuverse/knufactor/validations/api_client.rb', line 28

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.class == URI::HTTP || u.class == URI::HTTPS
  rescue URI::InvalidURIError
    valid = false
  end
  valid || raise(Exceptions::InvalidOptions, 'Invalid server')
end