Module: Controls::Default

Defined in:
lib/controls/default.rb

Overview

Default options merged with environment specific overrides to satisfy the options specified in the Configurable module

Constant Summary collapse

API_VERSION =

Returns the API version to connect to. default: 1.0.

Returns:

  • (String)

    the API version to connect to. default: 1.0

Since:

  • API v1.0.0

'1.0'.freeze
API_ENDPOINT =

Returns the API endpoint to connect to. default: nexpose.local:3780/insight/controls/api/1.0.

Returns:

Since:

  • API v1.0.0

"https://localhost:3780/insight/controls/api/#{API_VERSION}".freeze
MEDIA_TYPE =

Returns the default media type to send with requests. default: application/json.

Returns:

  • (String)

    the default media type to send with requests. default: application/json

Since:

  • API v1.0.0

'application/json'
USER_AGENT =

Returns the user agent to send with API requests. example: “controls/v1.0.0.beta (ruby; 2.0.0p247; [x86_64-darwin12.4.0]; Faraday v0.8.8)”.

Returns:

  • (String)

    the user agent to send with API requests. example: “controls/v1.0.0.beta (ruby; 2.0.0p247; [x86_64-darwin12.4.0]; Faraday v0.8.8)”

"controls/v#{Controls::VERSION} (#{(RUBY_DESCRIPTION.split[0..1] + [RUBY_DESCRIPTION.split.last]).join('; ')}; Faraday v#{Faraday::VERSION})".freeze
WEB_ENDPOINT =

Returns the web endpoint to connect to. default: nexpose.local:3780/insight/controls.

Returns:

'https://localhost:3780/insight/controls'.freeze

Class Method Summary collapse

Class Method Details

.api_endpointString

Returns the API endpoint’s URI as a URL.

Returns:

  • (String)

    the API endpoint’s URI as a URL



32
33
34
35
36
# File 'lib/controls/default.rb', line 32

def api_endpoint
  endpoint = ENV['CONTROLS_API_ENDPOINT'] || API_ENDPOINT
  # [todo] - this raises an exception, it is only used for URI validation so it's being commented out for now
  # URI.parse(endpoint).to_s
end

.api_versionString

Returns the API version to connect to.

Returns:

  • (String)

    the API version to connect to



39
40
41
42
43
44
45
# File 'lib/controls/default.rb', line 39

def api_version
  if ENV['CONTROLS_API_VERSION'].to_s =~ /\d+.\d+/
    ENV['CONTROLS_API_VERSION']
  else
    API_VERSION
  end
end

.connection_optionsHash

Returns the current connection options (headers, etc.).

Returns:

  • (Hash)

    the current connection options (headers, etc.)



48
49
50
51
52
53
54
55
# File 'lib/controls/default.rb', line 48

def connection_options
  {
    headers: {
      accept: default_media_type,
      user_agent: user_agent
    }
  }
end

.default_media_typeString

Returns the environment specific default media type. default: MEDIA_TYPE.

Returns:

  • (String)

    the environment specific default media type. default: MEDIA_TYPE



59
60
61
# File 'lib/controls/default.rb', line 59

def default_media_type
  ENV['CONTROLS_MEDIA_TYPE'] || MEDIA_TYPE
end

.middlewareFaraday::Connection

REVIEW: Ensure that middleware is unique to the client instance

Returns:

  • (Faraday::Connection)

    the middleware used to send requests



65
66
67
68
69
70
# File 'lib/controls/default.rb', line 65

def middleware
  @middleware ||= Faraday.new(api_endpoint, connection_options) do |conn|
    conn.adapter Faraday.default_adapter
    conn.response :logger if ENV['CONTROLS_DEBUG']
  end
end

.netrcBoolean

Returns whether to fallback on authentication using the specified netrc file.

Returns:

  • (Boolean)

    whether to fallback on authentication using the specified netrc file



74
75
76
# File 'lib/controls/default.rb', line 74

def netrc
  ENV['CONTROLS_NETRC'] || false
end

.netrc_fileString

Returns the netrc file to use for authentication. default: ~/.netrc.

Returns:

  • (String)

    the netrc file to use for authentication. default: ~/.netrc



80
81
82
# File 'lib/controls/default.rb', line 80

def netrc_file
  ENV['CONTROLS_NETRC_FILE'] || File.join(Dir.home, '.netrc')
end

.optionsHash

Returns options as a Hash, mapped by keys from Configurable.

Returns:

  • (Hash)

    options as a Hash, mapped by keys from Configurable



27
28
29
# File 'lib/controls/default.rb', line 27

def options
  Hash[Controls::Configurable.keys.map { |key| [key, send(key)] }]
end

.passwordString

Returns the password to use for authentication.

Returns:

  • (String)

    the password to use for authentication



86
87
88
# File 'lib/controls/default.rb', line 86

def password
  ENV['CONTROLS_PASSWORD']
end

.user_agentString

Returns the user agent that will be sent along any requests sent using Configurable#connection_options.

Returns:



92
93
94
# File 'lib/controls/default.rb', line 92

def user_agent
  ENV['CONTROLS_USER_AGENT'] || USER_AGENT
end

.usernameString

Returns the username to use for authentication.

Returns:

  • (String)

    the username to use for authentication



97
98
99
# File 'lib/controls/default.rb', line 97

def username
  ENV['CONTROLS_USERNAME']
end

.web_endpointString

Returns the web endpoint’s URI as a URL.

Returns:

  • (String)

    the web endpoint’s URI as a URL



102
103
104
105
# File 'lib/controls/default.rb', line 102

def web_endpoint
  endpoint = ENV['CONTROLS_WEB_ENDPOINT'] || WEB_ENDPOINT
  URI.parse(endpoint).to_s
end