Sha256: 6bee76e94f064e9a1d4998e35dcd0c93434b8e83772e5bc79d4178bd07e27e52

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require "mime/types"

module Nylas
  # A collection of file-related utilities.
  module FileUtils
    # Build a form request for the API.
    # @param request_body The values to create the message with.
    # @return The form data to send to the API and the opened files.
    # @!visibility private
    def self.build_form_request(request_body)
      attachments = request_body.delete(:attachments) || request_body.delete("attachments") || []
      message_payload = request_body.to_json

      # Prepare the data to return
      form_data = {}
      opened_files = []

      attachments.each_with_index do |attachment, index|
        file = attachment[:content] || attachment["content"]
        form_data.merge!({ "file#{index}" => file })
        opened_files << file
      end

      form_data.merge!({ "multipart" => true, "message" => message_payload })

      [form_data, opened_files]
    end

    # Build the request to attach a file to a message/draft object.
    # @param file_path [String] The path to the file to attach.
    # @return [Hash] The request that will attach the file to the message/draft
    def self.attach_file_request_builder(file_path)
      filename = File.basename(file_path)
      content_type = MIME::Types.type_for(file_path).first.to_s
      content_type = "application/octet-stream" if content_type.empty?
      size = File.size(file_path)
      content = File.new(file_path, "rb")

      {
        filename: filename,
        content_type: content_type,
        size: size,
        content: content
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nylas-6.0.0.beta.2 lib/nylas/utils/file_utils.rb