vonage 7.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +190 -0
  3. data/README.md +191 -0
  4. data/lib/vonage.rb +29 -0
  5. data/lib/vonage/abstract_authentication.rb +9 -0
  6. data/lib/vonage/account.rb +61 -0
  7. data/lib/vonage/alerts.rb +72 -0
  8. data/lib/vonage/applications.rb +148 -0
  9. data/lib/vonage/applications/list_response.rb +11 -0
  10. data/lib/vonage/authentication_error.rb +6 -0
  11. data/lib/vonage/basic.rb +13 -0
  12. data/lib/vonage/bearer_token.rb +14 -0
  13. data/lib/vonage/client.rb +134 -0
  14. data/lib/vonage/client_error.rb +6 -0
  15. data/lib/vonage/config.rb +208 -0
  16. data/lib/vonage/conversations.rb +210 -0
  17. data/lib/vonage/conversations/events.rb +73 -0
  18. data/lib/vonage/conversations/legs.rb +30 -0
  19. data/lib/vonage/conversations/members.rb +104 -0
  20. data/lib/vonage/conversations/users.rb +93 -0
  21. data/lib/vonage/conversions.rb +19 -0
  22. data/lib/vonage/entity.rb +51 -0
  23. data/lib/vonage/error.rb +6 -0
  24. data/lib/vonage/errors.rb +51 -0
  25. data/lib/vonage/files.rb +26 -0
  26. data/lib/vonage/form_data.rb +11 -0
  27. data/lib/vonage/gsm7.rb +13 -0
  28. data/lib/vonage/http.rb +43 -0
  29. data/lib/vonage/json.rb +17 -0
  30. data/lib/vonage/jwt.rb +43 -0
  31. data/lib/vonage/key_secret_params.rb +20 -0
  32. data/lib/vonage/keys.rb +51 -0
  33. data/lib/vonage/logger.rb +60 -0
  34. data/lib/vonage/messages.rb +25 -0
  35. data/lib/vonage/namespace.rb +118 -0
  36. data/lib/vonage/number_insight.rb +140 -0
  37. data/lib/vonage/numbers.rb +196 -0
  38. data/lib/vonage/numbers/list_response.rb +11 -0
  39. data/lib/vonage/numbers/response.rb +8 -0
  40. data/lib/vonage/params.rb +27 -0
  41. data/lib/vonage/pricing.rb +30 -0
  42. data/lib/vonage/pricing_types.rb +18 -0
  43. data/lib/vonage/redact.rb +37 -0
  44. data/lib/vonage/response.rb +25 -0
  45. data/lib/vonage/secrets.rb +85 -0
  46. data/lib/vonage/secrets/list_response.rb +11 -0
  47. data/lib/vonage/server_error.rb +6 -0
  48. data/lib/vonage/signature.rb +53 -0
  49. data/lib/vonage/sms.rb +121 -0
  50. data/lib/vonage/tfa.rb +14 -0
  51. data/lib/vonage/user_agent.rb +16 -0
  52. data/lib/vonage/verify.rb +253 -0
  53. data/lib/vonage/version.rb +5 -0
  54. data/lib/vonage/voice.rb +250 -0
  55. data/lib/vonage/voice/dtmf.rb +26 -0
  56. data/lib/vonage/voice/list_response.rb +11 -0
  57. data/lib/vonage/voice/stream.rb +44 -0
  58. data/lib/vonage/voice/talk.rb +48 -0
  59. data/vonage.gemspec +26 -0
  60. metadata +155 -0
@@ -0,0 +1,118 @@
1
+ # typed: ignore
2
+ # frozen_string_literal: true
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ module Vonage
7
+ class Namespace
8
+ def initialize(config)
9
+ @config = config
10
+
11
+ @host = self.class.host == :api_host ? @config.api_host : @config.rest_host
12
+
13
+ @http = Net::HTTP.new(@host, Net::HTTP.https_default_port, p_addr = nil)
14
+ @http.use_ssl = true
15
+
16
+ @config.http.set(@http) unless @config.http.nil?
17
+ end
18
+
19
+ def self.host
20
+ @host ||= :api_host
21
+ end
22
+
23
+ def self.host=(host)
24
+ raise ArgumentError unless host == :rest_host
25
+
26
+ @host = host
27
+ end
28
+
29
+ def self.authentication
30
+ @authentication ||= KeySecretParams
31
+ end
32
+
33
+ def self.authentication=(authentication)
34
+ @authentication = authentication
35
+ end
36
+
37
+ def self.request_body
38
+ @request_body ||= FormData
39
+ end
40
+
41
+ def self.request_body=(request_body)
42
+ @request_body = request_body
43
+ end
44
+
45
+ def self.request_headers
46
+ @request_headers ||= {}
47
+ end
48
+
49
+ protected
50
+
51
+ Get = Net::HTTP::Get
52
+ Put = Net::HTTP::Put
53
+ Post = Net::HTTP::Post
54
+ Delete = Net::HTTP::Delete
55
+
56
+ def request(path, params: nil, type: Get, response_class: Response, &block)
57
+ uri = URI('https://' + @host + path)
58
+
59
+ params ||= {}
60
+
61
+ authentication = self.class.authentication.new(@config)
62
+ authentication.update(params)
63
+
64
+ unless type::REQUEST_HAS_BODY || params.empty?
65
+ uri.query = Params.encode(params)
66
+ end
67
+
68
+ authentication.update(uri)
69
+
70
+ message = type.new(uri)
71
+
72
+ message['User-Agent'] = UserAgent.string(@config.app_name, @config.app_version)
73
+
74
+ self.class.request_headers.each do |key, value|
75
+ message[key] = value
76
+ end
77
+
78
+ authentication.update(message)
79
+
80
+ self.class.request_body.update(message, params) if type::REQUEST_HAS_BODY
81
+
82
+ logger.log_request_info(message)
83
+
84
+ response = @http.request(message, &block)
85
+
86
+ logger.log_response_info(response, @host)
87
+
88
+ return if block
89
+
90
+ logger.debug(response.body) if response.body
91
+
92
+ parse(response, response_class)
93
+ end
94
+
95
+ def parse(response, response_class)
96
+ case response
97
+ when Net::HTTPNoContent
98
+ response_class.new(nil, response)
99
+ when Net::HTTPSuccess
100
+ if response['Content-Type'].split(';').first == 'application/json'
101
+ entity = ::JSON.parse(response.body, object_class: Vonage::Entity)
102
+
103
+ response_class.new(entity, response)
104
+ else
105
+ response_class.new(nil, response)
106
+ end
107
+ else
108
+ raise Errors.parse(response)
109
+ end
110
+ end
111
+
112
+ def logger
113
+ @config.logger
114
+ end
115
+ end
116
+
117
+ private_constant :Namespace
118
+ end
@@ -0,0 +1,140 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ module Vonage
5
+ class NumberInsight < Namespace
6
+ # Provides basic number insight information about a number.
7
+ #
8
+ # @example
9
+ # response = client.number_insight.basic(number: '447700900000')
10
+ #
11
+ # @option params [required, String] :number
12
+ # A single phone number that you need insight about in national or international format.
13
+ #
14
+ # @option params [String] :country
15
+ # If a number does not have a country code or is uncertain, set the two-character country code.
16
+ # This code must be in ISO 3166-1 alpha-2 format and in upper case. For example, GB or US.
17
+ # If you set country and number is already in E.164 format, country must match the country code in number.
18
+ #
19
+ # @param [Hash] params
20
+ #
21
+ # @return [Response]
22
+ #
23
+ # @see https://developer.nexmo.com/api/number-insight#getNumberInsightBasic
24
+ #
25
+ def basic(params)
26
+ response = request('/ni/basic/json', params: params)
27
+
28
+ raise Error, response[:status_message] unless response.status.zero?
29
+
30
+ response
31
+ end
32
+
33
+ # Provides standard number insight information about a number.
34
+ #
35
+ # @example
36
+ # response = client.number_insight.standard(number: '447700900000')
37
+ #
38
+ # @option params [required, String] :number
39
+ # A single phone number that you need insight about in national or international format.
40
+ #
41
+ # @option params [String] :country
42
+ # If a number does not have a country code or is uncertain, set the two-character country code.
43
+ # This code must be in ISO 3166-1 alpha-2 format and in upper case. For example, GB or US.
44
+ # If you set country and number is already in E.164 format, country must match the country code in number.
45
+ #
46
+ # @option params [Boolean] :cnam
47
+ # Indicates if the name of the person who owns the phone number should be looked up and returned in the response.
48
+ # Set to true to receive phone number owner name in the response.
49
+ # This features is available for US numbers only and incurs an additional charge.
50
+ #
51
+ # @param [Hash] params
52
+ #
53
+ # @return [Response]
54
+ #
55
+ # @see https://developer.nexmo.com/api/number-insight#getNumberInsightStandard
56
+ #
57
+ def standard(params)
58
+ response = request('/ni/standard/json', params: params)
59
+
60
+ raise Error, response[:status_message] unless response.status.zero?
61
+
62
+ response
63
+ end
64
+
65
+ # Provides advanced number insight information about a number synchronously.
66
+ #
67
+ # @example
68
+ # response = client.number_insight.advanced(number: '447700900000')
69
+ #
70
+ # @option params [required, String] :number
71
+ # A single phone number that you need insight about in national or international format.
72
+ #
73
+ # @option params [String] :country
74
+ # If a number does not have a country code or is uncertain, set the two-character country code.
75
+ # This code must be in ISO 3166-1 alpha-2 format and in upper case. For example, GB or US.
76
+ # If you set country and number is already in E.164 format, country must match the country code in number.
77
+ #
78
+ # @option params [Boolean] :cnam
79
+ # Indicates if the name of the person who owns the phone number should be looked up and returned in the response.
80
+ # Set to true to receive phone number owner name in the response.
81
+ # This features is available for US numbers only and incurs an additional charge.
82
+ #
83
+ # @option params [String] :ip
84
+ # The IP address of the user.
85
+ # If supplied, we will compare this to the country the user's phone is located in and return an error if it does not match.
86
+ #
87
+ # @param [Hash] params
88
+ #
89
+ # @return [Response]
90
+ #
91
+ # @see https://developer.nexmo.com/api/number-insight#getNumberInsightAdvanced
92
+ #
93
+ def advanced(params)
94
+ response = request('/ni/advanced/json', params: params)
95
+
96
+ raise Error, response[:status_message] unless response.status.zero?
97
+
98
+ response
99
+ end
100
+
101
+ # Provides advanced number insight number information *asynchronously* using the URL specified in the callback parameter.
102
+ #
103
+ # @example
104
+ # response = client.number_insight.advanced_async(number: '447700900000', callback: webhook_url)
105
+ #
106
+ # @option params [required, String] :callback
107
+ # The callback URL.
108
+ #
109
+ # @option params [required, String] :number
110
+ # A single phone number that you need insight about in national or international format.
111
+ #
112
+ # @option params [String] :country
113
+ # If a number does not have a country code or is uncertain, set the two-character country code.
114
+ # This code must be in ISO 3166-1 alpha-2 format and in upper case. For example, GB or US.
115
+ # If you set country and number is already in E.164 format, country must match the country code in number.
116
+ #
117
+ # @option params [Boolean] :cnam
118
+ # Indicates if the name of the person who owns the phone number should be looked up and returned in the response.
119
+ # Set to true to receive phone number owner name in the response.
120
+ # This features is available for US numbers only and incurs an additional charge.
121
+ #
122
+ # @option params [String] :ip
123
+ # The IP address of the user.
124
+ # If supplied, we will compare this to the country the user's phone is located in and return an error if it does not match.
125
+ #
126
+ # @param [Hash] params
127
+ #
128
+ # @return [Response]
129
+ #
130
+ # @see https://developer.nexmo.com/api/number-insight#getNumberInsightAsync
131
+ #
132
+ def advanced_async(params)
133
+ response = request('/ni/advanced/async/json', params: params)
134
+
135
+ raise Error, response[:status_message] unless response.status.zero?
136
+
137
+ response
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,196 @@
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ module Vonage
5
+ class Numbers < Namespace
6
+ include Keys
7
+
8
+ self.host = :rest_host
9
+
10
+ # Retrieve all the inbound numbers associated with your Vonage account.
11
+ #
12
+ # @example
13
+ # response = client.numbers.list
14
+ # response.each do |item|
15
+ # puts "#{item.msisdn} #{item.country} #{item.type}"
16
+ # end
17
+ #
18
+ # @option params [String] :application_id
19
+ # The application that you want to return the numbers for.
20
+ #
21
+ # @option params [Boolean] :has_application
22
+ # Set this optional field to `true` to restrict your results to numbers associated with an application (any application).
23
+ # Set to `false` to find all numbers not associated with any application.
24
+ # Omit the field to avoid filtering on whether or not the number is assigned to an application.
25
+ #
26
+ # @option params [String] :country
27
+ # The two character country code to filter on (in ISO 3166-1 alpha-2 format).
28
+ #
29
+ # @option params [String] :pattern
30
+ # The number pattern you want to search for. Use in conjunction with **:search_pattern**.
31
+ #
32
+ # @option params [Integer] :search_pattern
33
+ # The strategy you want to use for matching:
34
+ # - `0` - Search for numbers that start with **:pattern**
35
+ # - `1` - Search for numbers that contain **:pattern**
36
+ # - `2` - Search for numbers that end with **:pattern**
37
+ #
38
+ # @option params [Integer] :size
39
+ # Page size.
40
+ #
41
+ # @option params [Integer] :index
42
+ # Page index.
43
+ #
44
+ # @param [Hash] params
45
+ #
46
+ # @return [ListResponse]
47
+ #
48
+ # @see https://developer.nexmo.com/api/developer/numbers#getOwnedNumbers
49
+ #
50
+ def list(params = nil)
51
+ request('/account/numbers', params: params, response_class: ListResponse)
52
+ end
53
+
54
+ # Retrieve inbound numbers that are available for the specified country.
55
+ #
56
+ # @example
57
+ # response = client.numbers.search(country: 'GB')
58
+ # response.each do |item|
59
+ # puts "#{item.msisdn} #{item.type} #{item.cost}"
60
+ # end
61
+ #
62
+ # @option params [required, String] :country
63
+ # The two character country code in ISO 3166-1 alpha-2 format.
64
+ #
65
+ # @option params [String] :type
66
+ # Set this parameter to filter the type of number, such as mobile or landline.
67
+ #
68
+ # @option params [String] :pattern
69
+ # The number pattern you want to search for.
70
+ # Use in conjunction with **:search_pattern**.
71
+ #
72
+ # @option params [Integer] :search_pattern
73
+ # The strategy you want to use for matching:
74
+ # - `0` - Search for numbers that start with **:pattern**
75
+ # - `1` - Search for numbers that contain **:pattern**
76
+ # - `2` - Search for numbers that end with **:pattern**
77
+ #
78
+ # @option params [String] :features
79
+ # Available features are `SMS` and `VOICE`.
80
+ # To look for numbers that support both, use a comma-separated value: `SMS,VOICE`.
81
+ #
82
+ # @option params [Integer] :size
83
+ # Page size.
84
+ #
85
+ # @option params [Integer] :index
86
+ # Page index.
87
+ #
88
+ # @param [Hash] params
89
+ #
90
+ # @return [ListResponse]
91
+ #
92
+ # @see https://developer.nexmo.com/api/developer/numbers#getAvailableNumbers
93
+ #
94
+ def search(params)
95
+ request('/number/search', params: params, response_class: ListResponse)
96
+ end
97
+
98
+ # Request to purchase a specific inbound number.
99
+ #
100
+ # @example
101
+ # response = client.numbers.buy(country: 'GB', msisdn: '447700900000')
102
+ #
103
+ # @option params [required, String] :country
104
+ # The two character country code in ISO 3166-1 alpha-2 format.
105
+ #
106
+ # @option params [required, String] :msisdn
107
+ # An available inbound virtual number.
108
+ #
109
+ # @option params [String] :target_api_key
110
+ # If you'd like to perform an action on a subaccount, provide the `api_key` of that account here.
111
+ # If you'd like to perform an action on your own account, you do not need to provide this field.
112
+ #
113
+ # @param [Hash] params
114
+ #
115
+ # @return [Response]
116
+ #
117
+ # @see https://developer.nexmo.com/api/developer/numbers#buyANumber
118
+ #
119
+ def buy(params)
120
+ request('/number/buy', params: params, type: Post, response_class: Response)
121
+ end
122
+
123
+ # Cancel your subscription for a specific inbound number.
124
+ #
125
+ # @example
126
+ # response = client.numbers.cancel(country: 'GB', msisdn: '447700900000')
127
+ #
128
+ # @option params [required, String] :country
129
+ # The two character country code in ISO 3166-1 alpha-2 format.
130
+ #
131
+ # @option params [required, String] :msisdn
132
+ # An available inbound virtual number.
133
+ #
134
+ # @option params [String] :target_api_key
135
+ # If you'd like to perform an action on a subaccount, provide the `api_key` of that account here.
136
+ # If you'd like to perform an action on your own account, you do not need to provide this field.
137
+ #
138
+ # @param [Hash] params
139
+ #
140
+ # @return [Response]
141
+ #
142
+ # @see https://developer.nexmo.com/api/developer/numbers#cancelANumber
143
+ #
144
+ def cancel(params)
145
+ request('/number/cancel', params: params, type: Post, response_class: Response)
146
+ end
147
+
148
+ # Change the behaviour of a number that you own.
149
+ #
150
+ # @example
151
+ # params = {
152
+ # country: 'GB',
153
+ # msisdn: '447700900000',
154
+ # voice_callback_type: 'app',
155
+ # voice_callback_value: application_id
156
+ # }
157
+ #
158
+ # response = client.numbers.update(params)
159
+ #
160
+ # @option params [required, String] :country
161
+ # The two character country code in ISO 3166-1 alpha-2 format.
162
+ #
163
+ # @option params [required, String] :msisdn
164
+ # An available inbound virtual number.
165
+ #
166
+ # @option params [String] :mo_http_url
167
+ # An URL-encoded URI to the webhook endpoint that handles inbound messages.
168
+ # Your webhook endpoint must be active before you make this request.
169
+ # Vonage makes a `GET` request to the endpoint and checks that it returns a `200 OK` response.
170
+ # Set this parameter's value to an empty string to remove the webhook.
171
+ #
172
+ # @option params [String] :mo_smpp_sys_type
173
+ # The associated system type for your SMPP client.
174
+ #
175
+ # @option params [String] :voice_callback_type
176
+ # Specify whether inbound voice calls on your number are handled by your Application configuration, or forwarded to a SIP or a telephone number.
177
+ # Must be used with the **:voice_callback_value** option.
178
+ #
179
+ # @option params [String] :voice_callback_value
180
+ # A SIP URI, telephone number or Application ID.
181
+ # Must be used with the **:voice_callback_type** option.
182
+ #
183
+ # @option params [String] :voice_status_callback
184
+ # A webhook URI for Vonage to send a request to when a call ends.
185
+ #
186
+ # @param [Hash] params
187
+ #
188
+ # @return [Response]
189
+ #
190
+ # @see https://developer.nexmo.com/api/developer/numbers#updateANumber
191
+ #
192
+ def update(params)
193
+ request('/number/update', params: camelcase(params), type: Post, response_class: Response)
194
+ end
195
+ end
196
+ end