Class: JoyUssdEngine::Menu
- Inherits:
-
Object
- Object
- JoyUssdEngine::Menu
- Defined in:
- lib/joy_ussd_engine/menu.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#context ⇒ Object
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.
-
#current_client_state ⇒ Object
Returns the value of attribute current_client_state.
-
#error_text ⇒ Object
Returns the value of attribute error_text.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#field_error ⇒ Object
Returns the value of attribute field_error.
-
#field_name ⇒ Object
Returns the value of attribute field_name.
-
#menu_error ⇒ Object
Returns the value of attribute menu_error.
-
#menu_items ⇒ Object
Returns the value of attribute menu_items.
-
#menu_text ⇒ Object
Returns the value of attribute menu_text.
-
#previous_client_state ⇒ Object
Returns the value of attribute previous_client_state.
-
#previous_menu ⇒ Object
Returns the value of attribute previous_menu.
-
#skip_save ⇒ Object
Returns the value of attribute skip_save.
Instance Method Summary collapse
- #after_render ⇒ Object
- #allow_validation ⇒ Object
- #before_render ⇒ Object
- #before_show_menu ⇒ Object
- #do_validation ⇒ Object
- #execute ⇒ Object
-
#get_previous_state ⇒ Object
def save_field_value_load_menu data = get_previous_state return if @skip_save return if data[:“#data</a>”].present? @context.set_state(href=":field">data”.to_sym => @context.params}) unless data.blank? end.
- #get_selected_item(error_message = "Sorry wrong option selected") ⇒ Object
- #has_selected? ⇒ Boolean
-
#initialize(context) ⇒ Menu
constructor
A new instance of Menu.
- #is_last_menu ⇒ Object
- #is_numeric(numeric_string) ⇒ Object
- #joy_release(error_message = "") ⇒ Object
- #joy_response(client_state) ⇒ Object
- #load_menu(menu_to_load) ⇒ Object
- #on_error ⇒ Object
- #on_validate ⇒ Object
- #raise_error(message) ⇒ Object
- #remove_error_field ⇒ Object
- #render ⇒ Object
- #render_field_error ⇒ Object
- #render_menu_error ⇒ Object
- #render_previous ⇒ Object
- #run ⇒ Object
- #save_field_value ⇒ Object
- #save_state(state) ⇒ Object
- #set_previous_state ⇒ Object
- #show_menu(title = '') ⇒ Object
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. end |
Instance Attribute Details
#context ⇒ Object (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_state ⇒ Object
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_text ⇒ Object
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 |
#errors ⇒ Object
Returns the value of attribute errors.
10 11 12 |
# File 'lib/joy_ussd_engine/menu.rb', line 10 def errors @errors end |
#field_error ⇒ Object
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_name ⇒ Object
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 |
#menu_error ⇒ Object
Returns the value of attribute menu_error.
10 11 12 |
# File 'lib/joy_ussd_engine/menu.rb', line 10 def @menu_error end |
#menu_items ⇒ Object
Returns the value of attribute menu_items.
10 11 12 |
# File 'lib/joy_ussd_engine/menu.rb', line 10 def @menu_items end |
#menu_text ⇒ Object
Returns the value of attribute menu_text.
10 11 12 |
# File 'lib/joy_ussd_engine/menu.rb', line 10 def @menu_text end |
#previous_client_state ⇒ Object
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_menu ⇒ Object
Returns the value of attribute previous_menu.
10 11 12 |
# File 'lib/joy_ussd_engine/menu.rb', line 10 def @previous_menu end |
#skip_save ⇒ Object
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_render ⇒ Object
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_validation ⇒ Object
199 200 201 |
# File 'lib/joy_ussd_engine/menu.rb', line 199 def allow_validation ! && !@previous_client_state.blank? end |
#before_render ⇒ Object
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_menu ⇒ Object
62 63 64 65 |
# File 'lib/joy_ussd_engine/menu.rb', line 62 def 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_validation ⇒ Object
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 |
#execute ⇒ Object
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 [:data] if @menu_error if allow_validation return render_previous if @previous_menu.field_error end 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_state ⇒ Object
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( = "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 = if @menu_error selected_item end |
#has_selected? ⇒ 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_menu ⇒ Object
195 196 197 |
# File 'lib/joy_ussd_engine/menu.rb', line 195 def @context. == @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( = "") @context.reset_state { ClientState: "EndJoyUssdEngine", data: @context.selected_provider.send("release", .blank? ? @menu_text : ) } 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_client_state = new_state return @context.(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 () return [:data] if @menu_error = .to_s if has_selected? @context.set_state({"#{@current_client_state}_show_menu_initiation".to_sym => nil}) set_previous_state @context. = @current_client_state = @context.() end end |
#on_error ⇒ Object
156 157 158 |
# File 'lib/joy_ussd_engine/menu.rb', line 156 def on_error end |
#on_validate ⇒ Object
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() @error_text = @menu_error = true end |
#remove_error_field ⇒ Object
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 |
#render ⇒ Object
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_error ⇒ Object
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_render on_error remove_error_field return [:data] if 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_error ⇒ Object
165 166 167 168 |
# File 'lib/joy_ussd_engine/menu.rb', line 165 def @context.reset_state joy_release(@error_text) end |
#render_previous ⇒ Object
189 190 191 192 193 |
# File 'lib/joy_ussd_engine/menu.rb', line 189 def render_previous @context. = @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 |
#run ⇒ Object
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 [:data] if @menu_error if allow_validation return render_previous if @previous_menu.field_error end 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_value ⇒ Object
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_state ⇒ Object
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 (title = '') raise_error("Sorry something went wrong!") if @menu_items.blank? = [] first_option = 0 @menu_items.each do |m| << "#{first_option+=1}. #{m[:title]}" end text = .join("\n") title.blank? ? text : "#{title}\n#{text}" end |