Class: Bianchi::USSD::Engine
- Inherits:
-
Object
- Object
- Bianchi::USSD::Engine
- Includes:
- ProviderConfigurations
- Defined in:
- lib/bianchi/ussd/engine.rb
Instance Attribute Summary collapse
-
#menus ⇒ Object
Returns the value of attribute menus.
-
#params ⇒ Object
Returns the value of attribute params.
-
#prompt_data ⇒ Object
Returns the value of attribute prompt_data.
-
#provider ⇒ Object
Returns the value of attribute provider.
-
#session ⇒ Object
Returns the value of attribute session.
Class Method Summary collapse
Instance Method Summary collapse
- #ensure_initial_menu ⇒ Object
- #initial_menu ⇒ Object
- #initial_menu_page ⇒ Object
-
#initialize(params, options) ⇒ Engine
constructor
A new instance of Engine.
- #menu(menu_name, options = {}) ⇒ Object
- #menu_page(page_number, action) ⇒ Object
- #process_activity_state ⇒ Object
- #subsequent_menu_page ⇒ Object
- #validate_engine_options(options) ⇒ Object
Methods included from ProviderConfigurations
#parse_params, #parser_prompt_data
Methods included from ProviderParsers::Arkesel
#arkesel_params_parser, #arkesel_prompt_data_parser
Methods included from ProviderParsers::Appsnmobile
#appsnmobile_params_parser, #appsnmobile_prompt_data_parser, #return_activity_state
Methods included from ProviderParsers::Africastalking
#africastalking_params_parser, #africastalking_prompt_data_parser
Constructor Details
#initialize(params, options) ⇒ Engine
Returns a new instance of Engine.
10 11 12 13 14 15 16 17 18 |
# File 'lib/bianchi/ussd/engine.rb', line 10 def initialize(params, ) () @provider = [:provider]&.to_sym || :none @params = parse_params(params) @session = Session.new @params @menus = [] @prompt_data = nil end |
Instance Attribute Details
#menus ⇒ Object
Returns the value of attribute menus.
8 9 10 |
# File 'lib/bianchi/ussd/engine.rb', line 8 def @menus end |
#params ⇒ Object
Returns the value of attribute params.
8 9 10 |
# File 'lib/bianchi/ussd/engine.rb', line 8 def params @params end |
#prompt_data ⇒ Object
Returns the value of attribute prompt_data.
8 9 10 |
# File 'lib/bianchi/ussd/engine.rb', line 8 def prompt_data @prompt_data end |
#provider ⇒ Object
Returns the value of attribute provider.
8 9 10 |
# File 'lib/bianchi/ussd/engine.rb', line 8 def provider @provider end |
#session ⇒ Object
Returns the value of attribute session.
8 9 10 |
# File 'lib/bianchi/ussd/engine.rb', line 8 def session @session end |
Class Method Details
.start(params, options = {}, &block) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/bianchi/ussd/engine.rb', line 20 def self.start(params, = {}, &block) raise ArgumentError, "block required to start the engine" unless block_given? new(params, ).tap do |e| validate_params(e.params) e.instance_eval(&block) e.process_activity_state end end |
.validate_params(params) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/bianchi/ussd/engine.rb', line 40 def self.validate_params(params) raise ArgumentError, "params expected to be a hash to start engine" unless params.is_a? Hash required_params = %w[session_id mobile_number input_body activity_state] left_required_params = required_params - params.keys.map(&:to_s) unless left_required_params.empty? raise ArgumentError, "#{left_required_params} required in params to start engine" end return if %w[initial subsequent].include? params[:activity_state] raise ArgumentError, "activity_state has to either be initial or subsequent" end |
Instance Method Details
#ensure_initial_menu ⇒ Object
65 66 67 68 69 |
# File 'lib/bianchi/ussd/engine.rb', line 65 def return if raise PageLoadError, "an initial menu is required to proceed" end |
#initial_menu ⇒ Object
89 90 91 |
# File 'lib/bianchi/ussd/engine.rb', line 89 def .find { || .[:initial] } end |
#initial_menu_page ⇒ Object
84 85 86 87 |
# File 'lib/bianchi/ussd/engine.rb', line 84 def session. = ("Page1", :request) end |
#menu(menu_name, options = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/bianchi/ussd/engine.rb', line 55 def (, = {}) unless [String, Symbol].include? .class raise ArgumentError, "menu_name expected to either be string/symbol to define menus" end raise ArgumentError, "menu_options expected to be a hash to start engine" unless .is_a? Hash @menus << Menu.new(, ) end |
#menu_page(page_number, action) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/bianchi/ussd/engine.rb', line 104 def (page_number, action) constant_name = "USSD::#{session..name.camelize}Menu::#{page_number}" page = constant_name.safe_constantize unless page raise PageLoadError, "#{constant_name} is supposed to be defined to process #{session..name} menu #{page_number}" end page.call(session, action) end |
#process_activity_state ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/bianchi/ussd/engine.rb', line 71 def process_activity_state page = case session.activity_state.to_sym when :initial when :subsequent end @prompt_data = parser_prompt_data page.session_prompt_data end |
#subsequent_menu_page ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/bianchi/ussd/engine.rb', line 93 def previous_session = session.store.previous_session unless previous_session raise PageLoadError, "Previous session not found to load subsequent page restart from initial menu" end session. = previous_session. (previous_session.page_number, :response) end |
#validate_engine_options(options) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/bianchi/ussd/engine.rb', line 30 def () raise ArgumentError, "options expected to be a hash to start engine" unless .is_a? Hash return if .empty? = %i[provider] return if (.keys.map(&:to_sym) - ).empty? raise ArgumentError, "#{} are the only valid option keys" end |