Completed
Push — master ( b1c03e...f40ce3 )
by John
01:04
created

Client.upload_media()   A

Complexity

Conditions 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 21
rs 9.3143
1
#!ruby
2
3
require 'date'
4
require 'faraday'
5
require 'faraday_middleware'
6
require 'multi_json'
7
require 'ringcentral_sdk'
8
require 'pp'
9
10
=begin
11
  
12
This demo uploads MP3 recording files with the file format "recording_.*\.mp3"
13
to VoiceBase for text transcription. This is the file format used by the
14
call-recording_download.rb script.
15
16
This file uses a Ruby port of the PHP demo in the VoiceBase API Developers
17
Guide version 1.1.4 PDF page 18.
18
19
=end
20
21
# Set your credentials in the test credentials file
22
23
class RingCentralSdkBootstrap
24
25
  attr_reader :credentials
26
27
  def load_credentials(credentials_filepath, usage_string=nil)
28
    unless credentials_filepath.to_s.length>0
29
      raise usage_string.to_s
30
    end
31
32
    unless File.exists?(credentials_filepath.to_s)
33
      raise "Error: credentials file does not exist for: #{credentials_filepath}"
34
    end
35
36
    @credentials = MultiJson.decode(IO.read(credentials_filepath), :symbolize_keys=>true)
37
  end
38
39
end
40
41
boot = RingCentralSdkBootstrap.new
42
boot.load_credentials(ARGV.shift, 'Usage: call-recording_transcribe_voicebase.rb path/to/credentials.json')
43
44
module VoiceBase
45
  class Client
46
    def initialize(api_key, password, transcript_type='machine-best')
47
      @api_key = api_key
48
      @password = password
49
      @transcript_type = transcript_type
50
      @conn = Faraday.new(:url => 'https://api.voicebase.com/services') do |faraday|
51
        faraday.request  :multipart               # multipart/form-data
52
        faraday.response :json
53
        faraday.response :logger                  # log requests to STDOUT
54
        faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
55
      end
56
    end
57
58
    def upload_media(filepath, content_type)
59
      params = {
60
        :version        => '1.1',
61
        :apikey         => @api_key,
62
        :password       => @password,
63
        :action         => 'uploadMedia',
64
        :file           => Faraday::UploadIO.new(filepath, content_type),
65
        :title          => DateTime.now.strftime('%Y-%m-%d %I:%M:%S %p'),
66
        :transcriptType => @transcript_type,
67
        :desc           => 'file description',
68
        :recordedDate   => DateTime.now.strftime('%Y-%m-%d %I:%M:%S'),
69
        :collection     => '',
70
        :public         => false,
71
        :sourceUrl      => '',
72
        :lang           => 'en',
73
        :imageUrl       => ''
74
      }
75
      response = @conn.post '/services', params
76
      pp response
77
      puts response.body['fileUrl']
78
    end
79
  end
80
end
81
82
def upload_recordings(vbsdk, dir)
83
  Dir.glob('recording_*.mp3').each_with_index do |file,i|
84
    puts file
85
    vbsdk.upload_media(file, 'audio/mpeg')
86
    break
87
  end
88
end
89
90
vbsdk = VoiceBase::Client.new(
91
  boot.credentials[:voicebase][:api_key],
92
  boot.credentials[:voicebase][:password])
93
94
upload_recordings(vbsdk, '.')
95
96
puts "DONE"
97