Class: Plivo::Resources::MediaInterface

Inherits:
Base::ResourceInterface show all
Defined in:
lib/plivo/resources/media.rb

Constant Summary

Constants included from Utils

Utils::TYPE_WHITELIST

Instance Method Summary collapse

Methods included from Utils

expected_type?, expected_value?, raise_invalid_request, valid_account?, valid_mainaccount?, valid_param?, valid_signature?, valid_subaccount?

Constructor Details

#initialize(client, resource_list_json = nil) ⇒ MediaInterface

Returns a new instance of MediaInterface.



25
26
27
28
29
30
# File 'lib/plivo/resources/media.rb', line 25

def initialize(client, resource_list_json = nil)
  @_name = 'Media'
  @_resource_type = Media
  @_identifier_string = 'media_id'
  super
end

Instance Method Details

#get(media_id) ⇒ Media

Get an Media

Parameters:

  • media_id (String)

Returns:



36
37
38
39
# File 'lib/plivo/resources/media.rb', line 36

def get(media_id)
  valid_param?(:media_id, media_id, [String, Symbol], true)
  perform_get(media_id)
end

#list(options = nil) ⇒ Hash

List all Media

Parameters:

  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :offset (Int)
  • :limit (Int)

Returns:

  • (Hash)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/plivo/resources/media.rb', line 47

def list(options=nil)
  return perform_list_without_object if options.nil?

  params = {}
  %i[offset limit].each do |param|
    if options.key?(param) && valid_param?(param, options[param],
                                           [Integer], true)
      params[param] = options[param]
    end
  end

  if options.key?(:limit) && (options[:limit] > 20 || options[:limit] <= 0)
    raise_invalid_request('The maximum number of results that can be '\
    "fetched is 20. limit can't be more than 20 or less than 1")
  end

  if options.key?(:offset) && options[:offset] < 0
    raise_invalid_request("Offset can't be negative")
  end

  perform_list_without_object(params)
end

#upload(filepath) ⇒ Object

Create a new upload



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/plivo/resources/media.rb', line 72

def upload(filepath)
  params = {}
  for file_to_upload in filepath do
    unless file_to_upload.nil?
    file_extension = file_to_upload.split('.')[-1]

    content_type = case file_extension
                     when 'jpeg' then 'image/jpeg'
                     when 'jpg' then 'image/jpeg'
                     when 'png' then 'image/png'
                     when 'pdf' then 'application/pdf'
                     when 'xcf' then 'image/xcf'
                     when 'text' then 'text/plain'
                     when 'mpeg' then 'video/mpeg'
                     when 'mp4' then 'video/mp4'
                     else raise_invalid_request("#{file_extension} is not yet supported for upload")
                   end

    params[:file] = Faraday::UploadIO.new(file_to_upload, content_type)
    end
  end
  perform_create(params, true)
end