joy_ussd_engine 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.gitignore +8 -0
  4. data/.rubocop.yml +13 -0
  5. data/CHANGELOG.md +5 -0
  6. data/CODE_OF_CONDUCT.md +84 -0
  7. data/Gemfile +10 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +559 -0
  10. data/Rakefile +8 -0
  11. data/bin/console +15 -0
  12. data/bin/setup +8 -0
  13. data/images/lifecycle.jpg +0 -0
  14. data/images/menu_doc1.png +0 -0
  15. data/images/menu_doc2.png +0 -0
  16. data/images/menu_items_routes.png +0 -0
  17. data/images/paginate_item_selected.png +0 -0
  18. data/images/paginate_menu1.png +0 -0
  19. data/images/paginate_menu2.png +0 -0
  20. data/images/transactions_menu.png +0 -0
  21. data/joy_ussd_engine.gemspec +38 -0
  22. data/lib/generators/joy_data_transformer/USAGE +8 -0
  23. data/lib/generators/joy_data_transformer/joy_data_transformer_generator.rb +13 -0
  24. data/lib/generators/joy_data_transformer/templates/joy_transformer_template.template +33 -0
  25. data/lib/generators/joy_menu/USAGE +8 -0
  26. data/lib/generators/joy_menu/joy_menu_generator.rb +13 -0
  27. data/lib/generators/joy_menu/templates/joy_menu_template.template +28 -0
  28. data/lib/generators/joy_paginate_menu/USAGE +8 -0
  29. data/lib/generators/joy_paginate_menu/joy_paginate_menu_generator.rb +13 -0
  30. data/lib/generators/joy_paginate_menu/templates/joy_paginate_menu_template.template +48 -0
  31. data/lib/generators/joy_route_menu/USAGE +8 -0
  32. data/lib/generators/joy_route_menu/joy_route_menu_generator.rb +13 -0
  33. data/lib/generators/joy_route_menu/templates/joy_route_menu_template.template +36 -0
  34. data/lib/joy_ussd_engine/data_transformer.rb +35 -0
  35. data/lib/joy_ussd_engine/menu.rb +218 -0
  36. data/lib/joy_ussd_engine/paginate_menu.rb +193 -0
  37. data/lib/joy_ussd_engine/session_manager.rb +41 -0
  38. data/lib/joy_ussd_engine/version.rb +5 -0
  39. data/lib/joy_ussd_engine.rb +47 -0
  40. metadata +101 -0
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JoyUssdEngine::SessionManager
4
+ def expire_mins
5
+ @selected_provider.expiration.blank? ? 60.seconds : @selected_provider.expiration
6
+ end
7
+
8
+ def user_mobile_number
9
+ @user_mobile_number ||= get_state[:"session_id"]
10
+ end
11
+
12
+ # Retrive Session Data
13
+ def get_state
14
+ session_id = params["session_id"]
15
+ REDIS.with do |conn|
16
+ data = conn.get(session_id)
17
+ return {} if data.blank?
18
+ JSON.parse(data, symbolize_names: true)
19
+ end
20
+ end
21
+
22
+ # Store USSD sessions in Redis with Expiry
23
+ def set_state(payload = {})
24
+ session_id = params["session_id"]
25
+ current_data = get_state
26
+ REDIS.with do |conn|
27
+ payload = current_data.merge(params.merge(payload))
28
+ conn.set(session_id, payload.to_json)
29
+ conn.expire(session_id, expire_mins)
30
+ end
31
+ end
32
+
33
+ # Delete Session payload
34
+ def reset_state
35
+ session_id = params["session_id"]
36
+ REDIS.with do |conn|
37
+ conn.del(session_id)
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JoyUssdEngine
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "joy_ussd_engine/version"
4
+ require 'joy_ussd_engine/menu'
5
+ require 'joy_ussd_engine/paginate_menu'
6
+ require 'joy_ussd_engine/data_transformer'
7
+ require 'joy_ussd_engine/session_manager'
8
+
9
+
10
+ module JoyUssdEngine
11
+ class Error < StandardError; end
12
+ class Core
13
+ include JoyUssdEngine::SessionManager
14
+
15
+ attr_reader :params, :selected_provider
16
+ attr_accessor :current_menu, :last_menu
17
+
18
+ def initialize(params, provider, start_point: nil, end_point: nil )
19
+
20
+ # gets provider currently in use and convert params to match ussd engines params
21
+ @selected_provider = provider.new(self)
22
+ convert_params = @selected_provider.send("request_params",params)
23
+ @params = convert_params
24
+ @last_menu = end_point.to_s
25
+ @data = get_state
26
+ # handles ending or terminating ussd based on provider response (HUBTEL, TWILIO, ETC.)
27
+ # If a particular provider returns some sort of response that can terminate the app we do that check here
28
+ return @current_menu = end_point.to_s if @selected_provider.send("app_terminator", params) || @data[:ClientState] == 'EndJoyUssdEngineiuewhjsdj'
29
+
30
+ @current_menu = @data[:ClientState].blank? ? start_point.to_s : @data[:ClientState]
31
+ end
32
+
33
+ def load_menu(name)
34
+ menu_name = name.constantize.new(self)
35
+ menu_name.send("execute")
36
+ end
37
+
38
+ def load_from_paginate_menu(name)
39
+ menu_name = name.constantize.new(self)
40
+ menu_name.send("run")
41
+ end
42
+
43
+ def process
44
+ load_menu(@current_menu)
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: joy_ussd_engine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Caleb Mantey
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: will_paginate
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.3.0
27
+ description: A ruby library for building text based applications rapidly. It supports
28
+ building whatsapp, ussd, telegram and various text or chat applications that communicate
29
+ with your rails backend. With this library you can target multiple platforms(whatsapp,
30
+ ussd, telegram, etc.) at once with just one codebase.
31
+ email:
32
+ - manteycaleb@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - ".DS_Store"
38
+ - ".gitignore"
39
+ - ".rubocop.yml"
40
+ - CHANGELOG.md
41
+ - CODE_OF_CONDUCT.md
42
+ - Gemfile
43
+ - LICENSE.txt
44
+ - README.md
45
+ - Rakefile
46
+ - bin/console
47
+ - bin/setup
48
+ - images/lifecycle.jpg
49
+ - images/menu_doc1.png
50
+ - images/menu_doc2.png
51
+ - images/menu_items_routes.png
52
+ - images/paginate_item_selected.png
53
+ - images/paginate_menu1.png
54
+ - images/paginate_menu2.png
55
+ - images/transactions_menu.png
56
+ - joy_ussd_engine.gemspec
57
+ - lib/generators/joy_data_transformer/USAGE
58
+ - lib/generators/joy_data_transformer/joy_data_transformer_generator.rb
59
+ - lib/generators/joy_data_transformer/templates/joy_transformer_template.template
60
+ - lib/generators/joy_menu/USAGE
61
+ - lib/generators/joy_menu/joy_menu_generator.rb
62
+ - lib/generators/joy_menu/templates/joy_menu_template.template
63
+ - lib/generators/joy_paginate_menu/USAGE
64
+ - lib/generators/joy_paginate_menu/joy_paginate_menu_generator.rb
65
+ - lib/generators/joy_paginate_menu/templates/joy_paginate_menu_template.template
66
+ - lib/generators/joy_route_menu/USAGE
67
+ - lib/generators/joy_route_menu/joy_route_menu_generator.rb
68
+ - lib/generators/joy_route_menu/templates/joy_route_menu_template.template
69
+ - lib/joy_ussd_engine.rb
70
+ - lib/joy_ussd_engine/data_transformer.rb
71
+ - lib/joy_ussd_engine/menu.rb
72
+ - lib/joy_ussd_engine/paginate_menu.rb
73
+ - lib/joy_ussd_engine/session_manager.rb
74
+ - lib/joy_ussd_engine/version.rb
75
+ homepage: https://github.com/Caleb-Mantey/joy_ussd_engine
76
+ licenses:
77
+ - MIT
78
+ metadata:
79
+ homepage_uri: https://github.com/Caleb-Mantey/joy_ussd_engine
80
+ source_code_uri: https://github.com/Caleb-Mantey/joy_ussd_engine
81
+ changelog_uri: https://github.com/Caleb-Mantey/joy_ussd_engine/CHANGELOG.md
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 2.4.0
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.0.9
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A gem for building ussd and text based applications rapidly.
101
+ test_files: []