Sha256: 93746376e90d62c9a83e9c94e631e13f876b9e7a72fdc82b8af8551ebe08a6c7

Contents?: true

Size: 1.86 KB

Versions: 5

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module Mock
  module Twilio
    module Webhooks
      class Voicemail < Base
        URL = "/api/v1/twilio_calls/create_voicemail"

        def self.trigger(call_sid)
          # Wait simulation from twilio
          sleep DELAY.sample

          request_url = Mock::Twilio.proto + "://" + Mock::Twilio.forwarded_host + URL

          data = voicemail_data(call_sid)

          signature = build_signature_for_request(request_url, data)

          response = webhook_client.request(Mock::Twilio.host,
                                            Mock::Twilio.port,
                                            'POST',
                                            URL,
                                            nil,
                                            data,
                                            headers.merge!({ 'X-Twilio-Signature': signature }),
                                            auth_twilio,
                                            nil)
          case response.status
          when 200..204
            response
          when 400..600
            raise Webhooks::RestError, Mock::Twilio::ErrorHandler.new(response).raise
          end
        end

        def self.voicemail_data(call_sid)
          prefix = "RE"
          recording_sid = prefix + SecureRandom.hex(16)
          {
            :RecordingSource=> "RecordVerb",
            :RecordingSid=> recording_sid,
            :RecordingUrl=> "https://cdn.pixabay.com/download/audio/2022/03/24/audio_4ff823c44c.mp3?filename=ding-101492.mp3",
            :RecordingStatus=> "completed",
            :RecordingChannels=> "1",
            :ErrorCode=> "0",
            :CallSid=> call_sid,
            :RecordingStartTime=> Time.current.rfc2822,
            :AccountSid=> twilio_client.account_sid,
            :RecordingDuration=> "4"
          }
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mock-twilio-1.4 lib/mock/twilio/webhooks/voicemail.rb
mock-twilio-1.3.2 lib/mock/twilio/webhooks/voicemail.rb
mock-twilio-1.3.1 lib/mock/twilio/webhooks/voicemail.rb
mock-twilio-1.3.0 lib/mock/twilio/webhooks/voicemail.rb
mock-twilio-1.2.0 lib/mock/twilio/webhooks/voicemail.rb