Class: Adhearsion::HostDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/adhearsion/host_definitions.rb

Overview

This class isn’t yet tied into Adhearsion.

Defined Under Namespace

Classes: HostDefinitionException

Constant Summary collapse

SUPPORTED_KEYS =
[:host, :username, :password, :key, :name]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ HostDefinition

Returns a new instance of HostDefinition.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/adhearsion/host_definitions.rb', line 46

def initialize(hash)
  @host, @username, @password, @key, @name = hash.values_at(*SUPPORTED_KEYS)
  @name ||= new_guid
  
  unrecognized_keys = hash.keys - SUPPORTED_KEYS
  raise HostDefinitionException, "Unrecognized key(s): #{unrecognized_keys.map(&:inspect).to_sentence}" if unrecognized_keys.any? 
  raise HostDefinitionException, "You must supply a password or key!" if username && !(password || key)
  raise HostDefinitionException, "You must supply a username!" unless username
  raise HostDefinitionException, 'You cannot supply both a password and key!' if password && key
  raise HostDefinitionException, 'You must supply a host!' unless host
  
  self.class.definitions << self
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



45
46
47
# File 'lib/adhearsion/host_definitions.rb', line 45

def host
  @host
end

#keyObject (readonly)

Returns the value of attribute key.



45
46
47
# File 'lib/adhearsion/host_definitions.rb', line 45

def key
  @key
end

#nameObject (readonly)

Returns the value of attribute name.



45
46
47
# File 'lib/adhearsion/host_definitions.rb', line 45

def name
  @name
end

#passwordObject (readonly)

Returns the value of attribute password.



45
46
47
# File 'lib/adhearsion/host_definitions.rb', line 45

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



45
46
47
# File 'lib/adhearsion/host_definitions.rb', line 45

def username
  @username
end

Class Method Details

.clear_definitions!Object



40
41
42
# File 'lib/adhearsion/host_definitions.rb', line 40

def clear_definitions!
  definitions.clear
end

.import_from_data_structure(local_definitions) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/adhearsion/host_definitions.rb', line 16

def import_from_data_structure(local_definitions)
  case local_definitions
    when Array
      local_definitions.each do |definition|
        raise HostDefinitionException, "Unrecognized definition: #{definition}" unless definition.is_a?(Hash)
      end
      local_definitions.map { |definition| new definition }
    when Hash
      local_definitions.map do |(name,definition)|
        new definition.merge(:name => name)
      end
    else
      raise HostDefinitionException, "Unrecognized definition #{local_definitions}"
  end
end

.import_from_yaml(yaml_string) ⇒ Object



32
33
34
# File 'lib/adhearsion/host_definitions.rb', line 32

def import_from_yaml(yaml_string)
  import_from_data_structure YAML.load(yaml_string)
end

.import_from_yaml_file(file) ⇒ Object



36
37
38
# File 'lib/adhearsion/host_definitions.rb', line 36

def import_from_yaml_file(file)
  import_from_yaml YAML.load_file(file)
end