Class: Codebot::NetworkManager

Inherits:
Object
  • Object
show all
Defined in:
lib/codebot/network_manager.rb

Overview

This class manages the networks associated with a configuration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ NetworkManager

Constructs a new network manager for a specified configuration.

Parameters:

  • config (Config)

    the configuration to manage



14
15
16
# File 'lib/codebot/network_manager.rb', line 14

def initialize(config)
  @config = config
end

Instance Attribute Details

#configConfig (readonly)

Returns the configuration managed by this class.

Returns:

  • (Config)

    the configuration managed by this class



9
10
11
# File 'lib/codebot/network_manager.rb', line 9

def config
  @config
end

Instance Method Details

#check_channels!(integration) ⇒ Object

Checks that all channels associated with an integration belong to a valid network.

Parameters:

  • integration (Integration)

    the integration to check



97
98
99
100
101
# File 'lib/codebot/network_manager.rb', line 97

def check_channels!(integration)
  integration.channels.map(&:network).map(&:name).each do |network|
    find_network!(network)
  end
end

#create(params) ⇒ Object

Creates a new network from the given parameters.

Parameters:

  • params (Hash)

    the parameters to initialize the network with



21
22
23
24
25
26
27
28
# File 'lib/codebot/network_manager.rb', line 21

def create(params)
  network = Network.new(params.merge(config: {}))
  @config.transaction do
    check_name_available!(network.name)
    @config.networks << network
    network_feedback(network, :created) unless params[:quiet]
  end
end

#destroy(name, params) ⇒ Object

Destroys a network.

Parameters:

  • name (String)

    the name of the network to destroy

  • params (Hash)

    the command-line options



49
50
51
52
53
54
55
# File 'lib/codebot/network_manager.rb', line 49

def destroy(name, params)
  @config.transaction do
    network = find_network!(name)
    @config.networks.delete network
    network_feedback(network, :destroyed) unless params[:quiet]
  end
end

#find_network(name) ⇒ Network?

Finds a network given its name.

Parameters:

  • name (String)

    the name to search for

Returns:

  • (Network, nil)

    the network, or nil if none was found



76
77
78
# File 'lib/codebot/network_manager.rb', line 76

def find_network(name)
  @config.networks.find { |net| net.name_eql? name }
end

#find_network!(name) ⇒ Network

Finds a network given its name.

Parameters:

  • name (String)

    the name to search for

Returns:

Raises:

  • (CommandError)

    if no network with the given name exists



85
86
87
88
89
90
91
# File 'lib/codebot/network_manager.rb', line 85

def find_network!(name)
  network = find_network(name)
  return network unless network.nil?

  raise CommandError, "a network with the name #{name.inspect} " \
                      'does not exist'
end

#list(search) ⇒ Object

Lists all networks, or networks with names containing the given search term.

Parameters:

  • search (String, nil)

    an optional search term



61
62
63
64
65
66
67
68
69
70
# File 'lib/codebot/network_manager.rb', line 61

def list(search)
  @config.transaction do
    networks = @config.networks.dup
    unless search.nil?
      networks.select! { |net| net.name.downcase.include? search.downcase }
    end
    puts 'No networks found' if networks.empty?
    networks.each { |net| show_network net }
  end
end

#update(name, params) ⇒ Object

Updates a network with the given parameters.

Parameters:

  • name (String)

    the current name of the network to update

  • params (Hash)

    the parameters to update the network with



34
35
36
37
38
39
40
41
42
43
# File 'lib/codebot/network_manager.rb', line 34

def update(name, params)
  @config.transaction do
    network = find_network!(name)
    unless params[:name].nil?
      check_name_available_except!(params[:name], network)
    end
    network.update!(params)
    network_feedback(network, :updated) unless params[:quiet]
  end
end