moceansdk 1.0.1 → 1.1.0

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: baa2fbc9440d656aee7a53d85dd3bac6be392cae9ac8559f41cf5e5c25694500
4
- data.tar.gz: bb6c25ba828434a22b301decc6d5e1edb5f3c1e82c85182415dc09a8169a4564
3
+ metadata.gz: e6afe87fef5a9c54ffd8fa33c4762ea50515918ec4049276e2d2e41fd510153a
4
+ data.tar.gz: b5af61783dbe5ceaa491de065a52608a2bd9a7399088797ec5bbf31d105f1e8b
5
5
  SHA512:
6
- metadata.gz: 516771c7d9ce1a9fa24ee023db3427e266abd89336bc15743f49f0877bffd5c03141eafa5867941cb95a8828d89647a7eef48232c185acb5b8b23c4fea80d270
7
- data.tar.gz: cda8a8545f864c4717cc3efec9fcd29afe7b585b33c8bb99ddd9c7f69a0f44b13e6cfbc76c8d3883ef55bbffaaa1513ebb92bf0a8d86adba1b754c574a8447ee
6
+ metadata.gz: a0093116366cf6d19fce9d0b39f7e194098899a4d7dd195cfef71875d709498fe03da51b0a3c2bf1f2d97e1d5e9ac75288b94ad83fa54e4c4a23342b7c930166
7
+ data.tar.gz: c9931cd1bf804cbccf4d0f0f3d25597efb403a114e42c5d8324f55b8aa103913386b7f51ac513d9a004a7da72f2df9f52b384f6210129b0652eaa6c9c7d7e3c0
@@ -23,6 +23,17 @@ require 'moceansdk/modules/message/verify_validate'
23
23
 
24
24
  require 'moceansdk/modules/number_lookup/number_lookup'
25
25
 
26
+ require 'moceansdk/modules/voice/mc_object/abstract_mc'
27
+ require 'moceansdk/modules/voice/mc_object/dial'
28
+ require 'moceansdk/modules/voice/mc_object/collect'
29
+ require 'moceansdk/modules/voice/mc_object/play'
30
+ require 'moceansdk/modules/voice/mc_object/record'
31
+ require 'moceansdk/modules/voice/mc_object/say'
32
+ require 'moceansdk/modules/voice/mc_object/sleep'
33
+ require 'moceansdk/modules/voice/mc'
34
+ require 'moceansdk/modules/voice/mc_builder'
35
+ require 'moceansdk/modules/voice/voice'
36
+
26
37
  require 'moceansdk/utils'
27
38
  require 'moceansdk/version'
28
39
  require 'moceansdk/client'
@@ -57,6 +57,10 @@ module Moceansdk
57
57
  def number_lookup
58
58
  Modules::NumberLookup::NumberLookup.new(@obj_auth, @transmitter)
59
59
  end
60
+
61
+ def voice
62
+ Modules::Voice::Voice.new(@obj_auth, @transmitter)
63
+ end
60
64
  end
61
65
 
62
66
  end
@@ -22,7 +22,7 @@ module Moceansdk
22
22
  end
23
23
 
24
24
  # convert string hash to symbol hash
25
- @params = final_params.inject({}) {|memo, (k, v)| memo[k.to_sym] = v; memo}
25
+ @params = Moceansdk::Utils.convert_to_symbol_hash(final_params)
26
26
  end
27
27
 
28
28
  def param_prefix_set?(key)
@@ -13,16 +13,23 @@ module Moceansdk
13
13
  def default_options
14
14
  {
15
15
  base_url: 'https://rest.moceanapi.com',
16
- version: '2'
16
+ version: '2',
17
+ verify_ssl: true
17
18
  }
18
19
  end
19
20
 
20
21
  def get(uri, params)
21
- request('get', uri, params)
22
+ request_and_parse_body('get', uri, params)
22
23
  end
23
24
 
24
25
  def post(uri, params)
25
- request('post', uri, params)
26
+ request_and_parse_body('post', uri, params)
27
+ end
28
+
29
+ def request_and_parse_body(method, uri, params)
30
+ res = request(method, uri, params)
31
+
32
+ format_response(res.to_s, params[:'mocean-resp-format'] == 'xml', uri)
26
33
  end
27
34
 
28
35
  def request(method, uri, params)
@@ -33,13 +40,11 @@ module Moceansdk
33
40
 
34
41
  url = @options[:base_url] + '/rest/' + @options[:version] + uri
35
42
 
36
- res = if method.casecmp('get').zero?
37
- HTTP.get(url, params: params)
38
- else
39
- HTTP.post(url, form: params)
40
- end
41
-
42
- format_response(res.to_s, params[:'mocean-resp-format'] == 'xml', uri)
43
+ if method.casecmp('get').zero?
44
+ HTTP.follow.get(url, params: params, ssl_context: make_ssl_context)
45
+ else
46
+ HTTP.post(url, form: params, ssl_context: make_ssl_context)
47
+ end
43
48
  end
44
49
 
45
50
  def format_response(response_text, is_xml = false, uri = nil)
@@ -80,12 +85,27 @@ module Moceansdk
80
85
  processed_response.messages.message = [processed_response.messages.message]
81
86
  end
82
87
  processed_response.messages = processed_response.messages.message
88
+ elsif uri == '/voice/dial'
89
+ unless processed_response.calls.call.is_a? Array
90
+ processed_response.calls.call = [processed_response.calls.call]
91
+ end
92
+ processed_response.calls = processed_response.calls.call
83
93
  end
84
94
  end
85
95
 
86
96
  processed_response.raw_response = raw_response
87
97
  processed_response
88
98
  end
99
+
100
+ def make_ssl_context
101
+ if @options[:verify_ssl]
102
+ return nil
103
+ end
104
+
105
+ ctx = OpenSSL::SSL::SSLContext.new
106
+ ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
107
+ ctx
108
+ end
89
109
  end
90
110
 
91
111
  end
@@ -0,0 +1,63 @@
1
+ module Moceansdk
2
+ module Modules
3
+ module Voice
4
+
5
+ class Mc
6
+ def self.say(text = nil)
7
+ ins = McObject::Say.new
8
+
9
+ unless text.nil?
10
+ ins.text = text
11
+ end
12
+
13
+ ins
14
+ end
15
+
16
+ def self.play(file = nil)
17
+ ins = McObject::Play.new
18
+
19
+ unless file.nil?
20
+ ins.files = file
21
+ end
22
+
23
+ ins
24
+ end
25
+
26
+ def self.dial(to = nil)
27
+ ins = McObject::Dial.new
28
+
29
+ unless to.nil?
30
+ ins.to = to
31
+ end
32
+
33
+ ins
34
+ end
35
+
36
+ def self.collect(event_url = nil)
37
+ ins = McObject::Collect.new
38
+
39
+ unless event_url.nil?
40
+ ins.event_url = event_url
41
+ end
42
+
43
+ ins
44
+ end
45
+
46
+ def self.sleep(duration = nil)
47
+ ins = McObject::Sleep.new
48
+
49
+ unless duration.nil?
50
+ ins.duration = duration
51
+ end
52
+
53
+ ins
54
+ end
55
+
56
+ def self.record
57
+ McObject::Record.new
58
+ end
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,30 @@
1
+ module Moceansdk
2
+ module Modules
3
+ module Voice
4
+
5
+ class McBuilder
6
+ def initialize
7
+ @mc = []
8
+ end
9
+
10
+ def add(mc)
11
+ unless mc.is_a? McObject::AbstractMc
12
+ raise Moceansdk::Exceptions::MoceanError, 'mc_object must extend AbstractMc'
13
+ end
14
+
15
+ @mc.push(mc)
16
+ self
17
+ end
18
+
19
+ def build
20
+ converted = []
21
+ @mc.each do |mc|
22
+ converted.push(mc.get_request_data)
23
+ end
24
+ converted
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,38 @@
1
+ module Moceansdk
2
+ module Modules
3
+ module Voice
4
+ module McObject
5
+
6
+ class AbstractMc
7
+ def initialize(params = nil)
8
+ @params = {}
9
+
10
+ @params = Moceansdk::Utils.convert_to_symbol_hash(params) unless params.nil?
11
+ end
12
+
13
+ def get_request_data
14
+ @params = Moceansdk::Utils.convert_to_symbol_hash(@params)
15
+
16
+ required_key.each do |key|
17
+ if @params[:"#{key}"].nil?
18
+ raise Moceansdk::Exceptions::RequiredFieldException, "#{key} is mandatory field, can't leave empty (#{self})"
19
+ end
20
+ end
21
+
22
+ @params[:action] = action
23
+ @params
24
+ end
25
+
26
+ def required_key
27
+ raise NotImplementedError, 'AbstractMc is a abstract class'
28
+ end
29
+
30
+ def action
31
+ raise NotImplementedError, 'AbstractMc is a abstract class'
32
+ end
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,39 @@
1
+ module Moceansdk
2
+ module Modules
3
+ module Voice
4
+ module McObject
5
+
6
+ class Collect < AbstractMc
7
+ def event_url=(param)
8
+ @params[:'event-url'] = param
9
+ end
10
+
11
+ def minimum=(param)
12
+ @params[:min] = param
13
+ end
14
+
15
+ def maximum=(param)
16
+ @params[:max] = param
17
+ end
18
+
19
+ def terminators=(param)
20
+ @params[:terminators] = param
21
+ end
22
+
23
+ def timeout=(param)
24
+ @params[:timeout] = param
25
+ end
26
+
27
+ def required_key
28
+ ['event-url', 'min', 'max', 'timeout']
29
+ end
30
+
31
+ def action
32
+ 'collect'
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,31 @@
1
+ module Moceansdk
2
+ module Modules
3
+ module Voice
4
+ module McObject
5
+
6
+ class Dial < AbstractMc
7
+ def to=(param)
8
+ @params[:to] = param
9
+ end
10
+
11
+ def from=(param)
12
+ @params[:from] = param
13
+ end
14
+
15
+ def dial_sequentially=(param)
16
+ @params[:'dial-sequentially'] = param
17
+ end
18
+
19
+ def required_key
20
+ ['to']
21
+ end
22
+
23
+ def action
24
+ 'dial'
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ module Moceansdk
2
+ module Modules
3
+ module Voice
4
+ module McObject
5
+
6
+ class Play < AbstractMc
7
+ def files=(param)
8
+ @params[:file] = param
9
+ end
10
+
11
+ def barge_in=(param)
12
+ @params[:'barge-in'] = param
13
+ end
14
+
15
+ def clear_digit_cache=(param)
16
+ @params[:'clear-digit-cache'] = param
17
+ end
18
+
19
+ def required_key
20
+ ['file']
21
+ end
22
+
23
+ def action
24
+ 'play'
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ module Moceansdk
2
+ module Modules
3
+ module Voice
4
+ module McObject
5
+
6
+ class Record < AbstractMc
7
+ def required_key
8
+ []
9
+ end
10
+
11
+ def action
12
+ 'record'
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,43 @@
1
+ module Moceansdk
2
+ module Modules
3
+ module Voice
4
+ module McObject
5
+
6
+ class Say < AbstractMc
7
+ def initialize(params = nil)
8
+ super(params)
9
+
10
+ if @params[:language].nil?
11
+ @params[:language] = 'en-US'
12
+ end
13
+ end
14
+
15
+ def language=(param)
16
+ @params[:language] = param
17
+ end
18
+
19
+ def text=(param)
20
+ @params[:text] = param
21
+ end
22
+
23
+ def barge_in=(param)
24
+ @params[:'barge-in'] = param
25
+ end
26
+
27
+ def clear_digit_cache=(param)
28
+ @params[:'clear-digit-cache'] = param
29
+ end
30
+
31
+ def required_key
32
+ ['text', 'language']
33
+ end
34
+
35
+ def action
36
+ 'say'
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,23 @@
1
+ module Moceansdk
2
+ module Modules
3
+ module Voice
4
+ module McObject
5
+
6
+ class Sleep < AbstractMc
7
+ def duration=(param)
8
+ @params[:duration] = param
9
+ end
10
+
11
+ def required_key
12
+ ['duration']
13
+ end
14
+
15
+ def action
16
+ 'sleep'
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,84 @@
1
+ module Moceansdk
2
+ module Modules
3
+ module Voice
4
+
5
+ class Voice < Moceansdk::Modules::AbstractClient
6
+ def initialize(obj_auth, transmitter)
7
+ super(obj_auth, transmitter)
8
+ @required_fields = ['mocean-api-key', 'mocean-api-secret', 'mocean-to']
9
+ end
10
+
11
+ def to=(param)
12
+ @params['mocean-to'] = param
13
+ end
14
+
15
+ def event_url=(param)
16
+ @params['mocean-event-url'] = param
17
+ end
18
+
19
+ def mocean_command=(param)
20
+ if param.is_a? McBuilder
21
+ @params['mocean-command'] = JSON.generate(param.build)
22
+ elsif param.is_a? McObject::AbstractMc
23
+ @params['mocean-command'] = JSON.generate([param.get_request_data])
24
+ elsif param.is_a? Array
25
+ @params['mocean-command'] = JSON.generate(param)
26
+ else
27
+ @params['mocean-command'] = param
28
+ end
29
+ end
30
+
31
+ def resp_format=(param)
32
+ @params['mocean-resp-format'] = param
33
+ end
34
+
35
+ def call(params = {})
36
+ sym_params = Moceansdk::Utils.convert_to_symbol_hash(params)
37
+
38
+ unless sym_params[:'mocean-command'].nil?
39
+ mc = sym_params[:'mocean-command']
40
+ sym_params.delete(:'mocean-command')
41
+ self.mocean_command = mc
42
+ end
43
+
44
+ create(sym_params)
45
+ create_final_params
46
+ required_field_set?
47
+
48
+ @transmitter.post('/voice/dial', @params)
49
+ end
50
+
51
+ def hangup(call_uuid)
52
+ @required_fields = ['mocean-api-key', 'mocean-api-secret', 'mocean-call-uuid']
53
+
54
+ create({:'mocean-call-uuid' => call_uuid})
55
+ create_final_params
56
+ required_field_set?
57
+
58
+ @transmitter.post("/voice/hangup", @params)
59
+ end
60
+
61
+ def recording(call_uuid)
62
+ @required_fields = ['mocean-api-key', 'mocean-api-secret', 'mocean-call-uuid']
63
+
64
+ create({:'mocean-call-uuid' => call_uuid})
65
+ create_final_params
66
+ required_field_set?
67
+
68
+ response = @transmitter.request('get', '/voice/rec', @params)
69
+
70
+ if response['Content-Type'] === 'audio/mpeg'
71
+ hashed_res = HashExtended.new.merge({'recording_buffer': response.to_s, 'filename': "#{call_uuid}.mp3"})
72
+ return hashed_res.to_dot
73
+ end
74
+
75
+ # this method will raise exception if there's error
76
+ processed_response = Moceansdk::Modules::ResponseFactory.create_object(response.to_s)
77
+ processed_response.raw_response = response.to_s
78
+ raise Moceansdk::Exceptions::MoceanError.new(processed_response['err_msg'], processed_response)
79
+ end
80
+ end
81
+
82
+ end
83
+ end
84
+ end
@@ -4,6 +4,12 @@ module Moceansdk
4
4
  def self.nil_or_empty?(str)
5
5
  str.nil? || str.empty?
6
6
  end
7
+
8
+ def self.convert_to_symbol_hash(str_hash)
9
+ str_hash.each_with_object({}) do |(k, v), memo|
10
+ memo[k.to_sym] = v
11
+ end
12
+ end
7
13
  end
8
14
 
9
15
  end
@@ -1,3 +1,3 @@
1
1
  module Moceansdk
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.add_dependency('xml-simple', '~> 1.1')
18
18
  s.add_development_dependency('codecov', '>= 0.1.10')
19
19
  s.add_development_dependency('minitest', '~> 5.0')
20
- s.add_development_dependency('rake', '~> 12.0')
20
+ s.add_development_dependency('rake', '~> 13.0')
21
21
  s.add_development_dependency('webmock', '~> 3.6')
22
22
  s.require_path = 'lib'
23
23
  s.metadata = {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moceansdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micro Ocean Technologies Sdn Bhd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-30 00:00:00.000000000 Z
11
+ date: 2019-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hash_dot
@@ -92,14 +92,14 @@ dependencies:
92
92
  requirements:
93
93
  - - "~>"
94
94
  - !ruby/object:Gem::Version
95
- version: '12.0'
95
+ version: '13.0'
96
96
  type: :development
97
97
  prerelease: false
98
98
  version_requirements: !ruby/object:Gem::Requirement
99
99
  requirements:
100
100
  - - "~>"
101
101
  - !ruby/object:Gem::Version
102
- version: '12.0'
102
+ version: '13.0'
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: webmock
105
105
  requirement: !ruby/object:Gem::Requirement
@@ -140,6 +140,16 @@ files:
140
140
  - lib/moceansdk/modules/number_lookup/number_lookup.rb
141
141
  - lib/moceansdk/modules/response_factory.rb
142
142
  - lib/moceansdk/modules/transmitter.rb
143
+ - lib/moceansdk/modules/voice/mc.rb
144
+ - lib/moceansdk/modules/voice/mc_builder.rb
145
+ - lib/moceansdk/modules/voice/mc_object/abstract_mc.rb
146
+ - lib/moceansdk/modules/voice/mc_object/collect.rb
147
+ - lib/moceansdk/modules/voice/mc_object/dial.rb
148
+ - lib/moceansdk/modules/voice/mc_object/play.rb
149
+ - lib/moceansdk/modules/voice/mc_object/record.rb
150
+ - lib/moceansdk/modules/voice/mc_object/say.rb
151
+ - lib/moceansdk/modules/voice/mc_object/sleep.rb
152
+ - lib/moceansdk/modules/voice/voice.rb
143
153
  - lib/moceansdk/utils.rb
144
154
  - lib/moceansdk/version.rb
145
155
  - moceansdk.gemspec