Class: Pechkin::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/pechkin/configuration.rb

Overview

Pechkin reads its configuration from provided directory structure. Basic layout expected to be as follows:

.
| - bots/                  <= Bots configuration
|   | - marvin.yml         <= Each bot described by yaml file
|   | - bender.yml
|
| - channels/              <= Channels description
|   | - slack-repository-feed
|       | - commit-hg.yml
|       | - commit-svn.yml
|
| - views/                 <= Template storage
    | - commit-hg.erb
    | - commit-svn.erb

Bots

Bots described in YAML files in `bots` directory. Bot described by
following fields:
  - token - API token used to authorize when doing requests to messenger
            API
  - connector - Connector name to instantiate. For exapmle: 'telegram' or
    'slack'

Channels

Channel is a description of message group. It used to describe group of
messages that sould be send to sepceific channel or user. Each
channel configuration is stored in its own folder. This folder name
is channel internal id. Channel is described by `_channel.yml` file,
Channel has following fields to configure:
  - chat_ids - list of ids to send all containing messages. It may be
    single item or list of ids.
  - bot - bot istance to use when messages are handled.
Other `*.yml` files in channel folder are message descriptions. Message
description has following fields to configure:
  - template - path to template relative to views/ folder. If no template
    specified then noop template will be used. No-op template returns empty
    string for each render request.
  - variables - predefined variables to use in template rendering. This is
    especialy useful when one wants to use same template in different
    channels. For exapmle when you need to render repository commit and
    want to substitute correct repository link
  - filters - list of rules which allows to deny some messages based on
    their content. For example we do not want to post commit messages from
    branches other than `master`.

And other connector speceific fields. For example:
  - telegram_parse_mode
  - slack_attachments

Views

'views' folder contains erb templates to render when data arives.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(working_dir, bots, views, channels) ⇒ Configuration

Returns a new instance of Configuration.



76
77
78
79
80
81
# File 'lib/pechkin/configuration.rb', line 76

def initialize(working_dir, bots, views, channels)
  @working_dir = working_dir
  @bots = bots
  @views = views
  @channels = channels
end

Instance Attribute Details

#botsObject

Returns the value of attribute bots.



74
75
76
# File 'lib/pechkin/configuration.rb', line 74

def bots
  @bots
end

#channelsObject

Returns the value of attribute channels.



74
75
76
# File 'lib/pechkin/configuration.rb', line 74

def channels
  @channels
end

#viewsObject

Returns the value of attribute views.



74
75
76
# File 'lib/pechkin/configuration.rb', line 74

def views
  @views
end

#working_dirObject

Returns the value of attribute working_dir.



74
75
76
# File 'lib/pechkin/configuration.rb', line 74

def working_dir
  @working_dir
end

Class Method Details

.load_from_directory(working_dir) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/pechkin/configuration.rb', line 63

def load_from_directory(working_dir)
  bots = ConfigurationLoaderBots.new.load_from_directory(working_dir)
  views = ConfigurationLoaderViews.new.load_from_directory(working_dir)

  channel_loader = ConfigurationLoaderChannels.new(bots, views)
  channels = channel_loader.load_from_directory(working_dir)

  Configuration.new(working_dir, bots, views, channels)
end

Instance Method Details

#listObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pechkin/configuration.rb', line 83

def list
  puts "Working dir: #{working_dir}\nBots:"

  bots.each do |name, bot|
    puts "  #{name}(#{bot.connector}): #{bot.token}"
  end

  puts "\nChannels:"
  channels.each do |channel_name, channel|
    puts "  - name #{channel_name}"
    puts "    bot: #{channel.connector.name}"
    puts '    messages: '
    channel.messages.each do |message_name, _message|
      puts "     - /#{channel_name}/#{message_name}"
    end
    puts
  end
end