Class: Kanal::Plugins::Batteries::Keyboard

Inherits:
Object
  • Object
show all
Includes:
Core::Logging
Defined in:
lib/kanal/plugins/batteries/keyboard.rb

Overview

This class provides methods to construct and get keyboard. You can construct keyboard inside respond do block in router. There are two ways of constructing a keyboard

  1. Using #to_array method: provide 2-dimensional array of button names

  2. Using #build method: provide block with calls to DSL method #row with button names as arguments

Instance Method Summary collapse

Constructor Details

#initializeKeyboard

Returns a new instance of Keyboard.



18
19
20
# File 'lib/kanal/plugins/batteries/keyboard.rb', line 18

def initialize
  @layout = []
end

Instance Method Details

#build(&block) ⇒ Object

Use this method to pass block. Code inside block should call for DSL method row. Example below is two rows of button names keyboard.build do

row "B", "B2"
row "B3"

end

Parameters:

  • block (Proc)


65
66
67
# File 'lib/kanal/plugins/batteries/keyboard.rb', line 65

def build(&block)
  instance_eval &block
end

#from_array(array) ⇒ Object

Use this method to pass names for buttons in 2-dimensional array of strings. Each sub-array represents a row of button names. Example call below is two rows of button names: keyboard.from_array [[“B1”, “B2”], [“B3”]]

Parameters:

  • array (Array<Array<String>>)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kanal/plugins/batteries/keyboard.rb', line 29

def from_array(array)
  unless array.instance_of? Array
    logger.error "2-dimensional array was not provided. Type of provided object is #{array.class.name}"

    raise "2-dimensional array was not provided"
  end

  array.each do |sub_array|
    unless sub_array.instance_of? Array
      logger.error "Value provided as row is not of Array type. Type of provided object is #{sub_array.class.name}"

      raise "Value provided as row is not of Array type"
    end

    sub_array.each do |value|
      unless value.instance_of? String
        logger.error "Value provided as button name is not of String type. Type of provided object is #{sub_array.class.name}"

        raise "Value provided as button name is not of String type"
      end
    end
  end

  @layout = array
end

#row(*args) ⇒ Object

Creates a row of button names, takes each provided arg and adds it to a row

Parameters:

  • args (Array<String>)


74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kanal/plugins/batteries/keyboard.rb', line 74

def row(*args)
  args.each do |value|
    unless value.instance_of? String
      logger.error "Value provided as button name is not of String type. Type of provided object is #{sub_array.class.name}"

      raise "Value provided as button name is not of String type"
    end
  end

  @layout.append args
end

#to_aArray<Array<String>>

Returns constructed 2-dimensional array of strings. Each sub-array represents a row of button names.

Returns:

  • (Array<Array<String>>)


92
93
94
# File 'lib/kanal/plugins/batteries/keyboard.rb', line 92

def to_a
  @layout
end