Class: Bandwidth::Media

Inherits:
Object
  • Object
show all
Extended by:
ClientWrapper
Defined in:
lib/bandwidth/media.rb

Overview

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

Class Method Summary collapse

Methods included from ClientWrapper

wrap_client_arg

Class Method Details

.delete(client, name) ⇒ Object

Remove file from the Bandwidth API server

Examples:

Media.delete(client, "file.pdf")

Parameters:

  • client (Client)

    optional client instance to make requests

  • name (String)

    file name



58
59
60
# 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) ⇒ Array

Download file from Bandwidth API server

Examples:

file_content, media_type = Media.download(client, "file.pdf")

Parameters:

  • client (Client)

    optional client instance to make requests

  • name (String)

    file name

Returns:

  • (Array)

    file content and media type



45
46
47
48
49
# 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) ⇒ Hash

Get information about a media file

Examples:

Media.get_info(client, "file.pdf")

Parameters:

  • client (Client)

    optional client instance to make requests

  • name (String)

    file name

Returns:

  • (Hash)

    content_type and content_length



69
70
71
72
73
# 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) ⇒ Array

Get a list of your media files

Examples:

files = Media.list(client)

Parameters:

  • client (Client)

    optional client instance to make requests

  • query (Hash) (defaults to: nil)

    query options

Returns:

  • (Array)

    array with file information



14
15
16
# 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) ⇒ Object

Upload file to the Bandwidth API server

Examples:

Media.upload((client, "file.pdf", File.open("/path/to/file.pdf", "r"), "application/pdf")

Parameters:

  • client (Client)

    optional client instance to make requests

  • name (String)

    file name

  • io (IO)

    io object file file content (file, string, etc)

  • media_type (defaults to: nil)

    media type of file



26
27
28
29
30
31
32
33
34
35
36
# 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