Class: AssemblyAI::FilesClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_client:) ⇒ FilesClient

Parameters:



14
15
16
17
# File 'lib/assemblyai/files/client.rb', line 14

def initialize(request_client:)
  # @type [RequestClient]
  @request_client = request_client
end

Instance Attribute Details

#request_clientObject (readonly)

Returns the value of attribute request_client.



10
11
12
# File 'lib/assemblyai/files/client.rb', line 10

def request_client
  @request_client
end

Instance Method Details

#upload(file:, request_options: nil) ⇒ Files::UploadedFile

Upload your media file directly to the AssemblyAI API if it isn’t accessible via a URL already.

Parameters:

  • file (String, IO)

    File path, Base64 String, or an IO object (e.g. Faraday::UploadIO, etc.)

  • request_options (RequestOptions) (defaults to: nil)

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/assemblyai/files/client.rb', line 24

def upload(file:, request_options: nil)
  response = @request_client.conn.post do |req|
    if file.is_a? String
      begin
        path = Pathname.new(file)
      rescue StandardError
        file_data = file
      end
      unless path.nil?
        if path.file?
          file_data = File.new(file)
        elsif path.directory?
          raise "file path has to be a path to file, but is a path to directory"
        else
          raise "file at path does not exist"
        end
      end
    else
      file_data = file
    end
    req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
    req.headers["Authorization"] = request_options.api_key unless request_options&.api_key.nil?
    req.headers = { **req.headers, **(request_options&.additional_headers || {}) }.compact
    req.headers["Content-Type"] = "application/octet-stream"
    if file.is_a? File
      req.headers["Content-Length"] = File.size(file_data.path).to_s
    else
      req.headers["Transfer-Encoding"] = "chunked"
    end
    req.body = file_data
    req.url "#{@request_client.get_url(request_options: request_options)}/v2/upload"
  end
  Files::UploadedFile.from_json(json_object: response.body)
end