Class: JoyUssdEngine::Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/joy_ussd_engine/menu.rb

Direct Known Subclasses

PaginateMenu

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Menu

Returns a new instance of Menu.



12
13
14
15
# File 'lib/joy_ussd_engine/menu.rb', line 12

def initialize(context)
    @context = context
    @current_client_state = @context.current_menu
end

Instance Attribute Details

#contextObject (readonly)

NOTE THIS CLASS SHOULD NEVER BE USED DIRECTLY BUT RATHER BE TREATED AS AN ABSTRACT CLASS FROM WHICH OTHER CLASSES CAN INHERIT FROM AND IMPLEMENT CERTAIN CUSTOM BEHAVIOURS PERTAINING TO THEIR OPERATION



9
10
11
# File 'lib/joy_ussd_engine/menu.rb', line 9

def context
  @context
end

#current_client_stateObject

Returns the value of attribute current_client_state.



10
11
12
# File 'lib/joy_ussd_engine/menu.rb', line 10

def current_client_state
  @current_client_state
end

#error_textObject

Returns the value of attribute error_text.



10
11
12
# File 'lib/joy_ussd_engine/menu.rb', line 10

def error_text
  @error_text
end

#errorsObject

Returns the value of attribute errors.



10
11
12
# File 'lib/joy_ussd_engine/menu.rb', line 10

def errors
  @errors
end

#field_errorObject

Returns the value of attribute field_error.



10
11
12
# File 'lib/joy_ussd_engine/menu.rb', line 10

def field_error
  @field_error
end

#field_nameObject

Returns the value of attribute field_name.



10
11
12
# File 'lib/joy_ussd_engine/menu.rb', line 10

def field_name
  @field_name
end

Returns the value of attribute menu_error.



10
11
12
# File 'lib/joy_ussd_engine/menu.rb', line 10

def menu_error
  @menu_error
end

Returns the value of attribute menu_items.



10
11
12
# File 'lib/joy_ussd_engine/menu.rb', line 10

def menu_items
  @menu_items
end

Returns the value of attribute menu_text.



10
11
12
# File 'lib/joy_ussd_engine/menu.rb', line 10

def menu_text
  @menu_text
end

#previous_client_stateObject

Returns the value of attribute previous_client_state.



10
11
12
# File 'lib/joy_ussd_engine/menu.rb', line 10

def previous_client_state
  @previous_client_state
end

#previous_menuObject

Returns the value of attribute previous_menu.



10
11
12
# File 'lib/joy_ussd_engine/menu.rb', line 10

def previous_menu
  @previous_menu
end

#skip_saveObject

Returns the value of attribute skip_save.



10
11
12
# File 'lib/joy_ussd_engine/menu.rb', line 10

def skip_save
  @skip_save
end

Instance Method Details

#after_renderObject



99
100
101
102
# File 'lib/joy_ussd_engine/menu.rb', line 99

def after_render
    # Implement after call backs
    puts "after render passed"
end

#allow_validationObject



199
200
201
# File 'lib/joy_ussd_engine/menu.rb', line 199

def allow_validation
    !is_last_menu && !@previous_client_state.blank?
end

#before_renderObject



94
95
96
97
# File 'lib/joy_ussd_engine/menu.rb', line 94

def before_render
    # Implement before call backs
    puts "before render passed"
end

#before_show_menuObject



62
63
64
65
# File 'lib/joy_ussd_engine/menu.rb', line 62

def before_show_menu
    is_first_render = @context.get_state[:"#{@current_client_state}_show_menu_initiation"].blank?
    @context.set_state({"#{@current_client_state}_show_menu_initiation".to_sym => is_first_render ? "is_new" : "exists"})
end

#do_validationObject



203
204
205
206
207
# File 'lib/joy_ussd_engine/menu.rb', line 203

def do_validation
    return unless allow_validation
   @previous_menu = @previous_client_state.constantize.new(@context)
   @previous_menu.on_validate
end

#executeObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/joy_ussd_engine/menu.rb', line 226

def execute
    save_field_value
    do_validation
    return render_menu_error[:data] if @menu_error
    if allow_validation
        return render_previous if @previous_menu.field_error
    end
    before_show_menu
    before_render
    response = render
    response = response.blank? ? joy_response(@current_client_state) : response
    after_render
    save_state(response[:ClientState]) if response[:ClientState] != "EndJoyUssdEngine"
    @context.reset_state if response[:ClientState] == "EndJoyUssdEngine" 
    response[:data].blank? ? response : response[:data]
end

#get_previous_stateObject

def save_field_value_load_menu

data = get_previous_state
return if @skip_save
return if data[:"#{data[:field]}"].present?
@context.set_state({"#{data[:field]}".to_sym => @context.params[:message]}) unless data[:field].blank?

end



137
138
139
140
141
# File 'lib/joy_ussd_engine/menu.rb', line 137

def get_previous_state
    data = @context.get_state
    @previous_client_state = data[:PrevClientState]
    data
end

#get_selected_item(error_message = "Sorry wrong option selected") ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/joy_ussd_engine/menu.rb', line 71

def get_selected_item(error_message = "Sorry wrong option selected")

    return unless has_selected?

    selected_item = nil
    check_input = is_numeric(@context.params[:message]) && @context.params[:message].to_i != 0  && !(@context.params[:message].to_i > @menu_items.length)

    if check_input 
        selected_item = @menu_items[@context.params[:message].to_i - 1][:route]
    end

    # 17332447
    
    @menu_error = selected_item.blank?
    @error_text = error_message if @menu_error

    selected_item
end

#has_selected?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/joy_ussd_engine/menu.rb', line 67

def has_selected?
    @context.get_state[:"#{@current_client_state}_show_menu_initiation"] == "exists"
end

#is_last_menuObject



195
196
197
# File 'lib/joy_ussd_engine/menu.rb', line 195

def is_last_menu
    @context.last_menu == @current_client_state
end

#is_numeric(numeric_string) ⇒ Object



90
91
92
# File 'lib/joy_ussd_engine/menu.rb', line 90

def is_numeric(numeric_string)
    "#{numeric_string}" !~ /\D/
end

#joy_release(error_message = "") ⇒ Object



31
32
33
34
35
36
37
# File 'lib/joy_ussd_engine/menu.rb', line 31

def joy_release(error_message = "")
    @context.reset_state
    { 
        ClientState: "EndJoyUssdEngine",
        data: @context.selected_provider.send("release", error_message.blank? ? @menu_text : error_message)
    }
end

#joy_response(client_state) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/joy_ussd_engine/menu.rb', line 17

def joy_response(client_state)
    new_state = client_state.to_s
    if @menu_text.blank?
        set_previous_state
        @context.current_menu = @current_client_state = new_state
        return @context.load_menu(new_state)
    end

    { 
        ClientState: new_state,
        data: @context.selected_provider.send("response", @menu_text, new_state) 
    }            
end

#load_menu(menu_to_load) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/joy_ussd_engine/menu.rb', line 39

def load_menu(menu_to_load)
    return render_menu_error[:data] if @menu_error
    next_menu = menu_to_load.to_s
    if has_selected?
        @context.set_state({"#{@current_client_state}_show_menu_initiation".to_sym =>  nil})
        set_previous_state 
        @context.current_menu = @current_client_state = next_menu
        @context.load_from_paginate_menu(next_menu) 
    end
end

#on_errorObject



156
157
158
# File 'lib/joy_ussd_engine/menu.rb', line 156

def on_error
    
end

#on_validateObject



152
153
154
# File 'lib/joy_ussd_engine/menu.rb', line 152

def on_validate
    
end

#raise_error(message) ⇒ Object



160
161
162
163
# File 'lib/joy_ussd_engine/menu.rb', line 160

def raise_error(message)
    @error_text = message
    @menu_error = true
end

#remove_error_fieldObject



170
171
172
173
# File 'lib/joy_ussd_engine/menu.rb', line 170

def remove_error_field
    data = @context.get_state
    @context.set_state({"#{@field_name}".to_sym => nil})
end

#renderObject



147
148
149
150
# File 'lib/joy_ussd_engine/menu.rb', line 147

def render
    # Render ussd menu here
    puts "render passed"
end

#render_field_errorObject



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/joy_ussd_engine/menu.rb', line 175

def render_field_error
    before_show_menu
    before_render
    on_error
    remove_error_field
    return render_menu_error[:data] if menu_error
    response = render
    response = response.blank? ? joy_response(current_client_state) : response
    after_render
    save_state(response[:ClientState]) if response[:ClientState] != "EndJoyUssdEngine"
    @context.reset_state if response[:ClientState] == "EndJoyUssdEngine" 
    response[:data].blank? ? response : response[:data]
end

#render_menu_errorObject



165
166
167
168
# File 'lib/joy_ussd_engine/menu.rb', line 165

def render_menu_error
    @context.reset_state
    joy_release(@error_text)
end

#render_previousObject



189
190
191
192
193
# File 'lib/joy_ussd_engine/menu.rb', line 189

def render_previous
   @context.current_menu = @previous_menu.current_client_state = @previous_client_state
   @context.set_state({"#{@previous_client_state}_show_menu_initiation".to_sym =>  nil})
   @previous_menu.render_field_error
end

#runObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/joy_ussd_engine/menu.rb', line 209

def run
    save_field_value
    do_validation
    return render_menu_error[:data] if @menu_error
    if allow_validation
        return render_previous if @previous_menu.field_error
    end
    before_show_menu
    before_render
    response = render
    response = response.blank? ? joy_response(@current_client_state) : response
    after_render
    save_state(response[:ClientState]) if response[:ClientState] != "EndJoyUssdEngine"
    @context.reset_state if response[:ClientState] == "EndJoyUssdEngine" 
    response
end

#save_field_valueObject



114
115
116
117
118
119
# File 'lib/joy_ussd_engine/menu.rb', line 114

def save_field_value
    data = get_previous_state
    return if @skip_save
    return if data[:"#{data[:field]}"].present?
    @context.set_state({"#{data[:field]}".to_sym => @context.params[:message]}) unless data[:field].blank?
end

#save_state(state) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/joy_ussd_engine/menu.rb', line 104

def save_state(state)
    return if @skip_save
    data = @context.get_state
    @previous_client_state = @current_client_state
    return if data[:"#{@field_name}"].present?
    return @context.set_state({ClientState: state, PrevClientState: @previous_client_state}) if @field_name.blank?
    # @context.set_state({"#{data[:field]}".to_sym => @context.params[:message]}) unless data[:field].blank?
    @context.set_state({ClientState: state, PrevClientState: @previous_client_state , field: @field_name, field_error: @field_error, error_text: @error_text})
end

#set_previous_stateObject



143
144
145
# File 'lib/joy_ussd_engine/menu.rb', line 143

def set_previous_state
    @context.set_state({PrevClientState: @current_client_state})
end

#show_menu(title = '') ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/joy_ussd_engine/menu.rb', line 50

def show_menu(title = '')
    raise_error("Sorry something went wrong!") if @menu_items.blank?
    tmp_menu = []
    
    first_option = 0
    @menu_items.each do |m|
      tmp_menu << "#{first_option+=1}. #{m[:title]}"
    end
    text = tmp_menu.join("\n")
    title.blank? ? text : "#{title}\n#{text}" 
end