Class: R53z::JsonFile

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging, Methadone::Main
Defined in:
lib/r53z/file.rb

Overview

Returns a hash of the contents of named file

Class Method Summary collapse

Class Method Details

.fix_path_json(path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/r53z/file.rb', line 21

def self.fix_path_json(path)
  unless path[-5..-1] == '.json'
    if path[-1] == '.'
      return path + 'json'
    else
      return path + '.json'
    end
  end
  return path
end

.read_json(path:) ⇒ Object



10
11
12
13
# File 'lib/r53z/file.rb', line 10

def self.read_json(path:)
  file = File.read(self.fix_path_json(path))
  JSON.parse(file, :symbolize_names => true)
end

.write_json(path:, data:) ⇒ Object



15
16
17
18
19
# File 'lib/r53z/file.rb', line 15

def self.write_json(path:, data:)
  File.open(self.fix_path_json(path), 'w') do |f|
    f.write(JSON.pretty_generate(data))
  end
end