Class: MessageBird::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/messagebird/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_key = nil, http_client = nil, conversation_client = nil, voice_client = nil) ⇒ Client

rubocop:disable Metrics/ParameterLists



52
53
54
55
56
57
58
59
# File 'lib/messagebird/client.rb', line 52

def initialize(access_key = nil, http_client = nil, conversation_client = nil, voice_client = nil) # rubocop:disable Metrics/ParameterLists
  super()
  @access_key = access_key || ENV['MESSAGEBIRD_ACCESS_KEY']
  @http_client = http_client || HttpClient.new(@access_key)
  @conversation_client = conversation_client || ConversationClient.new(@access_key)
  @number_client = http_client || NumberClient.new(@access_key)
  @voice_client = voice_client || VoiceClient.new(@access_key)
end

Instance Attribute Details

#access_keyObject (readonly)

Returns the value of attribute access_key.



50
51
52
# File 'lib/messagebird/client.rb', line 50

def access_key
  @access_key
end

#conversation_clientObject (readonly)

Returns the value of attribute conversation_client.



50
51
52
# File 'lib/messagebird/client.rb', line 50

def conversation_client
  @conversation_client
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



50
51
52
# File 'lib/messagebird/client.rb', line 50

def http_client
  @http_client
end

#voice_clientObject (readonly)

Returns the value of attribute voice_client.



50
51
52
# File 'lib/messagebird/client.rb', line 50

def voice_client
  @voice_client
end

Instance Method Details

#balanceObject

Retrieve your balance.



190
191
192
# File 'lib/messagebird/client.rb', line 190

def balance
  Balance.new(request(:get, 'balance'))
end

#call_create(source, destination, call_flow = {}, webhook = {}, params = {}) ⇒ Object



302
303
304
305
306
307
# File 'lib/messagebird/client.rb', line 302

def call_create(source, destination, call_flow = {}, webhook = {}, params = {})
  params = params.merge(callFlow: call_flow.to_json) unless call_flow.empty?
  params = params.merge(webhook: webhook.to_json) unless webhook.empty?

  Voice::Call.new(voice_request(:post, 'calls', params.merge(source: source, destination: destination)))
end

#call_delete(id) ⇒ Object



317
318
319
# File 'lib/messagebird/client.rb', line 317

def call_delete(id)
  voice_request(:delete, "calls/#{id}")
end

#call_flow_create(steps, default, record, params = {}) ⇒ Object



463
464
465
466
467
468
469
470
471
# File 'lib/messagebird/client.rb', line 463

def call_flow_create(steps, default, record, params = {})
  params = params.merge(
    steps: steps,
    default: default,
    record: record
  )

  CallFlow.new(voice_request(:post, 'call-flows', params))
end

#call_flow_delete(id) ⇒ Object



481
482
483
# File 'lib/messagebird/client.rb', line 481

def call_flow_delete(id)
  voice_request(:delete, "call-flows/#{id}")
end

#call_flow_list(per_page = CallFlowList::PER_PAGE, page = CallFlowList::CURRENT_PAGE) ⇒ Object



477
478
479
# File 'lib/messagebird/client.rb', line 477

def call_flow_list(per_page = CallFlowList::PER_PAGE, page = CallFlowList::CURRENT_PAGE)
  CallFlowList.new(CallFlow, voice_request(:get, "call-flows?perPage=#{per_page}&page=#{page}"))
end

#call_flow_view(id) ⇒ Object



473
474
475
# File 'lib/messagebird/client.rb', line 473

def call_flow_view(id)
  CallFlow.new(voice_request(:get, "call-flows/#{id}"))
end

#call_leg_list(call_id, per_page = Voice::List::PER_PAGE, current_page = Voice::List::CURRENT_PAGE) ⇒ Object



321
322
323
# File 'lib/messagebird/client.rb', line 321

def call_leg_list(call_id, per_page = Voice::List::PER_PAGE, current_page = Voice::List::CURRENT_PAGE)
  Voice::List.new(Voice::CallLeg, voice_request(:get, "calls/#{call_id}/legs?perPage=#{per_page}&currentPage=#{current_page}"))
end

#call_leg_recording_delete(call_id, leg_id, recording_id) ⇒ Object



333
334
335
# File 'lib/messagebird/client.rb', line 333

def call_leg_recording_delete(call_id, leg_id, recording_id)
  voice_request(:delete, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}")
end

#call_leg_recording_download(recording_uri, &block) ⇒ Object



337
338
339
# File 'lib/messagebird/client.rb', line 337

def call_leg_recording_download(recording_uri, &block)
  @voice_client.request_block(:get, recording_uri, {}, &block)
end

#call_leg_recording_list(call_id, leg_id) ⇒ Object



329
330
331
# File 'lib/messagebird/client.rb', line 329

def call_leg_recording_list(call_id, leg_id)
  Voice::List.new(Voice::CallLegRecording, voice_request(:get, "calls/#{call_id}/legs/#{leg_id}/recordings"))
end

#call_leg_recording_view(call_id, leg_id, recording_id) ⇒ Object



325
326
327
# File 'lib/messagebird/client.rb', line 325

def call_leg_recording_view(call_id, leg_id, recording_id)
  Voice::CallLegRecording.new(voice_request(:get, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}"))
end

#call_list(per_page = Voice::List::PER_PAGE, page = Voice::List::CURRENT_PAGE) ⇒ Object



309
310
311
# File 'lib/messagebird/client.rb', line 309

def call_list(per_page = Voice::List::PER_PAGE, page = Voice::List::CURRENT_PAGE)
  Voice::List.new(Voice::Call, voice_request(:get, "calls?perPage=#{per_page}&currentPage=#{page}"))
end

#call_view(id) ⇒ Object



313
314
315
# File 'lib/messagebird/client.rb', line 313

def call_view(id)
  Voice::Call.new(voice_request(:get, "calls/#{id}"))
end

#contact(id) ⇒ Object



377
378
379
# File 'lib/messagebird/client.rb', line 377

def contact(id)
  Contact.new(request(:get, "contacts/#{id}"))
end

#contact_create(phone_number, params = {}) ⇒ Object



369
370
371
372
373
374
375
# File 'lib/messagebird/client.rb', line 369

def contact_create(phone_number, params = {})
  Contact.new(request(
                :post,
                'contacts',
                params.merge(msisdn: phone_number.to_s)
  ))
end

#contact_delete(id) ⇒ Object



381
382
383
# File 'lib/messagebird/client.rb', line 381

def contact_delete(id)
  request(:delete, "contacts/#{id}")
end

#contact_list(limit = 0, offset = 0) ⇒ Object



389
390
391
# File 'lib/messagebird/client.rb', line 389

def contact_list(limit = 0, offset = 0)
  List.new(Contact, request(:get, "contacts?limit=#{limit}&offset=#{offset}"))
end

#contact_update(id, params = {}) ⇒ Object



385
386
387
# File 'lib/messagebird/client.rb', line 385

def contact_update(id, params = {})
  request(:patch, "contacts/#{id}", params)
end

#conversation(id) ⇒ Object



134
135
136
# File 'lib/messagebird/client.rb', line 134

def conversation(id)
  Conversation.new(conversation_request(:get, "conversations/#{id}"))
end

#conversation_list(limit = -1,, offset = -1)) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/messagebird/client.rb', line 121

def conversation_list(limit = -1, offset = -1)
  query = '?'
  if limit != -1
    query += "limit=#{limit}&"
  end

  if offset != -1
    query += "offset=#{offset}"
  end

  List.new(Conversation, conversation_request(:get, "conversations#{query}"))
end

#conversation_message(id) ⇒ Object



159
160
161
# File 'lib/messagebird/client.rb', line 159

def conversation_message(id)
  ConversationMessage.new(conversation_request(:get, "messages/#{id}"))
end

#conversation_messages_list(id, limit = -1,, offset = -1)) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/messagebird/client.rb', line 146

def conversation_messages_list(id, limit = -1, offset = -1)
  query = '?'
  if limit != -1
    query += "limit=#{limit}&"
  end

  if offset != -1
    query += "offset=#{offset}"
  end

  List.new(ConversationMessage, conversation_request(:get, "conversations/#{id}/messages#{query}"))
end

#conversation_reply(id, params = {}) ⇒ Object



142
143
144
# File 'lib/messagebird/client.rb', line 142

def conversation_reply(id, params = {})
  ConversationMessage.new(conversation_request(:post, "conversations/#{id}/messages", params))
end

#conversation_request(method, path, params = {}) ⇒ Object



61
62
63
64
65
66
# File 'lib/messagebird/client.rb', line 61

def conversation_request(method, path, params = {})
  response_body = @conversation_client.request(method, path, params)
  return if response_body.nil? || response_body.empty?

  parse_body(response_body)
end

#conversation_update(id, status) ⇒ Object



138
139
140
# File 'lib/messagebird/client.rb', line 138

def conversation_update(id, status)
  Conversation.new(conversation_request(:patch, "conversations/#{id}", status: status))
end

#conversation_webhook(id) ⇒ Object



181
182
183
# File 'lib/messagebird/client.rb', line 181

def conversation_webhook(id)
  ConversationWebhook.new(conversation_request(:get, "webhooks/#{id}"))
end

#conversation_webhook_create(channel_id, url, events = []) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/messagebird/client.rb', line 163

def conversation_webhook_create(channel_id, url, events = [])
  ConversationWebhook.new(conversation_request(
                            :post,
                            'webhooks',
                            channel_id: channel_id,
                            url: url,
                            events: events
  ))
end

#conversation_webhook_delete(id) ⇒ Object



185
186
187
# File 'lib/messagebird/client.rb', line 185

def conversation_webhook_delete(id)
  conversation_request(:delete, "webhooks/#{id}")
end

#conversation_webhook_update(id, params = {}) ⇒ Object



177
178
179
# File 'lib/messagebird/client.rb', line 177

def conversation_webhook_update(id, params = {})
  ConversationWebhook.new(conversation_request(:patch, "webhooks/#{id}", params))
end

#conversation_webhooks_list(limit = 0, offset = 0) ⇒ Object



173
174
175
# File 'lib/messagebird/client.rb', line 173

def conversation_webhooks_list(limit = 0, offset = 0)
  List.new(ConversationWebhook, conversation_request(:get, "webhooks?limit=#{limit}&offset=#{offset}"))
end

#group(id) ⇒ Object



393
394
395
# File 'lib/messagebird/client.rb', line 393

def group(id)
  Group.new(request(:get, "groups/#{id}"))
end

#group_add_contacts(group_id, contact_ids) ⇒ Object



413
414
415
416
417
418
419
420
# File 'lib/messagebird/client.rb', line 413

def group_add_contacts(group_id, contact_ids)
  # We expect an array, but we can handle a string ID as well...
  contact_ids = [contact_ids] if contact_ids.is_a? String

  query = add_contacts_query(contact_ids)

  request(:get, "groups/#{group_id}?#{query}")
end

#group_create(name) ⇒ Object



397
398
399
# File 'lib/messagebird/client.rb', line 397

def group_create(name)
  Group.new(request(:post, 'groups', name: name))
end

#group_delete(id) ⇒ Object



401
402
403
# File 'lib/messagebird/client.rb', line 401

def group_delete(id)
  request(:delete, "groups/#{id}")
end

#group_delete_contact(group_id, contact_id) ⇒ Object



422
423
424
# File 'lib/messagebird/client.rb', line 422

def group_delete_contact(group_id, contact_id)
  request(:delete, "groups/#{group_id}/contacts/#{contact_id}")
end

#group_list(limit = 0, offset = 0) ⇒ Object



405
406
407
# File 'lib/messagebird/client.rb', line 405

def group_list(limit = 0, offset = 0)
  List.new(Group, request(:get, "groups?limit=#{limit}&offset=#{offset}"))
end

#group_update(id, name) ⇒ Object



409
410
411
# File 'lib/messagebird/client.rb', line 409

def group_update(id, name)
  request(:patch, "groups/#{id}", name: name)
end

#hlr(id) ⇒ Object

Retrieve the information of specific HLR.



195
196
197
# File 'lib/messagebird/client.rb', line 195

def hlr(id)
  HLR.new(request(:get, "hlr/#{id}"))
end

#hlr_create(msisdn, reference) ⇒ Object

Create a new HLR.



200
201
202
203
204
205
206
207
# File 'lib/messagebird/client.rb', line 200

def hlr_create(msisdn, reference)
  HLR.new(request(
            :post,
            'hlr',
            msisdn: msisdn,
            reference: reference
  ))
end

#lookup(phone_number, params = {}) ⇒ Object



357
358
359
# File 'lib/messagebird/client.rb', line 357

def lookup(phone_number, params = {})
  Lookup.new(request(:get, "lookup/#{phone_number}", params))
end

#lookup_hlr(phone_number, params = {}) ⇒ Object



365
366
367
# File 'lib/messagebird/client.rb', line 365

def lookup_hlr(phone_number, params = {})
  HLR.new(request(:get, "lookup/#{phone_number}/hlr", params))
end

#lookup_hlr_create(phone_number, params = {}) ⇒ Object



361
362
363
# File 'lib/messagebird/client.rb', line 361

def lookup_hlr_create(phone_number, params = {})
  HLR.new(request(:post, "lookup/#{phone_number}/hlr", params))
end

#message(id) ⇒ Object

Retrieve the information of specific message.



239
240
241
# File 'lib/messagebird/client.rb', line 239

def message(id)
  Message.new(request(:get, "messages/#{id}"))
end

#message_create(originator, recipients, body, params = {}) ⇒ Object

Create a new message.



252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/messagebird/client.rb', line 252

def message_create(originator, recipients, body, params = {})
  # Convert an array of recipients to a comma-separated string.
  recipients = recipients.join(',') if recipients.is_a?(Array)

  Message.new(request(
                :post,
                'messages',
                params.merge(originator: originator.to_s,
                             body: body.to_s,
                             recipients: recipients)
  ))
end

#message_list(filters = {}) ⇒ Object

Retrieve messages with optional paging and status filter.



244
245
246
247
248
249
# File 'lib/messagebird/client.rb', line 244

def message_list(filters = {})
  params = { limit: 10, offset: 0 }.merge(filters).compact
  query = "messages?#{URI.encode_www_form(params)}"

  List.new(Message, request(:get, query))
end

#number_cancel(number) ⇒ Object

Cancel a number



459
460
461
# File 'lib/messagebird/client.rb', line 459

def number_cancel(number)
  number_request(:delete, "phone-numbers/#{number}")
end

#number_fetch(number) ⇒ Object

Fetch specific purchased number’s details



448
449
450
# File 'lib/messagebird/client.rb', line 448

def number_fetch(number)
  Number.new(number_request(:get, "phone-numbers/#{number}"))
end

#number_fetch_all(params = {}) ⇒ Object

Fetch all purchaed numbers’ details



443
444
445
# File 'lib/messagebird/client.rb', line 443

def number_fetch_all(params = {})
  List.new(Number, number_request(:get, add_querystring('phone-numbers', params), params))
end

#number_purchase(number, country_code, billing_interval_months) ⇒ Object

Purchase an avaiable number



433
434
435
436
437
438
439
440
# File 'lib/messagebird/client.rb', line 433

def number_purchase(number, country_code, billing_interval_months)
  params = {
    number: number,
    countryCode: country_code,
    billingIntervalMonths: billing_interval_months
  }
  Number.new(number_request(:post, 'phone-numbers', params))
end

#number_request(method, path, params = {}) ⇒ Object



68
69
70
71
72
73
# File 'lib/messagebird/client.rb', line 68

def number_request(method, path, params = {})
  response_body = @number_client.request(method, path, params)
  return if response_body.nil? || response_body.empty?

  parse_body(response_body)
end

#number_search(country_code, params = {}) ⇒ Object

Numbers API Search for available numbers



428
429
430
# File 'lib/messagebird/client.rb', line 428

def number_search(country_code, params = {})
  List.new(Number, number_request(:get, add_querystring("available-phone-numbers/#{country_code}", params), params))
end

#number_update(number, tags) ⇒ Object

Update a number



453
454
455
456
# File 'lib/messagebird/client.rb', line 453

def number_update(number, tags)
  tags = [tags] if tags.is_a? String
  Number.new(number_request(:patch, "phone-numbers/#{number}", tags: tags))
end

#parse_body(body) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/messagebird/client.rb', line 89

def parse_body(body)
  json = JSON.parse(body)

  # If the request returned errors, create Error objects and raise.
  if json.key?('errors')
    raise(ErrorException, json['errors'].map { |e| Error.new(e) })
  end

  json
end

#request(method, path, params = {}) ⇒ Object



82
83
84
85
86
87
# File 'lib/messagebird/client.rb', line 82

def request(method, path, params = {})
  response_body = @http_client.request(method, path, params)
  return if response_body.nil? || response_body.empty?

  parse_body(response_body)
end

#send_conversation_message(from, to, params = {}) ⇒ Object

Conversations Send a conversation message



102
103
104
105
106
107
108
109
# File 'lib/messagebird/client.rb', line 102

def send_conversation_message(from, to, params = {})
  ConversationMessage.new(conversation_request(
                            :post,
                            'send',
                            params.merge(from: from,
                                         to: to)
  ))
end

#start_conversation(to, channel_id, params = {}) ⇒ Object

Start a conversation



112
113
114
115
116
117
118
119
# File 'lib/messagebird/client.rb', line 112

def start_conversation(to, channel_id, params = {})
  Conversation.new(conversation_request(
                     :post,
                     'conversations/start',
                     params.merge(to: to,
                                  channel_id: channel_id)
  ))
end

#verify(id) ⇒ Object

Retrieve the information of specific Verify.



210
211
212
# File 'lib/messagebird/client.rb', line 210

def verify(id)
  Verify.new(request(:get, "verify/#{id}"))
end

#verify_create(recipient, params = {}) ⇒ Object

Generate a new One-Time-Password message.



220
221
222
223
224
225
226
# File 'lib/messagebird/client.rb', line 220

def verify_create(recipient, params = {})
  Verify.new(request(
               :post,
               'verify',
               params.merge(recipient: recipient)
  ))
end

#verify_delete(id) ⇒ Object

Delete a Verify, response is empty



234
235
236
# File 'lib/messagebird/client.rb', line 234

def verify_delete(id)
  request(:delete, "verify/#{id}")
end

#verify_email_message(id) ⇒ Object

Retrieve the information of specific Verify email message



215
216
217
# File 'lib/messagebird/client.rb', line 215

def verify_email_message(id)
  VerifyEmailMessage.new(request(:get, "verify/messages/email/#{id}"))
end

#verify_token(id, token) ⇒ Object

Verify the One-Time-Password.



229
230
231
# File 'lib/messagebird/client.rb', line 229

def verify_token(id, token)
  Verify.new(request(:get, "verify/#{id}?token=#{token}"))
end

#voice_message(id) ⇒ Object

Retrieve the information of a specific voice message.



266
267
268
# File 'lib/messagebird/client.rb', line 266

def voice_message(id)
  VoiceMessage.new(request(:get, "voicemessages/#{id}"))
end

#voice_message_create(recipients, body, params = {}) ⇒ Object

Create a new voice message.



271
272
273
274
275
276
277
278
279
280
# File 'lib/messagebird/client.rb', line 271

def voice_message_create(recipients, body, params = {})
  # Convert an array of recipients to a comma-separated string.
  recipients = recipients.join(',') if recipients.is_a?(Array)

  VoiceMessage.new(request(
                     :post,
                     'voicemessages',
                     params.merge(recipients: recipients, body: body.to_s)
  ))
end

#voice_request(method, path, params = {}) ⇒ Object



75
76
77
78
79
80
# File 'lib/messagebird/client.rb', line 75

def voice_request(method, path, params = {})
  response_body = @voice_client.request(method, path, params)
  return if response_body.nil? || response_body.empty?

  parse_body(response_body)
end

#voice_transcription_create(call_id, leg_id, recording_id, params = {}) ⇒ Object



341
342
343
# File 'lib/messagebird/client.rb', line 341

def voice_transcription_create(call_id, leg_id, recording_id, params = {})
  Voice::Transcription.new(voice_request(:post, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}/transcriptions", params))
end

#voice_transcription_download(call_id, leg_id, recording_id, transcription_id, &block) ⇒ Object



349
350
351
# File 'lib/messagebird/client.rb', line 349

def voice_transcription_download(call_id, leg_id, recording_id, transcription_id, &block)
  @voice_client.request_block(:get, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}/transcriptions/#{transcription_id}.txt", {}, &block)
end

#voice_transcription_view(call_id, leg_id, recording_id, transcription_id) ⇒ Object



353
354
355
# File 'lib/messagebird/client.rb', line 353

def voice_transcription_view(call_id, leg_id, recording_id, transcription_id)
  Voice::Transcription.new(voice_request(:get, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}/transcriptions/#{transcription_id}"))
end

#voice_transcriptions_list(call_id, leg_id, recording_id) ⇒ Object



345
346
347
# File 'lib/messagebird/client.rb', line 345

def voice_transcriptions_list(call_id, leg_id, recording_id)
  Voice::List.new(Voice::Transcription, voice_request(:get, "calls/#{call_id}/legs/#{leg_id}/recordings/#{recording_id}/transcriptions"))
end

#voice_webhook(id) ⇒ Object



294
295
296
# File 'lib/messagebird/client.rb', line 294

def voice_webhook(id)
  Voice::Webhook.new(voice_request(:get, "webhooks/#{id}"))
end

#voice_webhook_create(url, params = {}) ⇒ Object



282
283
284
# File 'lib/messagebird/client.rb', line 282

def voice_webhook_create(url, params = {})
  Voice::Webhook.new(voice_request(:post, 'webhooks', params.merge(url: url)))
end

#voice_webhook_delete(id) ⇒ Object



298
299
300
# File 'lib/messagebird/client.rb', line 298

def voice_webhook_delete(id)
  voice_request(:delete, "webhooks/#{id}")
end

#voice_webhook_update(id, params = {}) ⇒ Object



290
291
292
# File 'lib/messagebird/client.rb', line 290

def voice_webhook_update(id, params = {})
  Voice::Webhook.new(voice_request(:put, "webhooks/#{id}", params))
end

#voice_webhooks_list(per_page = VoiceList::PER_PAGE, page = VoiceList::CURRENT_PAGE) ⇒ Object



286
287
288
# File 'lib/messagebird/client.rb', line 286

def voice_webhooks_list(per_page = VoiceList::PER_PAGE, page = VoiceList::CURRENT_PAGE)
  Voice::List.new(Voice::Webhook, voice_request(:get, "webhooks?perPage=#{per_page}&page=#{page}"))
end