joy_ussd_engine 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87daa4c0dae57919366d4454695275f33052d7505412c80d19cf571e85e1ea91
4
- data.tar.gz: 7d0c6af6ff5792d2bbabc646ab0c5d720ae72eafbe0138dcf7ff9846e097065b
3
+ metadata.gz: a604dfd6d12b293936040a84184ef0c2e9db87481306f092bfda2d7f34fc24cc
4
+ data.tar.gz: 8c385b17128c1617228b04edabc55b17c77dad1f2629bff5bf32d7f441f53cf0
5
5
  SHA512:
6
- metadata.gz: eb4c5b98ce92119a64af3a8983f03282e653fc1728f1c003f61e35a515dade0bf8187f4016cf95103ccdd0222e43f742143d39088f065e08072ba666f4c54c3e
7
- data.tar.gz: 74e3b79758b858bbaece5fc524cee1ba3a3eee99e0322013cf16d331f2a9533f961ddd5cc24722f8140e1a0424d9f00e15cdfe37e1358a0ea1c1bdf66670bdfd
6
+ metadata.gz: b19012528c1a3e8fd021a16fc2953877fec56f9f543d1092e53d0452e365cc16699e0190374f11581ad23def254f9494557a285d386269dca709630fe254a509
7
+ data.tar.gz: d3ba5cdaaf1072085c0c13574d06900a9653cb97c0b3f70c0654b5a018f20b74a03dc9de09c7ca330b8dafeaefeaf6ed4c56bf61661f0c0aa8ea4b0fc5ebcef3
data/Gemfile.lock CHANGED
@@ -8,13 +8,28 @@ GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  ast (2.4.2)
11
+ diff-lcs (1.5.0)
11
12
  parallel (1.21.0)
12
13
  parser (3.0.2.0)
13
14
  ast (~> 2.4.1)
14
15
  rainbow (3.0.0)
15
16
  rake (13.0.6)
17
+ redis (4.6.0)
16
18
  regexp_parser (2.2.1)
17
19
  rexml (3.2.5)
20
+ rspec (3.11.0)
21
+ rspec-core (~> 3.11.0)
22
+ rspec-expectations (~> 3.11.0)
23
+ rspec-mocks (~> 3.11.0)
24
+ rspec-core (3.11.0)
25
+ rspec-support (~> 3.11.0)
26
+ rspec-expectations (3.11.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.11.0)
29
+ rspec-mocks (3.11.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.11.0)
32
+ rspec-support (3.11.0)
18
33
  rubocop (1.20.0)
19
34
  parallel (~> 1.10)
20
35
  parser (>= 3.0.0.0)
@@ -36,6 +51,8 @@ PLATFORMS
36
51
  DEPENDENCIES
37
52
  joy_ussd_engine!
38
53
  rake (~> 13.0)
54
+ redis
55
+ rspec
39
56
  rubocop (~> 1.7)
40
57
 
41
58
  BUNDLED WITH
data/README.md CHANGED
@@ -29,6 +29,9 @@ A ruby library for building text based applications rapidly. It supports buildin
29
29
  - [PaginateMenu Properties](#paginatemenu-properties)
30
30
  - [PaginateMenu Methods](#paginatemenu-methods)
31
31
  - [PaginateMenu Example](#paginatemenu-example)
32
+ - [Transformer Configs](#transformer-configs)
33
+ - [Hubtel Transformer](#hubtel-transformer)
34
+ - [Twilio Transformer](#twilio-transformer)
32
35
  - [Development](#development)
33
36
  - [Contributing](#contributing)
34
37
  - [License](#license)
@@ -546,6 +549,108 @@ When th user selects an item in the `PaginateMenu` we get the users selection wi
546
549
 
547
550
  ![paginate_item_select](images/paginate_item_selected.png)
548
551
 
552
+ ## Transformer Configs
553
+
554
+ Transformer configs for various providers. This configs can be used if you use any of these providers.
555
+
556
+ ### Hubtel Transformer
557
+
558
+ ```ruby
559
+ class HubtelTransformer < JoyUssdEngine::DataTransformer
560
+ # Tranforms request and response payload between hubtel and our application
561
+ def request_params(params)
562
+ {
563
+ session_id: params[:Mobile],
564
+ message: params[:Message],
565
+ ClientState: params[:ClientState],
566
+ Type: params[:Type]
567
+ data: params
568
+ }
569
+ end
570
+
571
+ def app_terminator(params)
572
+ params[:Type] == 'Release' || (params[:Type] != "Initiation" && @context.get_state.blank?)
573
+ end
574
+
575
+ def response(message, client_state)
576
+ {
577
+ Type: "Response",
578
+ Message: message,
579
+ ClientState: client_state
580
+ }
581
+ end
582
+
583
+ def release(message)
584
+ {
585
+ Type: "Release",
586
+ Message: message,
587
+ ClientState: "End"
588
+ }
589
+ end
590
+
591
+ def expiration
592
+ 60.seconds
593
+ end
594
+ end
595
+ ```
596
+
597
+ ### Twilio Transformer
598
+
599
+ ```ruby
600
+ class TwilioTransformer < JoyUssdEngine::DataTransformer
601
+ ACCOUNT_SID = "932843hwhjewhje7388"
602
+ AUTH_TOKEN = "3473847hewjrejrheeee"
603
+
604
+ def client
605
+ @client ||= Twilio::REST::Client.new(ACCOUNT_SID, AUTH_TOKEN)
606
+ end
607
+
608
+ # Tranforms request and response payload between twilio and our application
609
+ def request_params(params)
610
+ {
611
+ session_id: "0#{params[:From].last(9)}",
612
+ message: params[:Body],
613
+ Mobile: "0#{params[:From].last(9)}",
614
+ data: params
615
+ }
616
+ end
617
+
618
+ def app_terminator(params)
619
+ params[:Body] == 'end'
620
+ end
621
+
622
+ def response(message, client_state)
623
+ client.messages.create(
624
+ from: from,
625
+ to: to,
626
+ body: message
627
+ )
628
+ {message: message}
629
+ end
630
+
631
+ def release(message)
632
+ client.messages.create(
633
+ from: from,
634
+ to: to,
635
+ body: message
636
+ )
637
+ {message: message}
638
+ end
639
+
640
+ def expiration
641
+ # set expiration for different providers
642
+ end
643
+
644
+ def to
645
+ "whatsapp:+233#{@context.params[:Mobile].last(9)}"
646
+ end
647
+
648
+ def from
649
+ 'whatsapp:+14155238886'
650
+ end
651
+ end
652
+ ```
653
+
549
654
  ## Development
550
655
 
551
656
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -37,6 +37,8 @@ Gem::Specification.new do |spec|
37
37
  # Uncomment to register a new dependency of your gem
38
38
  # spec.add_dependency "example-gem", "~> 1.0"
39
39
  spec.add_dependency "will_paginate", "~> 3.3.0"
40
+ spec.add_development_dependency "rspec"
41
+ spec.add_development_dependency "redis"
40
42
 
41
43
  # For more information and examples about making a new gem, checkout our
42
44
  # guide at: https://bundler.io/guides/creating_gem.html
@@ -8,7 +8,7 @@ module JoyUssdEngine
8
8
  # what our application can understand
9
9
  attr_reader :context
10
10
  def initialize(context)
11
- @context = context
11
+ @context = context
12
12
  end
13
13
 
14
14
  def request_params(params)
@@ -30,6 +30,7 @@ module JoyUssdEngine
30
30
 
31
31
  def expiration
32
32
  # set expiration for different providers
33
+ @context.expiration.blank? ? 60.seconds : @expiration
33
34
  end
34
35
  end
35
36
  end
@@ -0,0 +1,36 @@
1
+ require 'data_transformer'
2
+
3
+ module JoyUssdEngine
4
+ class HubtelTransformer < JoyUssdEngine::DataTransformer
5
+ # Tranforms request and response payload between hubtel and our application
6
+ def request_params(params)
7
+ {
8
+ session_id: params[:Mobile],
9
+ message: params[:Message],
10
+ ClientState: params[:ClientState],
11
+ Type: params[:Type]
12
+ data: params
13
+ }
14
+ end
15
+
16
+ def app_terminator(params)
17
+ params[:Type] == 'Release' || (params[:Type] != "Initiation" && @context.get_state.blank?)
18
+ end
19
+
20
+ def response(message, client_state)
21
+ {
22
+ Type: "Response",
23
+ Message: message,
24
+ ClientState: client_state
25
+ }
26
+ end
27
+
28
+ def release(message)
29
+ {
30
+ Type: "Release",
31
+ Message: message,
32
+ ClientState: "EndJoyUssdEngine"
33
+ }
34
+ end
35
+ end
36
+ end
@@ -31,7 +31,7 @@ module JoyUssdEngine
31
31
  def joy_release(error_message = "")
32
32
  @context.reset_state
33
33
  {
34
- ClientState: "EndJoyUssdEngineiuewhjsdj",
34
+ ClientState: "EndJoyUssdEngine",
35
35
  data: @context.selected_provider.send("release", error_message.blank? ? @menu_text : error_message)
36
36
  }
37
37
  end
@@ -156,8 +156,8 @@ module JoyUssdEngine
156
156
  response = render
157
157
  response = response.blank? ? joy_response(current_client_state) : response
158
158
  after_render
159
- save_state(response[:ClientState]) if response[:ClientState] != "EndJoyUssdEngineiuewhjsdj"
160
- @context.reset_state if response[:ClientState] == "EndJoyUssdEngineiuewhjsdj"
159
+ save_state(response[:ClientState]) if response[:ClientState] != "EndJoyUssdEngine"
160
+ @context.reset_state if response[:ClientState] == "EndJoyUssdEngine"
161
161
  response[:data].blank? ? response : response[:data]
162
162
  end
163
163
 
@@ -193,8 +193,8 @@ module JoyUssdEngine
193
193
  response = render
194
194
  response = response.blank? ? joy_response(@current_client_state) : response
195
195
  after_render
196
- save_state(response[:ClientState]) if response[:ClientState] != "EndJoyUssdEngineiuewhjsdj"
197
- @context.reset_state if response[:ClientState] == "EndJoyUssdEngineiuewhjsdj"
196
+ save_state(response[:ClientState]) if response[:ClientState] != "EndJoyUssdEngine"
197
+ @context.reset_state if response[:ClientState] == "EndJoyUssdEngine"
198
198
  response
199
199
  end
200
200
 
@@ -210,8 +210,8 @@ module JoyUssdEngine
210
210
  response = render
211
211
  response = response.blank? ? joy_response(@current_client_state) : response
212
212
  after_render
213
- save_state(response[:ClientState]) if response[:ClientState] != "EndJoyUssdEngineiuewhjsdj"
214
- @context.reset_state if response[:ClientState] == "EndJoyUssdEngineiuewhjsdj"
213
+ save_state(response[:ClientState]) if response[:ClientState] != "EndJoyUssdEngine"
214
+ @context.reset_state if response[:ClientState] == "EndJoyUssdEngine"
215
215
  response[:data].blank? ? response : response[:data]
216
216
  end
217
217
  end
@@ -185,8 +185,8 @@ module JoyUssdEngine
185
185
  response = render
186
186
  response = response.blank? ? joy_response(@current_client_state) : response
187
187
  after_render
188
- save_state(response[:ClientState]) if response[:ClientState] != "EndJoyUssdEngineiuewhjsdj"
189
- @context.reset_state if response[:ClientState] == "EndJoyUssdEngineiuewhjsdj"
188
+ save_state(response[:ClientState]) if response[:ClientState] != "EndJoyUssdEngine"
189
+ @context.reset_state if response[:ClientState] == "EndJoyUssdEngine"
190
190
  response[:data].blank? ? response : response[:data]
191
191
  end
192
192
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JoyUssdEngine
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
@@ -5,6 +5,7 @@ require 'joy_ussd_engine/menu'
5
5
  require 'joy_ussd_engine/paginate_menu'
6
6
  require 'joy_ussd_engine/data_transformer'
7
7
  require 'joy_ussd_engine/session_manager'
8
+ require 'joy_ussd_engine/hubtel_transformer'
8
9
 
9
10
 
10
11
  module JoyUssdEngine
@@ -13,11 +14,12 @@ module JoyUssdEngine
13
14
  include JoyUssdEngine::SessionManager
14
15
 
15
16
  attr_reader :params, :selected_provider
16
- attr_accessor :current_menu, :last_menu
17
+ attr_accessor :current_menu, :last_menu, :expiration
17
18
 
18
- def initialize(params, provider, start_point: nil, end_point: nil )
19
+ def initialize(params, provider, start_point: nil, end_point: nil, expiration: nil )
19
20
 
20
21
  # gets provider currently in use and convert params to match ussd engines params
22
+ @expiration = expiration
21
23
  @selected_provider = provider.new(self)
22
24
  convert_params = @selected_provider.send("request_params",params)
23
25
  @params = convert_params
@@ -25,7 +27,7 @@ module JoyUssdEngine
25
27
  @data = get_state
26
28
  # handles ending or terminating ussd based on provider response (HUBTEL, TWILIO, ETC.)
27
29
  # 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'
30
+ return @current_menu = end_point.to_s if @selected_provider.send("app_terminator", params) || @data[:ClientState] == 'EndJoyUssdEngine'
29
31
 
30
32
  @current_menu = @data[:ClientState].blank? ? start_point.to_s : @data[:ClientState]
31
33
  end
@@ -43,5 +45,9 @@ module JoyUssdEngine
43
45
  def process
44
46
  load_menu(@current_menu)
45
47
  end
48
+
49
+ def expiration
50
+ @expiration
51
+ end
46
52
  end
47
53
  end
Binary file
@@ -0,0 +1,7 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe JoyUssdEngine do
4
+ it "has a version number" do
5
+ expect(JoyUssdEngine::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,26 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe JoyUssdEngine::DataTransformer do
4
+
5
+
6
+
7
+ it "saves the context object" do
8
+ expect(@transformer_context).to eq(nil)
9
+ end
10
+
11
+ it "returns false when calling the appterminator method" do
12
+ expect(@transformer_context).to eq(@context)
13
+ end
14
+
15
+ it 'transforms an incoming request params' do
16
+ params = { Message: "hello", phone: "+233578876155" }
17
+ allow(@data_transformer).to receive(:request_params).with(params).and_return({message: params[:Message], session_id: params[:phone]})
18
+ expect(@data_transformer.request_params(params)).to eq({message: "hello", session_id: "+233578876155"})
19
+ end
20
+
21
+ it 'returns false when app_terminator is called' do
22
+ allow(@data_transformer).to receive(:app_terminator).and_call_original
23
+ expect(@data_transformer.app_terminator({})).to eq(false)
24
+ end
25
+
26
+ end
@@ -0,0 +1,12 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'joy_ussd_engine' # and any other gems you need
5
+
6
+ RSpec.configure do |config|
7
+ # some (optional) config here
8
+ @params = { Message: "hello", phone: "+233578876155" }
9
+ @context = JoyUssdEngine::Core
10
+ @data_transformer = JoyUssdEngine::DataTransformer.new(@context)
11
+ @transformer_context = @data_transformer.context
12
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: joy_ussd_engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caleb Mantey
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-17 00:00:00.000000000 Z
11
+ date: 2022-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: will_paginate
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.3.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  description: A ruby library for building text based applications rapidly. It supports
28
56
  building whatsapp, ussd, telegram and various text or chat applications that communicate
29
57
  with your rails backend. With this library you can target multiple platforms(whatsapp,
@@ -66,12 +94,16 @@ files:
66
94
  - lib/generators/joy_route_menu/templates/joy_route_menu_template.template
67
95
  - lib/joy_ussd_engine.rb
68
96
  - lib/joy_ussd_engine/data_transformer.rb
97
+ - lib/joy_ussd_engine/hubtel_transformer.rb
69
98
  - lib/joy_ussd_engine/menu.rb
70
99
  - lib/joy_ussd_engine/paginate_menu.rb
71
100
  - lib/joy_ussd_engine/session_manager.rb
72
101
  - lib/joy_ussd_engine/version.rb
73
102
  - pkg/joy_ussd_engine-0.1.1.gem
74
103
  - pkg/joy_ussd_engine-0.1.2.gem
104
+ - spec/joy_ussd_engine_spec.rb
105
+ - spec/joy_ussd_engine_transformer.spec
106
+ - spec/spec_helper.rb
75
107
  homepage: https://github.com/Caleb-Mantey/joy_ussd_engine
76
108
  licenses:
77
109
  - MIT