Sha256: c4d8a7bf47a3160312ed451a8ca834e1c4dd25f6d2a6bf2b072432b535d98250

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

# encoding: utf-8
require 'fileutils'

module Imap
  module Backup
    module Serializer
      class Directory

        def initialize(path, folder)
          @path, @folder = path, folder
          permissions = 0700
          Imap::Backup::Utils.check_permissions(@path, permissions)
          Imap::Backup::Utils.make_folder(@path, @folder, permissions)
        end

        def uids
          return [] if ! File.exist?(directory)

          d = Dir.open(directory)
          as_strings = d.map do |file|
            file[/^0*(\d+).json$/, 1]
          end.compact
          as_strings.map(&:to_i).sort
        end

        def exist?(uid)
          message_filename = filename(uid)
          File.exist?(message_filename)
        end

        def save(uid, message)
          message_filename = filename(uid)
          File.open(message_filename, 'w') { |f| f.write message.to_json }
          FileUtils.chmod 0600, message_filename
        end

        private

        def directory
          File.join(@path, @folder)
        end

        def filename(uid)
          "#{directory}/%012u.json" % uid.to_i
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
imap-backup-0.0.5 lib/imap/backup/serializer/directory.rb
imap-backup-0.0.4 lib/imap/backup/serializer/directory.rb