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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f6532c4ee47058e7a4759f48927843506884547f
4
- data.tar.gz: 32fdced53ca4cfcf0f89e2764bd1239d7184845a
2
+ SHA256:
3
+ metadata.gz: d60c2b5a6d3baee166f95851e6882d58ecff0bbca91855fa471d3cb5e129523e
4
+ data.tar.gz: 0551d8738ac463a0ae9bf3e9a0836a5aa82302c40513d9e03d79126ccee29efd
5
5
  SHA512:
6
- metadata.gz: 48297b9b9172925ac6a8a548c5bb167f278a939a553f1cb3e1bdfda9baa1e4b333c39cab8711161be73e65fb2a4226d7ee666f94cbf382cbf1be0664ed644b14
7
- data.tar.gz: 45b77996ec94fa4a3927ddaeb57a8f3f422d8b960fc2f4b2c1e37ed5ce7e23083c6605e92fa074894ddd74154913310fd74f48e89accfa425ded1e5895fb0c26
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://badge.fury.io/rb/moceansdk.svg)](https://badge.fury.io/rb/moceansdk)
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 "moceanapi/init"
32
+ require 'moceansdk'
28
33
 
29
- token = Client.new("API_KEY_HERE", "API_SECRET_HERE")
30
- mocean = Mocean.new(token)
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.create().send()` method.
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.create({
41
- "mocean-text"=>'Hello World',
42
- "mocean-from"=>'MOCEAN',
43
- "mocean-to"=>'60123456789'
44
- }).send()
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
- [doc_sms]: https://docs.moceanapi.com/?ruby#send-sms
55
- [doc_inbound]: https://docs.moceanapi.com/?ruby#receive-sms
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 "moceansdk/version"
2
- require "moceansdk/init"
3
-
4
- module Moceansdk
5
-
6
- end
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
@@ -1,14 +1,62 @@
1
- class Client
2
- attr_reader :api_key,:api_secret
3
- def initialize api_key = '',api_secret = ''
4
- @api_key = api_key
5
- @api_secret = api_secret
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 set_api_key(api_key)
9
- @api_key = api_key
41
+ def balance
42
+ Modules::Account::Balance.new(@obj_auth, @transmitter)
10
43
  end
11
- def set_api_secret(api_secret)
12
- @api_secret = api_secret
44
+
45
+ def message_status
46
+ Modules::Message::MessageStatus.new(@obj_auth, @transmitter)
13
47
  end
14
- end
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,8 @@
1
+ module Moceansdk
2
+ module Exceptions
3
+
4
+ class RequiredFieldException < MoceanError
5
+ end
6
+
7
+ end
8
+ 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
- require_relative "../abstract"
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
- require_relative "../abstract"
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