Sha256: 10a8fb07e482da624c75ef71f92af5cac5a9383fcaa75e9710555b3db2adf2ca

Contents?: true

Size: 965 Bytes

Versions: 2

Compression:

Stored size: 965 Bytes

Contents

module Roart

  class AttachmentCollection < Array
    def to_payload
      hash = Hash.new
      self.each_with_index do |attach, index|
        hash["attachment_#{index+1}"] = attach
      end
      hash
    end
  end

  class Attachment < File

    attr_reader :name, :file

    def initialize(name, file)
      @name = name
      @file = file
    end

    def path
      name
    end

    def read
      file.read
    end

    def mime_type
      file.content_type if file.respond_to?(:content_type)
    end

    def self.detect(*args)
      AttachmentCollection.new Array(args.compact).flatten.map { |file|
        if file.is_a?(File)
          Attachment.new(File.basename(file.path), file)
        elsif file.is_a?(String)
          Attachment.new(File.basename(file), File.open(file, 'rb'))
        elsif file.respond_to?(:open, :original_filename)
          Attachment.new(file.original_filename, file.open)
        end
      }.compact
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ludo-roart-0.1.19 lib/roart/attachment.rb
ludo-roart-0.1.18 lib/roart/attachment.rb