Sha256: 05d94b442b4cff46029e3af890717d04de43eb8288038bbfccb8df9964ecb13a

Contents?: true

Size: 1.18 KB

Versions: 10

Compression:

Stored size: 1.18 KB

Contents

module VkontakteApi
  # A module implementing files uploading functionality.
  # 
  # @note `VkontakteApi::Uploading` extends `VkontakteApi` so these methods should be called from the latter.
  module Uploading
    # Files uploading. It uses the same faraday middleware stack as API method calls (by using `VkontakteApi::API.connection`).
    # @param [Hash] params A list of files to upload (also includes the upload URL). See example for the hash format.
    # @option params [String] :url URL for the request.
    # @return [Hashie::Mash] The server response.
    # @raise [ArgumentError] raised when a `:url` parameter is omitted.
    # @example
    #   VkontakteApi.upload(
    #     url:   'http://example.com/upload',
    #     file1: ['/path/to/file1.jpg', 'image/jpeg'],
    #     file2: ['/path/to/file2.png', 'image/png']
    #   )
    def upload(params = {})
      url = params.delete(:url)
      raise ArgumentError, 'You should pass :url parameter' unless url
      
      files = {}
      params.each do |param_name, (file_path, file_type)|
        files[param_name] = Faraday::UploadIO.new(file_path, file_type)
      end
      
      API.connection.post(url, files).body
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
vkontakte_api-1.2 lib/vkontakte_api/uploading.rb
vkontakte_api-1.1 lib/vkontakte_api/uploading.rb
vkontakte_api-1.0.4 lib/vkontakte_api/uploading.rb
vkontakte_api-1.0.3 lib/vkontakte_api/uploading.rb
vkontakte_api-1.0.2 lib/vkontakte_api/uploading.rb
vkontakte_api-1.0.1 lib/vkontakte_api/uploading.rb
vkontakte_api-1.0 lib/vkontakte_api/uploading.rb
vkontakte_api-1.0.rc3 lib/vkontakte_api/uploading.rb
vkontakte_api-1.0.rc2 lib/vkontakte_api/uploading.rb
vkontakte_api-1.0.rc lib/vkontakte_api/uploading.rb