Method: AssemblyAI::AsyncFilesClient#upload
- Defined in:
- lib/assemblyai/files/client.rb
#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.
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..timeout = .timeout_in_seconds unless &.timeout_in_seconds.nil? req.headers["Authorization"] = .api_key unless &.api_key.nil? req.headers = { **req.headers, **(&.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 |