Class: FileSystem

Inherits:
Object show all
Defined in:
lib/jirametrics/file_system.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#logfileObject

Returns the value of attribute logfile.



6
7
8
# File 'lib/jirametrics/file_system.rb', line 6

def logfile
  @logfile
end

#logfile_nameObject

Returns the value of attribute logfile_name.



6
7
8
# File 'lib/jirametrics/file_system.rb', line 6

def logfile_name
  @logfile_name
end

Instance Method Details

#compress(node) ⇒ Object

In some Jira instances, a sizeable portion of the JSON is made up of empty fields. I’ve seen cases where this simple compression will drop the filesize by half.



36
37
38
39
40
41
42
43
44
# File 'lib/jirametrics/file_system.rb', line 36

def compress node
  if node.is_a? Hash
    node.reject! { |_key, value| value.nil? || (value.is_a?(Array) && value.empty?) }
    node.each_value { |value| compress value }
  elsif node.is_a? Array
    node.each { |a| compress a }
  end
  node
end

#load(filename) ⇒ Object

Effectively the same as File.read except it forces the encoding to UTF-8



9
10
11
# File 'lib/jirametrics/file_system.rb', line 9

def load filename
  File.read filename, encoding: 'UTF-8'
end

#load_json(filename, fail_on_error: true) ⇒ Object



13
14
15
16
17
# File 'lib/jirametrics/file_system.rb', line 13

def load_json filename, fail_on_error: true
  return nil if fail_on_error == false && File.exist?(filename) == false

  JSON.parse load(filename)
end

#log(message) ⇒ Object



30
31
32
# File 'lib/jirametrics/file_system.rb', line 30

def log message
  logfile.puts message
end

#save_file(content:, filename:) ⇒ Object



23
24
25
26
27
28
# File 'lib/jirametrics/file_system.rb', line 23

def save_file content:, filename:
  file_path = File.dirname(filename)
  FileUtils.mkdir_p file_path unless File.exist?(file_path)

  File.write(filename, content)
end

#save_json(json:, filename:) ⇒ Object



19
20
21
# File 'lib/jirametrics/file_system.rb', line 19

def save_json json:, filename:
  save_file content: JSON.pretty_generate(compress json), filename: filename
end