plivo 0.3.19 → 4.0.0.beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +14 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +10 -0
  5. data/AUTHORS.md +4 -0
  6. data/CHANGELOG.md +19 -0
  7. data/Gemfile +3 -0
  8. data/LICENSE.txt +19 -0
  9. data/README.md +105 -24
  10. data/Rakefile +7 -0
  11. data/lib/plivo.rb +9 -815
  12. data/lib/plivo/base.rb +9 -0
  13. data/lib/plivo/base/resource.rb +85 -0
  14. data/lib/plivo/base/resource_interface.rb +93 -0
  15. data/lib/plivo/base/response.rb +29 -0
  16. data/lib/plivo/exceptions.rb +50 -0
  17. data/lib/plivo/resources.rb +14 -0
  18. data/lib/plivo/resources/accounts.rb +174 -0
  19. data/lib/plivo/resources/applications.rb +233 -0
  20. data/lib/plivo/resources/calls.rb +492 -0
  21. data/lib/plivo/resources/conferences.rb +371 -0
  22. data/lib/plivo/resources/endpoints.rb +130 -0
  23. data/lib/plivo/resources/messages.rb +178 -0
  24. data/lib/plivo/resources/numbers.rb +302 -0
  25. data/lib/plivo/resources/pricings.rb +43 -0
  26. data/lib/plivo/resources/recordings.rb +114 -0
  27. data/lib/plivo/rest_client.rb +199 -0
  28. data/lib/plivo/utils.rb +107 -0
  29. data/lib/plivo/version.rb +3 -0
  30. data/lib/plivo/xml.rb +27 -0
  31. data/lib/plivo/xml/conference.rb +20 -0
  32. data/lib/plivo/xml/dial.rb +16 -0
  33. data/lib/plivo/xml/dtmf.rb +13 -0
  34. data/lib/plivo/xml/element.rb +83 -0
  35. data/lib/plivo/xml/get_digits.rb +15 -0
  36. data/lib/plivo/xml/hangup.rb +12 -0
  37. data/lib/plivo/xml/message.rb +13 -0
  38. data/lib/plivo/xml/number.rb +13 -0
  39. data/lib/plivo/xml/play.rb +13 -0
  40. data/lib/plivo/xml/plivo_xml.rb +19 -0
  41. data/lib/plivo/xml/pre_answer.rb +12 -0
  42. data/lib/plivo/xml/record.rb +17 -0
  43. data/lib/plivo/xml/redirect.rb +13 -0
  44. data/lib/plivo/xml/response.rb +21 -0
  45. data/lib/plivo/xml/speak.rb +17 -0
  46. data/lib/plivo/xml/user.rb +13 -0
  47. data/lib/plivo/xml/wait.rb +12 -0
  48. data/plivo.gemspec +44 -0
  49. metadata +134 -45
  50. data/ext/mkrf_conf.rb +0 -9
data/lib/plivo/base.rb ADDED
@@ -0,0 +1,9 @@
1
+ require_relative 'base/resource_interface'
2
+ require_relative 'base/resource'
3
+ require_relative 'base/response'
4
+
5
+ module Plivo
6
+ module Base
7
+ PLIVO_API_URL = 'https://api.plivo.com'.freeze
8
+ end
9
+ end
@@ -0,0 +1,85 @@
1
+ module Plivo
2
+ module Base
3
+ class Resource
4
+ attr_reader :id
5
+ include Plivo::Utils
6
+
7
+ def initialize(client, options = nil)
8
+ configure_client(client)
9
+ configure_options(options) if options
10
+ configure_resource_uri
11
+ end
12
+
13
+ private
14
+
15
+ def configure_client(client)
16
+ valid_param?(:client, client, RestClient, true)
17
+ @_client = client
18
+ end
19
+
20
+ def configure_options(options)
21
+ valid_param?(:options, options, Hash, false)
22
+ @id = options[:resource_id] if options.key?(:resource_id)
23
+ parse_and_set(options[:resource_json]) if options.key?(:resource_json)
24
+ end
25
+
26
+ def configure_resource_uri
27
+ to_join = @id ? [@_client.auth_id, @_name, @id] : [@_client.auth_id, @_name]
28
+ to_join = ['', 'v1', 'Account'] << to_join
29
+
30
+ if @_name == 'Account'
31
+ @id = @_client.auth_id
32
+ to_join = ['', 'v1', 'Account', @id]
33
+ end
34
+
35
+ to_join << ''
36
+ @_resource_uri = to_join.join('/')
37
+ end
38
+
39
+ def parse_and_set(resource_json)
40
+ return unless resource_json
41
+
42
+ valid_param?(:resource_json, resource_json, Hash, true)
43
+
44
+ resource_json.each do |k, v|
45
+ instance_variable_set("@#{k}", v)
46
+ self.class.send(:attr_reader, k)
47
+ end
48
+
49
+ return unless @_identifier_string && (resource_json.key? @_identifier_string)
50
+ @id = resource_json[@_identifier_string]
51
+ end
52
+
53
+ def perform_update(params)
54
+ unless @id
55
+ raise_invalid_request("Cannot update a #{@_name} resource "\
56
+ 'without an identifier')
57
+ end
58
+
59
+ response_json = @_client.send_request(@_resource_uri, 'POST', params)
60
+
61
+ parse_and_set(params)
62
+ parse_and_set(response_json)
63
+ self
64
+ end
65
+
66
+ def perform_action(action = nil, method = 'GET', params = nil, parse = false)
67
+ resource_path = action ? @_resource_uri + action + '/' : @_resource_uri
68
+ response = @_client.send_request(resource_path, method, params)
69
+ parse ? parse_and_set(response) : self
70
+ method == 'POST' ? parse_and_set(params) : self
71
+ self
72
+ end
73
+
74
+ def perform_delete
75
+ unless @id
76
+ raise_invalid_request("Cannot delete a #{@_name} resource "\
77
+ 'without an identifier')
78
+ end
79
+
80
+ Response.new(@_client.send_request(@_resource_uri, 'DELETE', nil),
81
+ @_identifier_string)
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,93 @@
1
+ module Plivo
2
+ module Base
3
+ class ResourceInterface
4
+ include Plivo::Utils
5
+ def initialize(client, resource_list_json = nil)
6
+ configure_client(client)
7
+ configure_resource_uri
8
+ parse_and_set(resource_list_json) if resource_list_json
9
+ end
10
+
11
+ private
12
+
13
+ def configure_client(client)
14
+ valid_param?(:client, client, RestClient, true)
15
+ @_client = client
16
+ end
17
+
18
+ def configure_resource_uri
19
+ to_join = ['', 'v1', 'Account', @_client.auth_id, @_name, '']
20
+ to_join = ['', 'v1', 'Account', ''] if @_name == 'Account'
21
+
22
+ @_resource_uri = to_join.join('/')
23
+ end
24
+
25
+ def parse_and_set(resource_list_json)
26
+ return unless resource_list_json
27
+
28
+ valid_param?(:resource_list_json, resource_list_json, Hash, true)
29
+
30
+ resource_list_json.each do |k, v|
31
+ if k == 'objects'
32
+ parse_and_set_list(resource_list_json['objects'])
33
+ elsif k == 'meta'
34
+ @_meta = resource_list_json['meta']
35
+ else
36
+ instance_variable_set("@#{k}", v)
37
+ self.class.send(:attr_reader, k)
38
+ end
39
+ end
40
+ end
41
+
42
+ def perform_get(identifier, params = nil)
43
+ valid_param?(:identifier, identifier, [String, Symbol], true)
44
+ response_json = @_client.send_request(@_resource_uri + identifier.to_s + '/', 'GET', params)
45
+ @_resource_type.new(@_client, resource_json: response_json)
46
+ end
47
+
48
+ def perform_get_without_identifier(params)
49
+ valid_param?(:params, params, Hash, true)
50
+ response_json = @_client.send_request(@_resource_uri, 'GET', params)
51
+ @_resource_type.new(@_client, resource_json: response_json)
52
+ end
53
+
54
+ def perform_create(params)
55
+ Response.new(
56
+ @_client.send_request(@_resource_uri, 'POST', params),
57
+ @_identifier_string
58
+ )
59
+ end
60
+
61
+ def perform_post(params)
62
+ response_json = @_client.send_request(@_resource_uri, 'POST', params)
63
+
64
+ parse_and_set(response_json)
65
+ self
66
+ end
67
+
68
+ def parse_and_set_list(list_json)
69
+ return unless list_json
70
+ valid_param?(:list_json, list_json, Array, false)
71
+ @_resource_list = list_json.map do |resource|
72
+ @_resource_type.new(@_client, resource_json: resource)
73
+ end
74
+ end
75
+
76
+ def perform_list(params = nil)
77
+ response_json = @_client.send_request(@_resource_uri, 'GET', params)
78
+ parse_and_set(response_json)
79
+ {
80
+ api_id: @api_id,
81
+ meta: @_meta,
82
+ objects: @_resource_list
83
+ }
84
+ end
85
+
86
+ def perform_list_without_object(params = nil)
87
+ response_json = @_client.send_request(@_resource_uri, 'GET', params)
88
+ parse_and_set(response_json)
89
+ response_json
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,29 @@
1
+ module Plivo
2
+ module Base
3
+ ##
4
+ # A class to provide a blanket response based on what is
5
+ # being received from Plivo servers.
6
+ #
7
+ # This will be used only during POST and DELETE requests.
8
+ class Response
9
+ ##
10
+ # Instantiating a new instance requires a response_hash
11
+ # The id_symbol should contain a Symbol that represents
12
+ # the identifier of the resource for which this response
13
+ # is being generated for.
14
+
15
+ def initialize(response_hash, id_symbol = nil)
16
+ return unless response_hash
17
+
18
+ response_hash.each do |k, v|
19
+ instance_variable_set("@#{k}", v)
20
+ self.class.send(:attr_reader, k)
21
+ end
22
+ return unless id_symbol && response_hash.key?(id_symbol)
23
+
24
+ self.class.send(:attr_reader, :id)
25
+ @id = response_hash[id_symbol]
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,50 @@
1
+ module Plivo
2
+ ##
3
+ # The exceptions module that helps in passing on errors to the user in a better way.
4
+ module Exceptions
5
+ ##
6
+ # This is Plivo's base error and will be raised when there is an unknown
7
+ # error that is not covered by other errors.
8
+ PlivoRESTError = Class.new(StandardError)
9
+
10
+ ##
11
+ # This will be raised when A parameter is missing or is invalid while
12
+ # accessing resource.
13
+ ValidationError = Class.new(PlivoRESTError)
14
+ # 'A parameter is missing or is invalid while accessing resource'
15
+
16
+ ##
17
+ # This will be raised when there is an an error authenticating the request.
18
+ #
19
+ # It is because there were no authentication credentials found or passed
20
+ # or the auth credentials that were found or passed were incorrect.
21
+ AuthenticationError = Class.new(PlivoRESTError)
22
+
23
+ ##
24
+ # This will be raised when one or more parameters passed to Plivo servers
25
+ # is invalid.
26
+ #
27
+ # More specific error message will be displayed wherever possible.
28
+ InvalidRequestError = Class.new(PlivoRESTError)
29
+
30
+ ##
31
+ # This will be raised when there is a server error on the Plivo end.
32
+ #
33
+ # This is rarely encountered. You might want to catch this error and
34
+ # retry the request if needed.
35
+ #
36
+ # If retrying doesn't help, please contact https://support.plivo.com
37
+ # immediately.
38
+ PlivoServerError = Class.new(PlivoRESTError)
39
+
40
+ ##
41
+ # This will be raised when there is an XML generation error.
42
+ #
43
+ # Usually, the reason why this error is raised will be included.
44
+ PlivoXMLError = Class.new(PlivoRESTError)
45
+
46
+ ##
47
+ # This will be raised when there is an authentication error
48
+ ResourceNotFoundError = Class.new(PlivoRESTError)
49
+ end
50
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'resources/messages'
2
+ require_relative 'resources/accounts'
3
+ require_relative 'resources/applications'
4
+ require_relative 'resources/recordings'
5
+ require_relative 'resources/pricings'
6
+ require_relative 'resources/numbers'
7
+ require_relative 'resources/conferences'
8
+ require_relative 'resources/calls'
9
+ require_relative 'resources/endpoints'
10
+
11
+ module Plivo
12
+ module Resources
13
+ end
14
+ end
@@ -0,0 +1,174 @@
1
+ module Plivo
2
+ module Resources
3
+ include Plivo::Utils
4
+ class Subaccount < Base::Resource
5
+ def initialize(client, options = nil)
6
+ @_name = 'Subaccount'
7
+ @_identifier_string = 'auth_id'
8
+ super
9
+ end
10
+
11
+ def update(name, enabled = false)
12
+ valid_param?(:name, name, [String, Symbol], true)
13
+ valid_param?(:enabled, enabled, [TrueClass, FalseClass],
14
+ true, [true, false])
15
+
16
+ params = {
17
+ name: name,
18
+ enabled: enabled
19
+ }
20
+ perform_update(params)
21
+ end
22
+
23
+ def delete
24
+ perform_delete
25
+ end
26
+
27
+ def to_s
28
+ {
29
+ account: @account,
30
+ api_id: @api_id,
31
+ auth_id: @auth_id,
32
+ auth_token: @auth_token,
33
+ new_auth_token: @new_auth_token,
34
+ created: @created,
35
+ enabled: @enabled,
36
+ modified: @modified,
37
+ name: @name,
38
+ resource_uri: @resource_uri
39
+ }.to_s
40
+ end
41
+ end
42
+
43
+ # @!method get
44
+ # @!method create
45
+ # @!method list
46
+ class SubaccountInterface < Base::ResourceInterface
47
+ def initialize(client, resource_list_json = nil)
48
+ @_name = 'Subaccount'
49
+ @_resource_type = Subaccount
50
+ @_identifier_string = 'auth_id'
51
+ super
52
+ end
53
+
54
+ # @param [String] subaccount_id
55
+ def get(subaccount_id)
56
+ valid_subaccount?(subaccount_id, true)
57
+ perform_get(subaccount_id)
58
+ end
59
+
60
+ # @param [String] name
61
+ # @param [Boolean] enabled
62
+ def create(name, enabled = false)
63
+ valid_param?(:name, name, [String, Symbol], true)
64
+ valid_param?(:enabled, enabled, [TrueClass, FalseClass],
65
+ true, [true, false])
66
+
67
+ params = {
68
+ name: name,
69
+ enabled: enabled
70
+ }
71
+
72
+ perform_create(params)
73
+ end
74
+
75
+ # @param [Array] options
76
+ def list(options = nil)
77
+ return perform_list if options.nil?
78
+
79
+ params = {}
80
+
81
+ %i[offset limit].each do |param|
82
+ if options.key?(param) && valid_param?(param, options[param],
83
+ [Integer, Integer], true)
84
+ params[param] = options[param]
85
+ end
86
+ end
87
+
88
+ if options.key?(:limit) && (options[:limit] > 20 || options[:limit] <= 0)
89
+ raise_invalid_request('The maximum number of results that can be '\
90
+ "fetched is 20. limit can't be more than 20 or less than 1")
91
+ end
92
+
93
+ raise_invalid_request("Offset can't be negative") if options.key?(:offset) && options[:offset] < 0
94
+
95
+ perform_list(params)
96
+ end
97
+
98
+ def each
99
+ offset = 0
100
+ loop do
101
+ subaccount_list = list(offset: offset)
102
+ subaccount_list[:objects].each { |subaccount| yield subaccount }
103
+ offset += 20
104
+ return unless subaccount_list.length == 20
105
+ end
106
+ end
107
+
108
+ def update(subaccount_id, name, enabled = false)
109
+ Subaccount.new(@_client, resource_id: subaccount_id).update(name, enabled)
110
+ end
111
+
112
+ def delete(subaccount_id)
113
+ valid_subaccount?(subaccount_id, true)
114
+ Subaccount.new(@_client, resource_id: subaccount_id).delete
115
+ end
116
+ end
117
+
118
+ class Account < Base::Resource
119
+ def initialize(client, options = nil)
120
+ @_name = 'Account'
121
+ @_identifier_string = 'auth_id'
122
+ super
123
+ end
124
+
125
+ def update(details)
126
+ valid_param?(:details, details, Hash, true)
127
+
128
+ params = {}
129
+ %i[name city address].each do |param|
130
+ if details.key?(param) && valid_param?(param, details[param], [String, Symbol], true)
131
+ params[param] = details[param]
132
+ end
133
+ end
134
+
135
+ raise_invalid_request('One parameter of name, city and address is required') if params == {}
136
+ perform_update(params)
137
+ end
138
+
139
+ def to_s
140
+ {
141
+ account_type: @account_type,
142
+ address: @address,
143
+ api_id: @api_id,
144
+ auth_id: @auth_id,
145
+ auto_recharge: @auto_recharge,
146
+ billing_mode: @billing_mode,
147
+ cash_credits: @cash_credits,
148
+ city: @city,
149
+ name: @name,
150
+ resource_uri: @resource_uri,
151
+ state: @state,
152
+ timezone: @timezone
153
+ }.to_s
154
+ end
155
+ end
156
+
157
+ class AccountInterface < Base::ResourceInterface
158
+ def initialize(client, resource_list_json = nil)
159
+ @_name = 'Account'
160
+ @_resource_type = Account
161
+ @_identifier_string = 'auth_id'
162
+ super
163
+ end
164
+
165
+ def details
166
+ perform_get(@_client.auth_id)
167
+ end
168
+
169
+ def update(details)
170
+ Account.new(@_client, resource_id: @_client.auth_id).update(details)
171
+ end
172
+ end
173
+ end
174
+ end