Sha256: 4bac20bd97fea8a2875d412653479a1c729b6b77a201187bee8a08fe21c6e0d2

Contents?: true

Size: 1.59 KB

Versions: 6

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

require_relative 'upload_client'

module Uploadcare
  module Client
    # Groups serve a purpose of better organizing files in your Uploadcare projects.
    # You can create one from a set of files by using their UUIDs.
    # @see https://uploadcare.com/docs/api_reference/upload/groups/
    class GroupClient < UploadClient
      # Create files group from a set of files by using their UUIDs.
      # @see https://uploadcare.com/api-refs/upload-api/#operation/createFilesGroup
      def create(file_list, **options)
        body_hash = group_body_hash(file_list, **options)
        body = HTTP::FormData::Multipart.new(body_hash)
        post(path: 'group/',
             headers: { 'Content-Type': body.content_type },
             body: body)
      end

      # Get group info
      # @see https://uploadcare.com/api-refs/upload-api/#operation/filesGroupInfo
      def info(group_id)
        get(path: 'group/info/', params: { 'pub_key': Uploadcare.config.public_key, 'group_id': group_id })
      end

      private

      def file_params(file_ids)
        ids = (0...file_ids.size).map { |i| "files[#{i}]" }
        ids.zip(file_ids).to_h
      end

      def group_body_hash(file_list, **options)
        { pub_key: Uploadcare.config.public_key }.merge(file_params(parse_file_list(file_list))).merge(**options)
      end

      # API accepts only list of ids, but some users may want to upload list of files
      # @return [Array] of [String]
      def parse_file_list(file_list)
        file_list.map { |file| file.methods.include?(:uuid) ? file.uuid : file }
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
uploadcare-ruby-3.3.2 lib/uploadcare/client/group_client.rb
uploadcare-ruby-3.3.1 lib/uploadcare/client/group_client.rb
uploadcare-ruby-3.3.0 lib/uploadcare/client/group_client.rb
uploadcare-ruby-3.2.0 lib/uploadcare/client/group_client.rb
uploadcare-ruby-3.1.1 lib/uploadcare/client/group_client.rb
uploadcare-ruby-3.1.0 lib/uploadcare/client/group_client.rb