telerivet 1.1.7 → 1.2.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 22a61e5e23fb3d4e4126c4f8aa914acc10ff7291
4
- data.tar.gz: 8d3cdcb11da481a2ba69f6fd7588919540b708f8
3
+ metadata.gz: 956df92c7601623b5f4cca141712a7f4efa82074
4
+ data.tar.gz: b6fb090b8d492b7ea8a68ee394793dbfc5f6c890
5
5
  SHA512:
6
- metadata.gz: d4bada308cb913d0bc9890515d4c48d6cb0f6ca55f28feb5e2cf2e12d9aaf9634f01edcdc78feb78f417f3eb06e977fb51c43d5aaeed051f12972c3385a69d0a
7
- data.tar.gz: 3aeecae3a783a50e6498ef7dd8a223cc943d583fd711ec15c4f6fd9fe551669355d117a812c326ac59eddd6836e59e309a5d34d97f2511be87635bfb10e317a9
6
+ metadata.gz: dead19b8d3452ea46c0a3bc819899e59fc6a726df88cf023277031e58ff72f34c3a5b46a17c415afb699a07376a2f41f50833c9e3edc6a19f86464de9fc2c952
7
+ data.tar.gz: 3132ac8c08c783f6ba7d48cd06416967818bb416292341b472902e45f3bc19280b23d8e39639b7314d805c6dd41436ad89fd5794da242f8d000e4c9b04aa180c
@@ -5,11 +5,11 @@ require_relative 'telerivet/apicursor'
5
5
 
6
6
  module Telerivet
7
7
 
8
- class API
8
+ class API
9
9
  attr_reader :num_requests
10
10
 
11
- @@client_version = '1.1.7'
12
-
11
+ @@client_version = '1.2.1'
12
+
13
13
  #
14
14
  # Initializes a client handle to the Telerivet REST API.
15
15
  #
@@ -30,20 +30,20 @@ class API
30
30
  end
31
31
 
32
32
  public
33
-
33
+
34
34
  def do_request(method, path, params = nil)
35
-
35
+
36
36
  has_post_data = (method == 'POST' || method == 'PUT')
37
37
 
38
38
  url = @api_url + path
39
-
39
+
40
40
  if !has_post_data and params != nil && params.length > 0
41
41
  url += '?' + URI.encode_www_form(get_url_params(params))
42
42
  end
43
-
43
+
44
44
  uri = URI(url)
45
-
46
- if @session == nil
45
+
46
+ if @session == nil
47
47
  @session = Net::HTTP.start(uri.host, uri.port,
48
48
  :use_ssl => @api_url.start_with?("https://"),
49
49
  :ca_file => File.dirname(__FILE__) + '/cacert.pem',
@@ -51,39 +51,43 @@ class API
51
51
  :open_timeout => 20,
52
52
  )
53
53
  end
54
-
55
- cls = get_request_class(method)
54
+
55
+ cls = get_request_class(method)
56
56
  request = cls.new(uri.request_uri)
57
57
 
58
58
  request['User-Agent'] = "Telerivet Ruby Client/#{@@client_version} Ruby/#{RUBY_VERSION} OS/#{RUBY_PLATFORM}"
59
59
  request.basic_auth(@api_key, "")
60
60
 
61
61
  if has_post_data
62
- request.set_content_type("application/json")
62
+ request.set_content_type("application/json")
63
63
  if params != nil
64
- request.body = JSON.dump(params)
64
+ request.body = JSON.dump(params)
65
65
  end
66
66
  end
67
-
67
+
68
68
  @num_requests += 1
69
69
 
70
- response = @session.request(request)
71
-
72
- res = JSON.parse(response.body)
73
-
70
+ response = @session.request(request)
71
+
72
+ begin
73
+ res = JSON.parse(response.body)
74
+ rescue
75
+ raise IOError, "Unexpected response from Telerivet API (HTTP #{response.code}): #{response.body}"
76
+ end
77
+
74
78
  if res.has_key?("error")
75
79
  error = res['error']
76
80
  error_code = error['code']
77
-
81
+
78
82
  if error_code == 'invalid_param'
79
83
  raise InvalidParameterException, error['message'] #, error['code'], error['param'])
80
84
  elsif error_code == 'not_found'
81
85
  raise NotFoundException, error['message'] #, error['code']);
82
86
  else
83
- raise APIException, error['message'] #, error['code'])
87
+ raise APIException, error['message'] #, error['code'])
84
88
  end
85
89
  else
86
- return res
90
+ return res
87
91
  end
88
92
  end
89
93
 
@@ -160,35 +164,34 @@ class API
160
164
  ""
161
165
  end
162
166
 
163
-
164
167
  def cursor(item_cls, path, options)
165
168
  APICursor.new(self, item_cls, path, options)
166
169
  end
167
-
170
+
168
171
  private
169
-
172
+
170
173
  def encode_params_rec(param_name, value, res)
171
174
  return if value == nil
172
-
175
+
173
176
  if value.kind_of?(Array)
174
- value.each_index { |i| encode_params_rec("#{param_name}[#{i}]", value[i], res) }
177
+ value.each_index { |i| encode_params_rec("#{param_name}[#{i}]", value[i], res) }
175
178
  elsif value.kind_of?(Hash)
176
- value.each { |k,v| encode_params_rec("#{param_name}[#{k}]", v, res) }
179
+ value.each { |k,v| encode_params_rec("#{param_name}[#{k}]", v, res) }
177
180
  elsif !!value == value
178
181
  res[param_name] = value ? 1 : 0
179
182
  else
180
183
  res[param_name] = value
181
184
  end
182
185
  end
183
-
186
+
184
187
  def get_url_params(params)
185
188
  res = {}
186
189
  if params != nil
187
190
  params.each { |key,value| encode_params_rec(key, value, res) }
188
191
  end
189
- return res
192
+ return res
190
193
  end
191
-
194
+
192
195
  def get_request_class(method)
193
196
  case method
194
197
  when 'GET' then return Net::HTTP::Get
@@ -198,7 +201,7 @@ class API
198
201
  else return nil
199
202
  end
200
203
  end
201
-
204
+
202
205
  end
203
206
 
204
207
  class APIException < Exception
@@ -0,0 +1,275 @@
1
+
2
+ module Telerivet
3
+
4
+ #
5
+ # Fields:
6
+ #
7
+ # - id (string, max 34 characters)
8
+ # * ID of the broadcast
9
+ # * Read-only
10
+ #
11
+ # - recipients (array of objects)
12
+ # * List of recipients. Each recipient is an object with a string `type` property, which
13
+ # may be `"phone_number"`, `"group"`, or `"filter"`.
14
+ #
15
+ # If the type is `"phone_number"`, the `phone_number` property will
16
+ # be set to the recipient's phone number.
17
+ #
18
+ # If the type is `"group"`, the `group_id` property will be set to
19
+ # the ID of the group, and the `group_name` property will be set to the name of the
20
+ # group.
21
+ #
22
+ # If the type is `"filter"`, the `filter_type` property (string) and
23
+ # `filter_params` property (object) describe the filter used to send the broadcast. (API
24
+ # clients should not rely on a particular value or format of the `filter_type` or
25
+ # `filter_params` properties, as they may change without notice.)
26
+ # * Read-only
27
+ #
28
+ # - recipients_str
29
+ # * A string with a human readable description of the first few recipients (possibly
30
+ # truncated)
31
+ # * Read-only
32
+ #
33
+ # - time_created (UNIX timestamp)
34
+ # * Time the broadcast was sent in Telerivet
35
+ # * Read-only
36
+ #
37
+ # - last_message_time (UNIX timestamp)
38
+ # * Time the most recent message was queued to send in this broadcast
39
+ # * Read-only
40
+ #
41
+ # - last_send_time (UNIX timestamp)
42
+ # * Time the most recent message was sent (or failed to send) in this broadcast, or null
43
+ # if no messages have been sent yet
44
+ # * Read-only
45
+ #
46
+ # - status_counts (Hash)
47
+ # * An object whose keys are the possible status codes (`"queued"`, `"sent"`,
48
+ # `"failed"`, `"failed_queued"`, `"delivered"`, `"not_delivered"`, and `"cancelled"`),
49
+ # and whose values are the number of messages in the broadcast currently in that status.
50
+ # * Read-only
51
+ #
52
+ # - message_count (int)
53
+ # * The total number of messages created for this broadcast. For large broadcasts, the
54
+ # messages may not be created immediately after the broadcast is sent. The
55
+ # `message_count` includes messages in any status, including messages that are still
56
+ # queued.
57
+ # * Read-only
58
+ #
59
+ # - estimated_count (int)
60
+ # * The estimated number of messages in this broadcast when it is complete. The
61
+ # `estimated_count` is calculated at the time the broadcast is sent. When the broadcast
62
+ # is completed, the `estimated_count` may differ from `message_count` if there are any
63
+ # blocked contacts among the recipients (blocked contacts are included in
64
+ # `estimated_count` but not in `message_count`), if any contacts don't have phone
65
+ # numbers, or if the group membership changed while the broadcast was being sent.
66
+ # * Read-only
67
+ #
68
+ # - message_type
69
+ # * Type of message sent from this broadcast
70
+ # * Allowed values: sms, mms, ussd, call
71
+ # * Read-only
72
+ #
73
+ # - content (string)
74
+ # * The text content of the message (null for USSD messages and calls)
75
+ # * Read-only
76
+ #
77
+ # - audio_url
78
+ # * For voice calls, the URL of an MP3 file to play when the contact answers the call
79
+ # * Read-only
80
+ #
81
+ # - tts_lang
82
+ # * For voice calls, the language of the text-to-speech voice
83
+ # * Allowed values: en-US, en-GB, en-GB-WLS, en-AU, en-IN, da-DK, nl-NL, fr-FR, fr-CA,
84
+ # de-DE, is-IS, it-IT, pl-PL, pt-BR, pt-PT, ru-RU, es-ES, es-US, sv-SE
85
+ # * Read-only
86
+ #
87
+ # - tts_voice
88
+ # * For voice calls, the text-to-speech voice
89
+ # * Allowed values: female, male
90
+ # * Read-only
91
+ #
92
+ # - is_template (bool)
93
+ # * Set to true if Telerivet will render variables like [[contact.name]] in the message
94
+ # content, false otherwise
95
+ # * Read-only
96
+ #
97
+ # - status
98
+ # * The current status of the broadcast.
99
+ # * Allowed values: queuing, sending, complete, cancelled
100
+ # * Read-only
101
+ #
102
+ # - source
103
+ # * How the message originated within Telerivet
104
+ # * Allowed values: phone, provider, web, api, service, webhook, scheduled
105
+ # * Read-only
106
+ #
107
+ # - simulated (bool)
108
+ # * Whether this message was simulated within Telerivet for testing (and not actually
109
+ # sent to or received by a real phone)
110
+ # * Read-only
111
+ #
112
+ # - label_ids (array)
113
+ # * List of IDs of labels applied to all messages in the broadcast
114
+ # * Read-only
115
+ #
116
+ # - vars (Hash)
117
+ # * Custom variables stored for this message
118
+ # * Read-only
119
+ #
120
+ # - price (number)
121
+ # * The total price of all messages in this broadcast, if known.
122
+ # * Read-only
123
+ #
124
+ # - price_currency
125
+ # * The currency of the message price, if applicable.
126
+ # * Read-only
127
+ #
128
+ # - reply_count (int)
129
+ # * The number of replies received in response to a message sent in this broadcast.
130
+ # (Replies are not included in `message_count` ,`status_counts`, or `price`.)
131
+ # * Read-only
132
+ #
133
+ # - last_reply_time (UNIX timestamp)
134
+ # * Time the most recent reply was received in response to a message sent in this
135
+ # broadcast, or null if no replies have been sent yet
136
+ # * Read-only
137
+ #
138
+ # - route_id (string, max 34 characters)
139
+ # * ID of the phone or route used to send the broadcast (if applicable)
140
+ # * Read-only
141
+ #
142
+ # - user_id (string, max 34 characters)
143
+ # * ID of the Telerivet user who sent the broadcast (if applicable)
144
+ # * Read-only
145
+ #
146
+ # - project_id
147
+ # * ID of the project this broadcast belongs to
148
+ # * Read-only
149
+ #
150
+ class Broadcast < Entity
151
+ #
152
+ # Cancels sending a broadcast that has not yet been completely sent. No additional messages
153
+ # will be queued, and any existing queued messages will be cancelled when they would otherwise
154
+ # have been sent (except for messages already queued on the Telerivet Android app, which will
155
+ # not be automatically cancelled).
156
+ #
157
+ # Returns:
158
+ # Telerivet::Broadcast
159
+ #
160
+ def cancel()
161
+ require_relative 'broadcast'
162
+ Broadcast.new(@api, @api.do_request("POST", get_base_api_path() + "/cancel"))
163
+ end
164
+
165
+ def id
166
+ get('id')
167
+ end
168
+
169
+ def recipients
170
+ get('recipients')
171
+ end
172
+
173
+ def recipients_str
174
+ get('recipients_str')
175
+ end
176
+
177
+ def time_created
178
+ get('time_created')
179
+ end
180
+
181
+ def last_message_time
182
+ get('last_message_time')
183
+ end
184
+
185
+ def last_send_time
186
+ get('last_send_time')
187
+ end
188
+
189
+ def status_counts
190
+ get('status_counts')
191
+ end
192
+
193
+ def message_count
194
+ get('message_count')
195
+ end
196
+
197
+ def estimated_count
198
+ get('estimated_count')
199
+ end
200
+
201
+ def message_type
202
+ get('message_type')
203
+ end
204
+
205
+ def content
206
+ get('content')
207
+ end
208
+
209
+ def audio_url
210
+ get('audio_url')
211
+ end
212
+
213
+ def tts_lang
214
+ get('tts_lang')
215
+ end
216
+
217
+ def tts_voice
218
+ get('tts_voice')
219
+ end
220
+
221
+ def is_template
222
+ get('is_template')
223
+ end
224
+
225
+ def status
226
+ get('status')
227
+ end
228
+
229
+ def source
230
+ get('source')
231
+ end
232
+
233
+ def simulated
234
+ get('simulated')
235
+ end
236
+
237
+ def label_ids
238
+ get('label_ids')
239
+ end
240
+
241
+ def price
242
+ get('price')
243
+ end
244
+
245
+ def price_currency
246
+ get('price_currency')
247
+ end
248
+
249
+ def reply_count
250
+ get('reply_count')
251
+ end
252
+
253
+ def last_reply_time
254
+ get('last_reply_time')
255
+ end
256
+
257
+ def route_id
258
+ get('route_id')
259
+ end
260
+
261
+ def user_id
262
+ get('user_id')
263
+ end
264
+
265
+ def project_id
266
+ get('project_id')
267
+ end
268
+
269
+ def get_base_api_path()
270
+ "/projects/#{get('project_id')}/broadcasts/#{get('id')}"
271
+ end
272
+
273
+ end
274
+
275
+ end
@@ -145,6 +145,9 @@ class Contact < Entity
145
145
  # - time_created[max] (UNIX timestamp)
146
146
  # * Filter messages created before a particular time
147
147
  #
148
+ # - external_id
149
+ # * Filter messages by ID from an external provider
150
+ #
148
151
  # - contact_id
149
152
  # * ID of the contact who sent/received the message
150
153
  #
@@ -1,6 +1,6 @@
1
-
2
- module Telerivet
3
-
1
+
2
+ module Telerivet
3
+
4
4
  #
5
5
  # Represents the current state of a particular contact for a particular Telerivet service.
6
6
  #
@@ -55,7 +55,7 @@ module Telerivet
55
55
  # * ID of the project this contact/service state belongs to
56
56
  # * Read-only
57
57
  #
58
- class ContactServiceState < Entity
58
+ class ContactServiceState < Entity
59
59
  #
60
60
  # Saves the state id and any custom variables for this contact. If the state id is null, this
61
61
  # is equivalent to calling reset().
@@ -103,6 +103,6 @@ class ContactServiceState < Entity
103
103
  "/projects/#{get('project_id')}/services/#{get('service_id')}/states/#{get('contact_id')}"
104
104
  end
105
105
 
106
- end
107
-
108
- end
106
+ end
107
+
108
+ end