moceansdk 0.1.7 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE.txt +21 -21
- data/README.md +30 -13
- data/lib/moceansdk.rb +28 -6
- data/lib/moceansdk/auth/abstract_auth.rb +15 -0
- data/lib/moceansdk/auth/basic.rb +29 -0
- data/lib/moceansdk/client.rb +58 -10
- data/lib/moceansdk/exceptions/mocean_error.rb +18 -0
- data/lib/moceansdk/exceptions/required_field_exception.rb +8 -0
- data/lib/moceansdk/modules/abstact_client.rb +54 -0
- data/lib/moceansdk/modules/account/balance.rb +24 -20
- data/lib/moceansdk/modules/account/pricing.rb +35 -35
- data/lib/moceansdk/modules/message/channel.rb +12 -0
- data/lib/moceansdk/modules/message/message_status.rb +27 -22
- data/lib/moceansdk/modules/message/sms.rb +71 -93
- data/lib/moceansdk/modules/message/verify_request.rb +79 -56
- data/lib/moceansdk/modules/message/verify_validate.rb +31 -35
- data/lib/moceansdk/modules/number_lookup/number_lookup.rb +34 -0
- data/lib/moceansdk/modules/response_factory.rb +50 -0
- data/lib/moceansdk/modules/transmitter.rb +92 -0
- data/lib/moceansdk/utils.rb +9 -0
- data/lib/moceansdk/version.rb +3 -3
- data/moceansdk.gemspec +25 -19
- metadata +56 -19
- data/lib/moceansdk/init.rb +0 -4
- data/lib/moceansdk/mocean.rb +0 -52
- data/lib/moceansdk/modules/abstract.rb +0 -142
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d60c2b5a6d3baee166f95851e6882d58ecff0bbca91855fa471d3cb5e129523e
|
4
|
+
data.tar.gz: 0551d8738ac463a0ae9bf3e9a0836a5aa82302c40513d9e03d79126ccee29efd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29e7c9f0e5a578d8bc1f0bc1afce8ae3179a928a850677ea212bff9fdebb3c0a93d7f67e49f2294bdc5d5cfd3e315492653f9e87828d1c4296202ee8318b5922
|
7
|
+
data.tar.gz: 489e052c4e49ac0ddf96a3d27411e2ca51c3cec7d05d02c84998c54d4fa0ce2cb3f9914a1fc49c5a6d1d3ec4952f147b05ae04627dcbc9118bae916442efc73c
|
data/LICENSE.txt
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
The MIT License (MIT)
|
2
|
-
|
3
|
-
Copyright (c) 2018 Micro Ocean Technologies Sdn Bhd
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
13
|
-
all copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
THE SOFTWARE.
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018-2019 Micro Ocean Technologies Sdn Bhd
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
MoceanAPI Client Library for Ruby
|
2
2
|
============================
|
3
|
-
[![Gem Version](https://
|
3
|
+
[![Gem Version](https://img.shields.io/gem/v/moceansdk.svg)](https://rubygems.org/gems/moceansdk)
|
4
|
+
[![build status](https://img.shields.io/travis/com/MoceanAPI/mocean-sdk-ruby.svg)](https://travis-ci.com/MoceanAPI/mocean-sdk-ruby)
|
5
|
+
[![codecov](https://img.shields.io/codecov/c/github/MoceanAPI/mocean-sdk-ruby.svg)](https://codecov.io/gh/MoceanAPI/mocean-sdk-ruby)
|
6
|
+
[![codacy](https://img.shields.io/codacy/grade/5d5f7f9e7b1c4e888636a244283bf6f7.svg)](https://app.codacy.com/project/MoceanAPI/mocean-sdk-ruby/dashboard)
|
7
|
+
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
|
8
|
+
[![total downloads](https://img.shields.io/gem/dt/moceansdk.svg)](https://rubygems.org/gems/moceansdk)
|
4
9
|
|
5
10
|
This is the Ruby client library for use Mocean's API. To use this, you'll need a Mocean account. Sign up [for free at
|
6
11
|
moceanapi.com][signup].
|
@@ -24,34 +29,46 @@ gem install moceansdk
|
|
24
29
|
Create a client with your API key and secret:
|
25
30
|
|
26
31
|
```ruby
|
27
|
-
require
|
32
|
+
require 'moceansdk'
|
28
33
|
|
29
|
-
|
30
|
-
mocean =
|
34
|
+
credential = Moceansdk::Auth::Basic.new("API_KEY_HERE", "API_SECRET_HERE")
|
35
|
+
mocean = Moceansdk::Client.new(credential)
|
31
36
|
```
|
32
37
|
|
33
38
|
## Example
|
34
39
|
|
35
|
-
To use [Mocean's SMS API][doc_sms] to send an SMS message, call the `mocean.sms.
|
40
|
+
To use [Mocean's SMS API][doc_sms] to send an SMS message, call the `mocean.sms.send()` method.
|
36
41
|
|
37
42
|
The API can be called directly, using a simple array of parameters, the keys match the [parameters of the API][doc_sms].
|
38
43
|
|
39
44
|
```ruby
|
40
|
-
res = mocean.sms.
|
41
|
-
|
42
|
-
"mocean-from"
|
43
|
-
"mocean-to"
|
44
|
-
|
45
|
+
res = mocean.sms.send({
|
46
|
+
"mocean-text": 'Hello World',
|
47
|
+
"mocean-from": 'MOCEAN',
|
48
|
+
"mocean-to": '60123456789'
|
49
|
+
})
|
45
50
|
|
46
51
|
puts res
|
47
52
|
```
|
48
53
|
|
54
|
+
### Responses
|
55
|
+
|
56
|
+
For your convenient, the API response has been parsed to `Hash` using [hash_dot](https://github.com/adsteel/hash_dot) package.
|
57
|
+
```ruby
|
58
|
+
puts res # show full response string
|
59
|
+
puts res.status # show response status, '0' in this case
|
60
|
+
puts res['status'] # same as above
|
61
|
+
```
|
62
|
+
|
63
|
+
## Documentation
|
64
|
+
|
65
|
+
Kindly visit [MoceanApi Docs][doc_main] for more usage
|
66
|
+
|
49
67
|
## License
|
50
68
|
|
51
69
|
This library is released under the [MIT License][license]
|
52
70
|
|
53
71
|
[signup]: https://dashboard.moceanapi.com/register?medium=github&campaign=sdk-ruby
|
54
|
-
[
|
55
|
-
[
|
56
|
-
[doc_verify]: https://docs.moceanapi.com/?ruby#overview-3
|
72
|
+
[doc_main]: https://moceanapi.com/docs/?ruby
|
73
|
+
[doc_sms]: https://moceanapi.com/docs/?ruby#send-sms
|
57
74
|
[license]: LICENSE.txt
|
data/lib/moceansdk.rb
CHANGED
@@ -1,6 +1,28 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
require 'http'
|
2
|
+
require 'json'
|
3
|
+
require 'hash_dot'
|
4
|
+
require 'xmlsimple'
|
5
|
+
|
6
|
+
require 'moceansdk/auth/abstract_auth'
|
7
|
+
require 'moceansdk/auth/basic'
|
8
|
+
require 'moceansdk/exceptions/mocean_error'
|
9
|
+
require 'moceansdk/exceptions/required_field_exception'
|
10
|
+
|
11
|
+
require 'moceansdk/modules/abstact_client'
|
12
|
+
require 'moceansdk/modules/response_factory'
|
13
|
+
require 'moceansdk/modules/transmitter'
|
14
|
+
|
15
|
+
require 'moceansdk/modules/account/balance'
|
16
|
+
require 'moceansdk/modules/account/pricing'
|
17
|
+
|
18
|
+
require 'moceansdk/modules/message/channel'
|
19
|
+
require 'moceansdk/modules/message/message_status'
|
20
|
+
require 'moceansdk/modules/message/sms'
|
21
|
+
require 'moceansdk/modules/message/verify_request'
|
22
|
+
require 'moceansdk/modules/message/verify_validate'
|
23
|
+
|
24
|
+
require 'moceansdk/modules/number_lookup/number_lookup'
|
25
|
+
|
26
|
+
require 'moceansdk/utils'
|
27
|
+
require 'moceansdk/version'
|
28
|
+
require 'moceansdk/client'
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Moceansdk
|
2
|
+
module Auth
|
3
|
+
|
4
|
+
class AbstractAuth
|
5
|
+
def auth_method
|
6
|
+
raise NotImplementedError, 'AbstractAuth is a abstract class'
|
7
|
+
end
|
8
|
+
|
9
|
+
def params
|
10
|
+
raise NotImplementedError, 'AbstractAuth is a abstract class'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Moceansdk
|
2
|
+
module Auth
|
3
|
+
|
4
|
+
class Basic < AbstractAuth
|
5
|
+
def initialize(api_key = nil, api_secret = nil)
|
6
|
+
@params = {}
|
7
|
+
@params['mocean-api-key'] = api_key unless api_key.nil?
|
8
|
+
@params['mocean-api-secret'] = api_secret unless api_secret.nil?
|
9
|
+
end
|
10
|
+
|
11
|
+
def api_key=(api_key)
|
12
|
+
@params['mocean-api-key'] = api_key
|
13
|
+
end
|
14
|
+
|
15
|
+
def api_secret=(api_secret)
|
16
|
+
@params['mocean-api-secret'] = api_secret
|
17
|
+
end
|
18
|
+
|
19
|
+
def auth_method
|
20
|
+
'basic'
|
21
|
+
end
|
22
|
+
|
23
|
+
def params
|
24
|
+
@params
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
data/lib/moceansdk/client.rb
CHANGED
@@ -1,14 +1,62 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module Moceansdk
|
2
|
+
|
3
|
+
class Client
|
4
|
+
def initialize(obj_auth, options = nil)
|
5
|
+
unless obj_auth.is_a? Auth::AbstractAuth
|
6
|
+
raise Exceptions::MoceanError, 'auth object must extend AbstractAuth'
|
7
|
+
end
|
8
|
+
|
9
|
+
if obj_auth.auth_method.casecmp('basic').zero?
|
10
|
+
if Utils.nil_or_empty?(obj_auth.params['mocean-api-key']) || Utils.nil_or_empty?(obj_auth.params['mocean-api-secret'])
|
11
|
+
raise Exceptions::RequiredFieldException, "api key and api secret for client object can't be empty"
|
12
|
+
end
|
13
|
+
else
|
14
|
+
raise Exceptions::MoceanError, 'unsupported auth method'
|
15
|
+
end
|
16
|
+
|
17
|
+
@transmitter = if options.is_a? Modules::Transmitter
|
18
|
+
options
|
19
|
+
else
|
20
|
+
Modules::Transmitter.new(options)
|
21
|
+
end
|
22
|
+
|
23
|
+
@obj_auth = obj_auth
|
24
|
+
end
|
25
|
+
|
26
|
+
def sms
|
27
|
+
Modules::Message::Sms.new(@obj_auth, @transmitter)
|
28
|
+
end
|
29
|
+
|
30
|
+
def flash_sms
|
31
|
+
sms = Modules::Message::Sms.new(@obj_auth, @transmitter)
|
32
|
+
sms.mclass = 1
|
33
|
+
sms.alt_dcs = 1
|
34
|
+
sms
|
35
|
+
end
|
36
|
+
|
37
|
+
def pricing
|
38
|
+
Modules::Account::Pricing.new(@obj_auth, @transmitter)
|
6
39
|
end
|
7
40
|
|
8
|
-
def
|
9
|
-
|
41
|
+
def balance
|
42
|
+
Modules::Account::Balance.new(@obj_auth, @transmitter)
|
10
43
|
end
|
11
|
-
|
12
|
-
|
44
|
+
|
45
|
+
def message_status
|
46
|
+
Modules::Message::MessageStatus.new(@obj_auth, @transmitter)
|
13
47
|
end
|
14
|
-
|
48
|
+
|
49
|
+
def verify_request
|
50
|
+
Modules::Message::VerifyRequest.new(@obj_auth, @transmitter)
|
51
|
+
end
|
52
|
+
|
53
|
+
def verify_validate
|
54
|
+
Modules::Message::VerifyValidate.new(@obj_auth, @transmitter)
|
55
|
+
end
|
56
|
+
|
57
|
+
def number_lookup
|
58
|
+
Modules::NumberLookup::NumberLookup.new(@obj_auth, @transmitter)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Moceansdk
|
2
|
+
module Exceptions
|
3
|
+
|
4
|
+
class MoceanError < StandardError
|
5
|
+
attr_reader :error_response
|
6
|
+
|
7
|
+
def initialize(msg, error_response = nil)
|
8
|
+
if error_response.nil?
|
9
|
+
super(msg)
|
10
|
+
else
|
11
|
+
super(error_response['err_msg'])
|
12
|
+
@error_response = error_response
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Moceansdk
|
2
|
+
module Modules
|
3
|
+
|
4
|
+
class AbstractClient
|
5
|
+
attr_accessor :params
|
6
|
+
|
7
|
+
def initialize(obj_auth, transmitter)
|
8
|
+
@params = obj_auth.params
|
9
|
+
@transmitter = transmitter
|
10
|
+
end
|
11
|
+
|
12
|
+
def create(params = {})
|
13
|
+
@params = @params.merge(params) if params.is_a? Hash
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_final_params
|
17
|
+
final_params = {}
|
18
|
+
@params.each do |key, value|
|
19
|
+
unless value.nil?
|
20
|
+
param_prefix_set?(key) ? final_params[key] = value : final_params["mocean-#{key}"] = value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# convert string hash to symbol hash
|
25
|
+
@params = final_params.inject({}) {|memo, (k, v)| memo[k.to_sym] = v; memo}
|
26
|
+
end
|
27
|
+
|
28
|
+
def param_prefix_set?(key)
|
29
|
+
cloned_key = if key.is_a? String
|
30
|
+
key
|
31
|
+
else
|
32
|
+
key.to_s
|
33
|
+
end
|
34
|
+
|
35
|
+
return false if cloned_key.scan(/^mocean-/i).empty?
|
36
|
+
|
37
|
+
true
|
38
|
+
end
|
39
|
+
|
40
|
+
def required_field_set?
|
41
|
+
if @required_fields.is_a?(Array) && !@required_fields.empty?
|
42
|
+
@required_fields.each do |field|
|
43
|
+
if @params[:"#{field}"].nil?
|
44
|
+
raise Moceansdk::Exceptions::RequiredFieldException, "#{field} is mandatory field, can't leave empty"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
true
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -1,22 +1,26 @@
|
|
1
|
-
|
1
|
+
module Moceansdk
|
2
|
+
module Modules
|
3
|
+
module Account
|
4
|
+
|
5
|
+
class Balance < Moceansdk::Modules::AbstractClient
|
6
|
+
def initialize(obj_auth, transmitter)
|
7
|
+
super(obj_auth, transmitter)
|
8
|
+
@required_fields = ['mocean-api-key', 'mocean-api-secret']
|
9
|
+
end
|
10
|
+
|
11
|
+
def resp_format=(param)
|
12
|
+
@params['mocean-resp-format'] = param
|
13
|
+
end
|
14
|
+
|
15
|
+
def inquiry(params = {})
|
16
|
+
create(params)
|
17
|
+
create_final_params
|
18
|
+
required_field_set?
|
19
|
+
|
20
|
+
@transmitter.get('/account/balance', @params)
|
21
|
+
end
|
22
|
+
end
|
2
23
|
|
3
|
-
class Balance < MoceanFactory
|
4
|
-
def initialize client
|
5
|
-
super(client)
|
6
|
-
@required_fields = ['mocean-api-key','mocean-api-secret']
|
7
|
-
end
|
8
|
-
|
9
|
-
def setRespFormat param
|
10
|
-
@params['mocean-resp-format'] = param
|
11
|
-
return self
|
12
|
-
end
|
13
|
-
|
14
|
-
def inquiry params = {}
|
15
|
-
create(params)
|
16
|
-
createFinalParams
|
17
|
-
isRequiredFieldsSet
|
18
|
-
response = Transmitter.new('/rest/1/account/balance','get', @params)
|
19
|
-
reset
|
20
|
-
return response.getResponse()
|
21
24
|
end
|
22
|
-
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,38 +1,38 @@
|
|
1
|
-
|
1
|
+
module Moceansdk
|
2
|
+
module Modules
|
3
|
+
module Account
|
4
|
+
|
5
|
+
class Pricing < Moceansdk::Modules::AbstractClient
|
6
|
+
def initialize(obj_auth, transmitter)
|
7
|
+
super(obj_auth, transmitter)
|
8
|
+
@required_fields = ['mocean-api-key', 'mocean-api-secret']
|
9
|
+
end
|
10
|
+
|
11
|
+
def mcc=(param)
|
12
|
+
@params['mocean-mcc'] = param
|
13
|
+
end
|
14
|
+
|
15
|
+
def mnc=(param)
|
16
|
+
@params['mocean-mnc'] = param
|
17
|
+
end
|
18
|
+
|
19
|
+
def delimiter=(param)
|
20
|
+
@params['mocean-delimiter'] = param
|
21
|
+
end
|
22
|
+
|
23
|
+
def resp_format=(param)
|
24
|
+
@params['mocean-resp-format'] = param
|
25
|
+
end
|
26
|
+
|
27
|
+
def inquiry(params = {})
|
28
|
+
create(params)
|
29
|
+
create_final_params
|
30
|
+
required_field_set?
|
31
|
+
|
32
|
+
@transmitter.get('/account/pricing', @params)
|
33
|
+
end
|
34
|
+
end
|
2
35
|
|
3
|
-
class Pricing < MoceanFactory
|
4
|
-
|
5
|
-
def initialize client
|
6
|
-
super(client)
|
7
|
-
required_fields = ['mocean-api-key','mocean-api-secret']
|
8
|
-
end
|
9
|
-
|
10
|
-
def setMcc param
|
11
|
-
@params['mocean-mcc'] = param
|
12
|
-
return self
|
13
|
-
end
|
14
|
-
|
15
|
-
def setMnc param
|
16
|
-
@params['mocean-mnc'] = param
|
17
|
-
return self
|
18
|
-
end
|
19
|
-
|
20
|
-
def setDelimiter param
|
21
|
-
@params['mocean-delimiter'] = param
|
22
|
-
return self
|
23
|
-
end
|
24
|
-
|
25
|
-
def setRespFormat param
|
26
|
-
@params['mocean-resp-format'] = param
|
27
|
-
return self
|
28
|
-
end
|
29
|
-
|
30
|
-
def inquiry params = {}
|
31
|
-
create(params)
|
32
|
-
createFinalParams
|
33
|
-
isRequiredFieldsSet
|
34
|
-
response = Transmitter.new('/rest/1/account/pricing','get',@params)
|
35
|
-
reset
|
36
|
-
return response.getResponse()
|
37
36
|
end
|
37
|
+
end
|
38
38
|
end
|