class Controls::Client

A class that handles interactions with the **controls**insight API

Constants

SSL_WARNING

A few messages to show the user of Controls::Client in the case that a bad certificate is encountered

Attributes

_last_request[R]

Public Class Methods

new(options = {}) click to toggle source

Creates a new {Controls::Client} object

@param [Hash] options the options to use when adding keys from

{Controls::Configurable}
# File lib/controls/client.rb, line 49
def initialize(options = {})
  Controls::Configurable.keys.each do |key|
    value = options[key].nil? ? Controls.instance_variable_get(:"@#{key}") : options[key]
    instance_variable_set(:"@#{key}", value)
  end

  if options[:verify_ssl].nil?
    middleware.ssl[:verify] = if ENV['CONTROLS_VERIFY_SSL'].nil?
                                true
                              else
                                !(ENV['CONTROLS_VERIFY_SSL'] =~ /false/)
                              end
  else
    middleware.ssl[:verify] = !!options[:verify_ssl]
  end

  login_from_netrc unless authenticated?

  if basic_authenticated?
    middleware.basic_auth(@username, @password)
  end
end

Public Instance Methods

api_methods() click to toggle source

A list of methods for API connections available to the {Controls::Client}

@note Any methods defined in a child module will be returned. @return [Array<Symbol>] the methods defined in {Controls::Client} that are API related

# File lib/controls/client.rb, line 151
def api_methods
  mods = Controls::Client.included_modules.map do |mod|
    if mod.to_s =~ /^Controls::Client::/
      mod
    end
  end

  mods.compact.map { |mod| mod.instance_methods(false) }.flatten.sort
end
exception(message = "HTTP Error") click to toggle source

Creates an error from the last request

@param [String] message the message to prepend to the response code/status @return [Controls::Error] the generated error message

# File lib/controls/client.rb, line 226
def exception(message = "HTTP Error")
  last_request = _last_request
  if last_request
    message << ": #{last_request[:response].status} #{Rack::Utils::HTTP_STATUS_CODES[last_request[:response].status]} #{last_request[:path]}"
  else
    message = 'Unknown error'
  end

  Controls::Error.new(message)
end
get(path, params = {}, headers = {}) click to toggle source

A wrapper for GET requests

@return [Array,Hash] an array or hash of parsed JSON data

# File lib/controls/client.rb, line 97
def get(path, params = {}, headers = {})
  headers = connection_options[:headers].merge(headers)
  url = URI.escape(File.join(api_endpoint, path))
  resp = middleware.get(url, params, headers)
  @_last_request = {
    response: resp,
    path: path
  }

  if !resp.headers['content-type'] =~ /^application\/json/
    fail exception('Invalid content-type error')
  end

  Response.parse(resp.body, resp.status, path)
rescue Faraday::Error::ConnectionFailed => e
  if e.message =~ /^SSL_connect/
    warn(*SSL_WARNING)
  else
    raise e
  end
end
inspect() click to toggle source

Censors the password from the output of {#inspect}

@return [String] the censored data

Calls superclass method
# File lib/controls/client.rb, line 87
def inspect
  raw = super
  raw.sub!(/(@password=")#{@password}(")/, "\\1*********\\2") if @password

  raw
end
put(path, body = {}, headers = {}, &block) click to toggle source

A wrapper for PUT requests

@return [Array,Hash] an array or hash of parsed JSON data

# File lib/controls/client.rb, line 122
def put(path, body = {}, headers = {}, &block)
  headers = connection_options[:headers].merge(headers)
  headers['content-type'] = 'application/json'
  url = URI.escape(File.join(api_endpoint, path))
  resp = middleware.put(url, body, headers, &block)
  @_last_request = {
    response: resp,
    path: path
  }

  if !resp.headers['content-type'] =~ /^application\/json/
    fail exception('Invalid content-type error')
  end

  return resp.status if resp.status == 200

  Response.parse(resp.body, resp.status, path)
rescue Faraday::Error::ConnectionFailed => e
  if e.message =~ /^SSL_connect/
    warn(*SSL_WARNING)
  else
    raise e
  end
end
references(version = '1.0') click to toggle source

A set of references from the “documentation” API endpoint /api

@param [String] version the API version to collect documentation from

# File lib/controls/client.rb, line 164
def references(version = '1.0')
  version = '1.0' unless version =~ /\d.\d/
  web_get "/api/#{version}"
rescue Faraday::Error::ConnectionFailed => e
  if e.message =~ /^SSL_connect/
    warn(*SSL_WARNING)
  else
    raise e
  end
end
same_options?(opts) click to toggle source

Compares {#options} or with the given options hash

@param [Hash] opts whether the options are the same or different @return whether the options are the same or different

# File lib/controls/client.rb, line 179
def same_options?(opts)
  opts.hash.eql? options.hash
end
verify_ssl() click to toggle source

Whether the middleware is currently set to verify SSL connections

# File lib/controls/client.rb, line 73
def verify_ssl
  middleware.ssl[:verify].nil? || !!middleware.ssl[:verify]
end
verify_ssl=(verify) click to toggle source

Sets the middleware to to verify the SSL on true, or disregard it on false

@param [Boolean] verify whether to verify SSL or not

# File lib/controls/client.rb, line 80
def verify_ssl=(verify)
  middleware.ssl[:verify] = !!verify
end
web_get(path, params = {}, headers = {}) click to toggle source

A wrapper for GET requests to the Controls endpoint root (web endpoint)

@return [Array,Hash] an array or hash of parsed JSON data

# File lib/controls/client.rb, line 200
def web_get(path, params = {}, headers = {})
  headers = connection_options[:headers].merge(headers)
  url = URI.escape(File.join(web_endpoint, path))
  resp = middleware.get(url, params, headers)
  @_last_request = {
    response: resp,
    path: path
  }

  if !resp.headers['content-type'] =~ /^application\/json/
    fail exception('Invalid content-type error')
  end

  JSON.parse(resp.body)
rescue Faraday::Error::ConnectionFailed => e
  if e.message =~ /^SSL_connect/
    warn(*SSL_WARNING)
  else
    raise e
  end
end