Class: Smith::Artifacts::File

Inherits:
Object
  • Object
show all
Defined in:
lib/smith/artifacts/file.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir:, namespace: nil) ⇒ File

Returns a new instance of File.



9
10
11
12
# File 'lib/smith/artifacts/file.rb', line 9

def initialize(dir:, namespace: nil)
  @dir = dir
  @namespace = namespace
end

Instance Method Details

#expired(retention: nil, execution_namespace: nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/smith/artifacts/file.rb', line 28

def expired(retention: nil, execution_namespace: nil)
  return [] unless retention

  cutoff = Time.now.utc - retention
  Dir.glob(::File.join(@dir, "*")).reject { |f| f.end_with?(".meta") }.filter_map do |path|
    ref = ::File.basename(path)
    next unless ::File.mtime(path).utc < cutoff
    next if execution_namespace && !matches_execution_namespace?(ref, execution_namespace)

    ref
  end
end

#fetch(ref) ⇒ Object



23
24
25
26
# File 'lib/smith/artifacts/file.rb', line 23

def fetch(ref)
  path = ::File.join(@dir, ref)
  ::File.exist?(path) ? ::File.read(path) : nil
end

#store(data, content_type: "application/octet-stream", execution_namespace: nil) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/smith/artifacts/file.rb', line 14

def store(data, content_type: "application/octet-stream", execution_namespace: nil)
  ref = generate_ref
  meta = { content_type: content_type }
  meta[:execution_namespace] = execution_namespace if execution_namespace
  ::File.write(::File.join(@dir, ref), data)
  ::File.write(::File.join(@dir, "#{ref}.meta"), JSON.generate(meta))
  ref
end