Client   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %
Metric Value
wmc 5
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 7 1
B upload_media() 0 29 3
A new_http_client() 0 9 1
1
require 'date'
2
require 'faraday'
3
require 'faraday_middleware'
4
5
module VoiceBase::V1
6
  VOICEBASE_API_VERSION = '1.1'
7
8
  attr_accessor :conn_url_encoded
9
  attr_accessor :conn_multipart
10
11
  class Client
12
    def initialize(api_key, password, transcript_type = 'machine-best')
13
      @api_key = api_key
14
      @password = password
15
      @transcript_type = transcript_type
16
      @conn_url_encoded = new_http_client :url_encoded
17
      @conn_multipart = new_http_client :multipart
18
    end
19
20
    def new_http_client(request = :url_encoded)
21
      Faraday.new(url: VoiceBase::VOICEBASE_API_BASE_PATH) \
22
      do |faraday|
23
        faraday.request request
24
        faraday.response :json
25
        faraday.response :logger                # log requests to STDOUT
26
        faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
27
      end
28
    end
29
30
    def upload_media(params = {})
31
      params = {
32
        version: VOICEBASE_API_VERSION,
33
        apikey: @api_key,
34
        password: @password,
35
        action: 'uploadMedia',
36
        title: DateTime.now.strftime('%Y-%m-%d %I:%M:%S %p'),
37
        transcriptType: @transcript_type,
38
        desc: 'file description',
39
        recordedDate: DateTime.now.strftime('%Y-%m-%d %I:%M:%S'),
40
        collection: '',
41
        public: false,
42
        sourceUrl: '',
43
        lang: 'en',
44
        imageUrl: ''
45
      }.merge params
46
47
      if params.key?(:filePath) && params.key?(:fileContentType)
48
        params[:file] = Faraday::UploadIO.new(
49
          params[:filePath],
50
          params[:fileContentType]
51
        )
52
        params.delete :filePath
53
        params.delete :fileContentType
54
        return @conn_multipart.post '/services', params
55
      else
56
        return @conn_url_encoded.post '/services', params
57
      end
58
    end
59
  end
60
end
61