Module: VkontakteApi::Uploading

Included in:
VkontakteApi
Defined in:
lib/vkontakte_api/uploading.rb

Overview

Note:

VkontakteApi::Uploading extends VkontakteApi so these methods should be called from the latter.

A module implementing files uploading functionality.

Instance Method Summary collapse

Instance Method Details

#upload(params = {}) ⇒ Hashie::Mash

Files uploading. It uses the same faraday middleware stack as API method calls (by using VkontakteApi::API.connection).

Examples:

VkontakteApi.upload(
  url:   'http://example.com/upload',
  file1: ['/path/to/file1.jpg', 'image/jpeg'],
  file2: [io_object, 'image/png', '/path/to/file2.png']
)
# alternative syntax
VkontakteApi.upload(
  url:   'http://example.com/upload',
  files: [
    ['/path/to/file1.jpg', 'image/jpeg'],
    [io_object, 'image/png', '/path/to/file2.png']
  ]
)

Parameters:

  • params (Hash) (defaults to: {})

    A list of files to upload (also includes the upload URL). See example for the hash format.

Options Hash (params):

  • :url (String)

    URL for the request.

Returns:

  • (Hashie::Mash)

    The server response.

Raises:

  • (ArgumentError)

    raised when a :url parameter is omitted.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vkontakte_api/uploading.rb', line 25

def upload(params = {})
  url = params.delete(:url)
  raise ArgumentError, 'You should pass :url parameter' unless url
  
  (params.delete(:files) || []).each_with_index do |file, index|
    key = "file#{index.succ}"
    params[key.to_sym] = file
  end
  
  files = {}
  params.each do |param_name, (file_path_or_io, file_type, file_path)|
    files[param_name] = Faraday::UploadIO.new(file_path_or_io, file_type, file_path)
  end
  
  API.connection.post(url, files).body
end