Class: KNSEmailEndpoint::Storage::FileStorage

Inherits:
AbstractStorage show all
Defined in:
lib/kns_email_endpoint/storage/file_storage.rb

Instance Attribute Summary collapse

Attributes inherited from AbstractStorage

#message_id, #retry_count, #state, #unique_id

Instance Method Summary collapse

Methods inherited from AbstractStorage

#find_or_create, #to_h

Constructor Details

#initialize(settings = {}) ⇒ FileStorage

Returns a new instance of FileStorage.



8
9
10
11
12
13
14
15
16
# File 'lib/kns_email_endpoint/storage/file_storage.rb', line 8

def initialize(settings={})
  @dir = settings[:file_location]
  raise "Unknown file_location" unless @dir
  FileUtils.mkdir_p @dir

  super(settings)
  @name = "file"
          
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/kns_email_endpoint/storage/file_storage.rb', line 6

def name
  @name
end

Instance Method Details

#create(opts = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kns_email_endpoint/storage/file_storage.rb', line 25

def create(opts={})
  options = {
    :state => :unprocessed,
    :retry_count => 0
  }.merge! opts
  raise ":unique_id is required" unless options[:unique_id]
  raise ":message_id is required" unless options[:message_id]

  @current_file = File.join(@dir, options[:unique_id]) 

  if File.exists? @current_file
    raise "unique_id #{options[:unique_id]} already exists"
  end
  
  set_vars(options)
  save!
  
  return true
end

#deleteObject



18
19
20
21
22
23
# File 'lib/kns_email_endpoint/storage/file_storage.rb', line 18

def delete
  return false unless @current_file
  FileUtils.rm @current_file if File.exists? @current_file
  reset_storage
  return true
end

#delete_allObject



57
58
59
60
# File 'lib/kns_email_endpoint/storage/file_storage.rb', line 57

def delete_all
  FileUtils.remove_dir(@dir, true)
  FileUtils.mkdir_p @dir
end

#find(unique_id) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kns_email_endpoint/storage/file_storage.rb', line 45

def find(unique_id)
  lookup_file = File.join(@dir, unique_id)
  if File.exists? lookup_file
    @current_file = lookup_file
    set_vars JSON.parse(File.open(@current_file, 'r').read)
    return self
  else
    reset_storage
    return nil
  end
end