Skip to content

Introduce objects/methods for dealing with voice call recordings #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions examples/call.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env ruby

# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib/')
require 'messagebird'

ACCESS_KEY = 'YOUR ACCESS KEY HERE'

unless defined?(ACCESS_KEY)
puts 'You need to set an ACCESS_KEY constant in this file'
exit 1
end

begin
# Create a MessageBird client with the specified ACCESS_KEY.
client = MessageBird::Client.new(ACCESS_KEY)

# Start a conversation
calls = client.call_list(10, 0)

calls.items.each do |call|
puts "Call ID: #{call.id}"
end
rescue MessageBird::ErrorException => e
puts
puts 'An error occured while listing the calls:'
puts

e.errors.each do |error|
puts " code : #{error.code}"
puts " description : #{error.description}"
puts " parameter : #{error.parameter}"
puts
end
end
58 changes: 58 additions & 0 deletions examples/call_leg_recordings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env ruby

# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib/')
require 'messagebird'

ACCESS_KEY = 'YOUR KEY HERE'
CALL_ID = 'YOUR CALL ID HERE'

unless defined?(ACCESS_KEY)
puts 'You need to set an ACCESS_KEY constant in this file'
exit 1
end

begin
# Create a MessageBird client with the specified ACCESS_KEY.
client = MessageBird::Client.new(ACCESS_KEY)

# Request the legs (overview of inbound/outbound portions of this call)
puts "Retrieving legs for call #{CALL_ID}"
legs = client.call_leg_list(CALL_ID)

legs.items.each do |leg|
puts " Retrieving recordings for leg #{leg.id}"
recordings = client.call_leg_recording_list(CALL_ID, leg.id)

recordings.items.each do |recording|
client.call_leg_recording_view(CALL_ID, leg.id, recording.id)

puts ' --------------------------------------------------'
puts " recording ID : #{recording.id}"
puts " recording URI : #{recording.uri}"
print ' downloading : '
client.call_leg_recording_download(recording.uri) do |response|
File.open("#{recording.id}.wav", 'w') do |io|
response.read_body do |chunk|
putc '.'
io.write(chunk)
end
end
end
puts ' DONE!'
puts
end
end
rescue MessageBird::ErrorException => e
puts
puts 'An error occured while listing the calls:'
puts

e.errors.each do |error|
puts " code : #{error.code}"
puts " description : #{error.description}"
puts " parameter : #{error.parameter}"
puts
end
end
49 changes: 49 additions & 0 deletions examples/call_legs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env ruby

# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib/')
require 'messagebird'

ACCESS_KEY = 'YOUR ACCESS KEY HERE'
CALL_ID = 'YOUR CALL ID HERE'

unless defined?(ACCESS_KEY)
puts 'You need to set an ACCESS_KEY constant in this file'
exit 1
end

begin
# Create a MessageBird client with the specified ACCESS_KEY.
client = MessageBird::Client.new(ACCESS_KEY)

# Start a conversation
legs = client.call_leg_list(CALL_ID, 10, 0)

legs.items.each do |leg_obj|
puts "leg ID: #{leg_obj.id}"
puts " call ID : #{leg_obj.callId}"
puts " source : #{leg_obj.source}"
puts " destination : #{leg_obj.destination}"
puts " status : #{leg_obj.status}"
puts " direction : #{leg_obj.direction}"
puts " cost : #{leg_obj.cost}"
puts " currency : #{leg_obj.currency}"
puts " duration : #{leg_obj.duration}"
puts " createdAt : #{leg_obj.createdAt}"
puts " updatedAt : #{leg_obj.updatedAt}"
puts " answeredAt : #{leg_obj.answeredAt}"
puts " endedAt : #{leg_obj.endedAt}"
end
rescue MessageBird::ErrorException => e
puts
puts 'An error occured while listing the calls:'
puts

e.errors.each do |error|
puts " code : #{error.code}"
puts " description : #{error.description}"
puts " parameter : #{error.parameter}"
puts
end
end
8 changes: 7 additions & 1 deletion lib/messagebird.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true

libdir = File.dirname(__FILE__)
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)

require 'messagebird/version'
require 'messagebird/balance'
require 'messagebird/client'
require 'messagebird/contact'
Expand All @@ -12,5 +16,7 @@
require 'messagebird/verify'
require 'messagebird/message'
require 'messagebird/voicemessage'
require 'messagebird/call'
require 'messagebird/voice/call'
require 'messagebird/voice/call_leg'
require 'messagebird/voice/call_leg_recording'
require 'messagebird/voice/webhook'
15 changes: 10 additions & 5 deletions lib/messagebird/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@

module MessageBird
class Base
def initialize(json)
json.each do |k, v|
m = k.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase # convert came case to snake case

send("#{m}=", v) if respond_to?(:"#{m}=")
# takes each element from the given hash and apply it to ourselves through an assignment method
def map_hash_elements_to_self(hash)
hash.each do |key, value|
method_name = key.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase # convert came case to snake case
method_name += '='
send(method_name, value) if respond_to?(method_name)
end
end

def initialize(json)
map_hash_elements_to_self(json)
end

def value_to_time(value)
value ? Time.parse(value) : nil
end
Expand Down
20 changes: 0 additions & 20 deletions lib/messagebird/call.rb

This file was deleted.

26 changes: 0 additions & 26 deletions lib/messagebird/call/list.rb

This file was deleted.

7 changes: 0 additions & 7 deletions lib/messagebird/call/webhook.rb

This file was deleted.

62 changes: 30 additions & 32 deletions lib/messagebird/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
require 'messagebird/voice/list'
require 'messagebird/voice/webhook'
require 'messagebird/voicemessage'
require 'messagebird/call'
require 'messagebird/call/list'
require 'messagebird/voice_client'
require 'messagebird/voice/call'
require 'messagebird/voice/call_leg'
require 'messagebird/voice/call_leg_recording'
require 'messagebird/voice/list'

module MessageBird
class ErrorException < StandardError
Expand Down Expand Up @@ -251,36 +254,19 @@ def voice_message_create(recipients, body, params = {})
end

def voice_webhook_create(url, params = {})
list = VoiceList.new(VoiceWebhook, voice_request(
:post,
'webhooks',
params.merge(url: url)
))

list.items[0]
Voice::Webhook.new(voice_request(:post, 'webhooks', params.merge(url: url)))
end

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

def voice_webhook_update(id, params = {})
list = VoiceList.new(VoiceWebhook, voice_request(
:put,
"webhooks/#{id}",
params
))

list.items[0]
Voice::Webhook.new(voice_request(:put, "webhooks/#{id}", params))
end

def voice_webhook(id)
list = VoiceList.new(VoiceWebhook, voice_request(
:get,
"webhooks/#{id}"
))

list.items[0]
Voice::Webhook.new(voice_request(:get, "webhooks/#{id}"))
end

def voice_webhook_delete(id)
Expand All @@ -291,23 +277,35 @@ 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?

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

def call_list(per_page = CallList::PER_PAGE, page = CallList::CURRENT_PAGE)
CallList.new(Call, request(:get, "calls?perPage=#{per_page}&page=#{page}"))
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

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

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

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

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

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

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

def lookup(phone_number, params = {})
Expand Down
19 changes: 15 additions & 4 deletions lib/messagebird/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ def endpoint
ENDPOINT
end

def request(method, path, params = {}, check_json = true)
uri = URI.join(endpoint, path)

# Set up the HTTP object.
def build_http_client(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

http.set_debug_output($stdout) unless ENV['DEBUG_MB_HTTP_CLIENT'].nil?

http
end

def request(method, path, params = {}, check_json = true)
uri = URI.join(endpoint, path)
http = build_http_client(uri)
request = build_request(method, uri, params)

# Execute the request and fetch the response.
Expand All @@ -41,6 +44,14 @@ def request(method, path, params = {}, check_json = true)
response.body
end

def request_block(method, path, params = {})
uri = URI.join(endpoint, path)
http = build_http_client(uri)
request = build_request(method, uri, params)

http.request(request, &Proc.new)
end

def prepare_request(request, params = {})
request.set_form_data(params)
request
Expand Down
Loading