Class: AssemblyAI::AsyncFilesClient

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:) ⇒ AsyncFilesClient

Parameters:



65
66
67
68
# File 'lib/assemblyai/files/client.rb', line 65

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

Instance Attribute Details

#request_clientObject (readonly)

Returns the value of attribute request_client.



61
62
63
# File 'lib/assemblyai/files/client.rb', line 61

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:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/assemblyai/files/client.rb', line 75

def upload(file:, request_options: nil)
  Async do
    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
end