Class: FlowChat::Ussd::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/flow_chat/ussd/prompt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Prompt

Returns a new instance of Prompt.



6
7
8
# File 'lib/flow_chat/ussd/prompt.rb', line 6

def initialize(input)
  @user_input = input
end

Instance Attribute Details

#user_inputObject (readonly)

Returns the value of attribute user_input.



4
5
6
# File 'lib/flow_chat/ussd/prompt.rb', line 4

def user_input
  @user_input
end

Instance Method Details

#ask(msg, choices: nil, convert: nil, validate: nil, transform: nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/flow_chat/ussd/prompt.rb', line 10

def ask(msg, choices: nil, convert: nil, validate: nil, transform: nil)
  if user_input.present?
    input = user_input
    input = convert.call(input) if convert.present?
    validation_error = validate.call(input) if validate.present?

    prompt!([validation_error, msg].join("\n\n"), choices:) if validation_error.present?

    input = transform.call(input) if transform.present?
    return input
  end

  prompt! msg, choices:
end

#say(message) ⇒ Object



25
26
27
# File 'lib/flow_chat/ussd/prompt.rb', line 25

def say(message)
  terminate! message
end

#select(msg, choices) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/flow_chat/ussd/prompt.rb', line 29

def select(msg, choices)
  choices, choices_prompt = build_select_choices choices
  ask(
    msg,
    choices: choices_prompt,
    convert: lambda { |choice| choice.to_i },
    validate: lambda { |choice| "Invalid selection:" unless (1..choices.size).include?(choice) },
    transform: lambda { |choice| choices[choice - 1] }
  )
end

#yes?(msg) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/flow_chat/ussd/prompt.rb', line 40

def yes?(msg)
  select(msg, ["Yes", "No"]) == "Yes"
end