Class: Codebot::IntegrationManager

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

Overview

This class manages the integrations associated with a configuration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ IntegrationManager

Constructs a new integration manager for a specified configuration.

Parameters:

  • config (Config)

    the configuration to manage



15
16
17
# File 'lib/codebot/integration_manager.rb', line 15

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



10
11
12
# File 'lib/codebot/integration_manager.rb', line 10

def config
  @config
end

Instance Method Details

#create(params) ⇒ Object

Creates a new integration from the given parameters.

Parameters:

  • params (Hash)

    the parameters to initialize the integration with



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/codebot/integration_manager.rb', line 22

def create(params)
  integration = Integration.new(
    params.merge(config: { networks: @config.networks })
  )
  @config.transaction do
    check_available!(integration.name, integration.endpoint)
    NetworkManager.new(@config).check_channels!(integration)
    @config.integrations << integration
    integration_feedback(integration, :created) unless params[:quiet]
  end
end

#destroy(name, params) ⇒ Object

Destroys an integration.

Parameters:

  • name (String)

    the name of the integration to destroy

  • params (Hash)

    the command-line options



53
54
55
56
57
58
59
# File 'lib/codebot/integration_manager.rb', line 53

def destroy(name, params)
  @config.transaction do
    integration = find_integration!(name)
    @config.integrations.delete integration
    integration_feedback(integration, :destroyed) unless params[:quiet]
  end
end

#find_integration(name) ⇒ Integration?

Finds an integration given its name.

Parameters:

  • name (String)

    the name to search for

Returns:

  • (Integration, nil)

    the integration, or nil if none was found



82
83
84
# File 'lib/codebot/integration_manager.rb', line 82

def find_integration(name)
  @config.integrations.find { |intg| intg.name_eql? name }
end

#find_integration!(name) ⇒ Integration

Finds an integration given its name.

Parameters:

  • name (String)

    the name to search for

Returns:

Raises:

  • (CommandError)

    if no integration with the given name exists



99
100
101
102
103
104
105
# File 'lib/codebot/integration_manager.rb', line 99

def find_integration!(name)
  integration = find_integration(name)
  return integration unless integration.nil?

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

#find_integration_by_endpoint(endpoint) ⇒ Integration?

Finds an integration given its endpoint.

Parameters:

  • endpoint (String)

    the endpoint to search for

Returns:

  • (Integration, nil)

    the integration, or nil if none was found



90
91
92
# File 'lib/codebot/integration_manager.rb', line 90

def find_integration_by_endpoint(endpoint)
  @config.integrations.find { |intg| intg.endpoint_eql? endpoint }
end

#list(search) ⇒ Object

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

Parameters:

  • search (String, nil)

    an optional search term



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/codebot/integration_manager.rb', line 65

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

#update(name, params) ⇒ Object

Updates an integration with the given parameters.

Parameters:

  • name (String)

    the current name of the integration to update

  • params (Hash)

    the parameters to update the integration with



38
39
40
41
42
43
44
45
46
47
# File 'lib/codebot/integration_manager.rb', line 38

def update(name, params)
  @config.transaction do
    integration = find_integration!(name)
    check_available_except!(params[:name], params[:endpoint], integration)
    update_channels!(integration, params)
    NetworkManager.new(@config).check_channels!(integration)
    integration.update!(params)
    integration_feedback(integration, :updated) unless params[:quiet]
  end
end