Class: Cyclid::Client::Config

Inherits:
Object
  • Object
show all
Includes:
AuthMethods
Defined in:
lib/cyclid/config.rb

Overview

Cyclid client per-organization configuration

Constant Summary

Constants included from AuthMethods

AuthMethods::AUTH_NONE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :path (String)

    Fully qualified path to the configuration file

  • :auth (FixNum)

    Authentication method

  • :username (String)

    Users username.

  • :secret (String)

    Users secret for use with HMAC authentication.

  • :password (String)

    Users password for use with HTTP Basic authentication.

  • :token (String)

    Users API token for use with Token authentication.

  • :server (String)

    API server address.

  • :port (Integer)

    API server port.

  • :organization (String)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cyclid/config.rb', line 67

def initialize(options = {})
  # Load the config if a path was provided
  @path = options[:path] || nil
  unless File.file?(@path)
    raise "no config found at #{@path} - Have you selected an organisation with " \
          '`cyclid organization use <name>`?'
  end
  @config = @path.nil? ? nil : YAML.load_file(@path)

  # Select the authentication type & associated authentication data.
  @auth = options[:auth] || AUTH_HMAC
  case @auth
  when AUTH_NONE
    # Nothing
    nil
  when AUTH_HMAC
    @secret = options[:secret] || @config['secret']
  when AUTH_BASIC
    @password = options[:password] || @config['password']
  when AUTH_TOKEN
    @token = options[:token] || @config['token']
  end

  # Set defaults from the options
  @url = options[:url] || nil
  @server = options[:server] || nil
  @port = options[:port] || nil
  @tls = options[:tls] || nil
  @ssl_verify_none = options[:ssl_verify_none] || nil
  @organization = options[:organization] || nil
  @username = options[:username] || nil

  # Get anything provided in the config file
  if @config
    @url ||= @config['url']
    @server ||= @config['server']
    @port ||= @config['port'] || 8361
    @tls ||= @config['tls'] || false
    @ssl_verify_none ||= @config['ssl_verify_none'] || false
    @organization ||= @config['organization']
    @username ||= @config['username']
  end

  # Parse the URL if one was given
  if @url
    uri = URI.parse(@url)

    # Items parsed from the uri always over-write any individual
    # configuration settings
    @server = uri.host
    @port = uri.port
    @tls = uri.scheme == 'https' ? true : false
  end

  # Server & Username *must* be set
  raise 'server address must be provided' if @server.nil?
  raise 'username must be provided' if @username.nil? and @auth != AUTH_NONE
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



25
26
27
# File 'lib/cyclid/config.rb', line 25

def auth
  @auth
end

#organizationObject (readonly)

Returns the value of attribute organization.



25
26
27
# File 'lib/cyclid/config.rb', line 25

def organization
  @organization
end

#passwordObject (readonly)

Returns the value of attribute password.



25
26
27
# File 'lib/cyclid/config.rb', line 25

def password
  @password
end

#pathObject (readonly)

Returns the value of attribute path.



25
26
27
# File 'lib/cyclid/config.rb', line 25

def path
  @path
end

#portObject (readonly)

Returns the value of attribute port.



25
26
27
# File 'lib/cyclid/config.rb', line 25

def port
  @port
end

#secretObject (readonly)

Returns the value of attribute secret.



25
26
27
# File 'lib/cyclid/config.rb', line 25

def secret
  @secret
end

#serverObject (readonly)

Returns the value of attribute server.



25
26
27
# File 'lib/cyclid/config.rb', line 25

def server
  @server
end

#ssl_verify_noneObject (readonly)

Returns the value of attribute ssl_verify_none.



25
26
27
# File 'lib/cyclid/config.rb', line 25

def ssl_verify_none
  @ssl_verify_none
end

#tlsObject (readonly)

Returns the value of attribute tls.



25
26
27
# File 'lib/cyclid/config.rb', line 25

def tls
  @tls
end

#tokenObject (readonly)

Returns the value of attribute token.



25
26
27
# File 'lib/cyclid/config.rb', line 25

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username.



25
26
27
# File 'lib/cyclid/config.rb', line 25

def username
  @username
end