class Bandwidth::Media

The Media resource lets you upload your media files to Bandwidth API servers

Public Class Methods

delete(client, name) click to toggle source

Remove file from the Bandwidth API server @param client [Client] optional client instance to make requests @param name [String] file name @example

Media.delete(client, "file.pdf")
# File lib/bandwidth/media.rb, line 58
def self.delete(client, name)
  client.make_request(:delete, client.concat_user_path("#{MEDIA_PATH}/#{URI.encode(name)}"))[0]
end
download(client, name) click to toggle source

Download file from Bandwidth API server @param client [Client] optional client instance to make requests @param name [String] file name @return [Array] file content and media type @example

file_content, media_type = Media.download(client, "file.pdf")
# File lib/bandwidth/media.rb, line 45
def self.download(client, name)
  connection = client.create_connection()
  response = connection.get("/#{client.api_version}#{client.concat_user_path(MEDIA_PATH)}/#{URI.encode(name)}")
  [response.body, response.headers['Content-Type'] || 'application/octet-stream']
end
get_info(client, name) click to toggle source

Get information about a media file @param client [Client] optional client instance to make requests @param name [String] file name @return [Hash] content_type and content_length @example

Media.get_info(client, "file.pdf")
# File lib/bandwidth/media.rb, line 69
def self.get_info(client, name)
  headers = client.make_request(:head, client.concat_user_path("#{MEDIA_PATH}/#{URI.encode(name)}"))[1]
  puts headers
  {:content_type => headers[:content_type], :content_length => headers[:content_length]}
end
list(client, query = nil) click to toggle source

Get a list of your media files @param client [Client] optional client instance to make requests @param query [Hash] query options @return [Array] array with file information @example

files = Media.list(client)
# File lib/bandwidth/media.rb, line 14
def self.list(client, query = nil)
  client.make_request(:get, client.concat_user_path(MEDIA_PATH), query)[0]
end
upload(client, name, io, media_type = nil) click to toggle source

Upload file to the Bandwidth API server @param client [Client] optional client instance to make requests @param name [String] file name @param io [IO] io object file file content (file, string, etc) @param media_type media type of file @example

Media.upload((client, "file.pdf", File.open("/path/to/file.pdf", "r"), "application/pdf")
# File lib/bandwidth/media.rb, line 26
def self.upload(client, name, io, media_type = nil)
  connection = client.create_connection()
  # FIXME use streams directly when Faraday will be support streaming
  buf = io.read()
  response = connection.put("/#{client.api_version}#{client.concat_user_path(MEDIA_PATH)}/#{URI.encode(name)}") do |req|
    req.headers['Content-Length'] = buf.size.to_s
    req.headers['Content-Type'] = media_type || 'application/octet-stream'
    req.body = buf
  end
  client.check_response(response)
end