Class: Kanal::Plugins::Batteries::BatteriesPlugin

Inherits:
Core::Plugins::Plugin show all
Defined in:
lib/kanal/plugins/batteries/batteries_plugin.rb

Overview

Plugin with some batteries like .body property etc

Instance Method Summary collapse

Methods inherited from Core::Plugins::Plugin

#rake_tasks

Instance Method Details

#attachments_batteries(core) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/kanal/plugins/batteries/batteries_plugin.rb', line 123

def attachments_batteries(core)
  core.register_input_parameter :image
  core.register_input_parameter :audio
  core.register_input_parameter :video
  core.register_input_parameter :document

  core.register_output_parameter :image
  core.register_output_parameter :audio
  core.register_output_parameter :video
  core.register_output_parameter :document

  _this = self

  core.add_condition_pack :image do
    add_condition :exists do
      met? do |input, _core, _argument|
        !input.image.nil?
      end
    end

    add_condition :is do
      with_argument

      met? do |input, _, argument|
        if input.image.image? && input.image.send((argument.to_s + "?").to_sym)
          true
        else
          false
        end
      end
    end
  end

  core.add_condition_pack :audio do
    add_condition :exists do
      met? do |input, _core, _argument|
        !input.audio.nil?
      end
    end

    add_condition :is do
      with_argument

      met? do |input, _, argument|
        if input.audio.audio? && input.audio.send((argument.to_s + "?").to_sym)
          true
        else
          false
        end
      end
    end
  end

  core.add_condition_pack :video do
    add_condition :exists do
      met? do |input, _core, _argument|
        !input.video.nil?
      end
    end

    add_condition :is do
      with_argument

      met? do |input, _, argument|
        if input.video.video? && input.video.send((argument.to_s + "?").to_sym)
          true
        else
          false
        end
      end
    end
  end

  core.add_condition_pack :document do
    add_condition :exists do
      met? do |input, _core, _argument|
        !input.document.nil?
      end
    end

    add_condition :is do
      with_argument

      met? do |input, _, argument|
        if input.document.document? && input.document.send((argument.to_s + "?").to_sym)
          true
        else
          false
        end
      end
    end
  end
end

#body_batteries(core) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
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
# File 'lib/kanal/plugins/batteries/batteries_plugin.rb', line 56

def body_batteries(core)
  core.register_input_parameter :body
  core.register_output_parameter :body

  core.add_condition_pack :body do
    add_condition :starts_with do
      with_argument

      met? do |input, _, argument|
        if input.body.is_a? String
          input.body.start_with? argument
        else
          false
        end
      end
    end

    add_condition :ends_with do
      with_argument

      met? do |input, _, argument|
        if input.body.is_a? String
          input.body.end_with? argument
        else
          false
        end
      end
    end

    add_condition :contains do
      with_argument

      met? do |input, _, argument|
        if input.body.is_a? String
          input.body.include? argument
        else
          false
        end
      end
    end

    add_condition :contains_one_of do
      with_argument

      met? do |input, _, argument|
        met = false

        argument.each do |word|
          met = input.body.include? word

          break if met
        end

        met
      end
    end

    add_condition :equals do
      with_argument

      met? do |input, _, argument|
        input.body == argument
      end
    end
  end
end

#button_batteries(core) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/kanal/plugins/batteries/batteries_plugin.rb', line 229

def button_batteries(core)
  core.register_input_parameter :button_pressed

  core.add_condition_pack :button do
    add_condition :pressed do
      with_argument

      met? do |input, _, argument|
        input.button_pressed == argument
      end
    end
  end
end

#flow_batteries(core) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/kanal/plugins/batteries/batteries_plugin.rb', line 30

def flow_batteries(core)
  core.add_condition_pack :flow do
    add_condition :any do
      met? do |_, _, _|
        true
      end
    end
  end
end

#keyboard_batteries(core) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/kanal/plugins/batteries/batteries_plugin.rb', line 217

def keyboard_batteries(core)
  core.register_output_parameter :keyboard

  core.hooks.attach :output_just_created do |input, output|
    output.keyboard = Keyboard.new
  end
end

#nameObject



15
16
17
# File 'lib/kanal/plugins/batteries/batteries_plugin.rb', line 15

def name
  :batteries
end

#setup(core) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/kanal/plugins/batteries/batteries_plugin.rb', line 19

def setup(core)
  source_batteries core
  body_batteries core
  flow_batteries core
  attachments_batteries core
  keyboard_batteries core
  username_batteries core
  button_batteries core
  specifics_batteries core
end

#source_batteries(core) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kanal/plugins/batteries/batteries_plugin.rb', line 40

def source_batteries(core)
  # This parameter can be filled by different plugins/interfaces
  # to point from which source message came
  core.register_input_parameter :source

  core.add_condition_pack :source do
    add_condition :from do
      with_argument

      met? do |input, _, argument|
        input.source == argument
      end
    end
  end
end

#specifics_batteries(core) ⇒ Object



243
244
245
246
247
248
249
# File 'lib/kanal/plugins/batteries/batteries_plugin.rb', line 243

def specifics_batteries(core)
  core.register_output_parameter :specifics

  core.hooks.attach :output_just_created do |input, output|
    output.specifics = Specifics.new
  end
end

#username_batteries(core) ⇒ Object



225
226
227
# File 'lib/kanal/plugins/batteries/batteries_plugin.rb', line 225

def username_batteries(core)
  core.register_input_parameter :username
end