Class: Telegem::Markup::Keyboard

Inherits:
Object
  • Object
show all
Defined in:
lib/markup/keyboard.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buttons = [], **options) ⇒ Keyboard



6
7
8
9
10
11
12
13
# File 'lib/markup/keyboard.rb', line 6

def initialize(buttons = [], **options)
  @buttons = buttons
  @options = {
    resize_keyboard: true,
    one_time_keyboard: false,
    selective: false
  }.merge(options)
end

Instance Attribute Details

#buttonsObject (readonly)

Returns the value of attribute buttons.



4
5
6
# File 'lib/markup/keyboard.rb', line 4

def buttons
  @buttons
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/markup/keyboard.rb', line 4

def options
  @options
end

Class Method Details

.[](*rows) ⇒ Object

Create from array



16
17
18
# File 'lib/markup/keyboard.rb', line 16

def self.[](*rows)
  new(rows)
end

.build(&block) ⇒ Object

Builder pattern



21
22
23
24
25
# File 'lib/markup/keyboard.rb', line 21

def self.build(&block)
  builder = Builder.new
  builder.instance_eval(&block) if block_given?
  builder.keyboard
end

.force_reply(selective: false, input_field_placeholder: nil) ⇒ Object

Force reply



82
83
84
85
86
87
88
89
# File 'lib/markup/keyboard.rb', line 82

def self.force_reply(selective: false, input_field_placeholder: nil)
  markup = {
    force_reply: true,
    selective: selective
  }
  markup[:input_field_placeholder] = input_field_placeholder if input_field_placeholder
  markup
end

.remove(selective: false) ⇒ Object

Remove keyboard



74
75
76
77
78
79
# File 'lib/markup/keyboard.rb', line 74

def self.remove(selective: false)
  {
    remove_keyboard: true,
    selective: selective
  }
end

Instance Method Details

#button(text, **options) ⇒ Object

Add a button



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

def button(text, **options)
  last_row = @buttons.last || []

  if last_row.is_a?(Array)
    last_row << { text: text }.merge(options)
  else
    @buttons << [{ text: text }.merge(options)]
  end
  self
end

#one_time(one_time = true) ⇒ Object



51
52
53
54
# File 'lib/markup/keyboard.rb', line 51

def one_time(one_time = true)
  @options[:one_time_keyboard] = one_time
  self
end

#resize(resize = true) ⇒ Object

Chainable options



46
47
48
49
# File 'lib/markup/keyboard.rb', line 46

def resize(resize = true)
  @options[:resize_keyboard] = resize
  self
end

#row(*buttons) ⇒ Object

Add a row



28
29
30
31
# File 'lib/markup/keyboard.rb', line 28

def row(*buttons)
  @buttons << buttons.flatten
  self
end

#selective(selective = true) ⇒ Object



56
57
58
59
# File 'lib/markup/keyboard.rb', line 56

def selective(selective = true)
  @options[:selective] = selective
  self
end

#to_hObject

Convert to Telegram format



62
63
64
65
66
67
# File 'lib/markup/keyboard.rb', line 62

def to_h
  {
    keyboard: @buttons.map { |row| row.is_a?(Array) ? row : [row] },
    **@options
  }
end

#to_json(*args) ⇒ Object



69
70
71
# File 'lib/markup/keyboard.rb', line 69

def to_json(*args)
  to_h.to_json(*args)
end