Class: Runger::EJSONParser

Inherits:
Object
  • Object
show all
Defined in:
lib/runger/ejson_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bin_path = 'ejson') ⇒ EJSONParser

Returns a new instance of EJSONParser.



11
12
13
# File 'lib/runger/ejson_parser.rb', line 11

def initialize(bin_path = 'ejson')
  @bin_path = bin_path
end

Instance Attribute Details

#bin_pathObject (readonly)

Returns the value of attribute bin_path.



9
10
11
# File 'lib/runger/ejson_parser.rb', line 9

def bin_path
  @bin_path
end

Instance Method Details

#call(file_path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/runger/ejson_parser.rb', line 15

def call(file_path)
  return unless File.exist?(file_path)

  raw_content = nil

  stdout, stderr, status = Open3.capture3("#{bin_path} decrypt #{file_path}")

  if status.success?
    raw_content = JSON.parse(stdout.chomp)
  else
    Kernel.warn("Failed to decrypt #{file_path}: #{stderr}")
  end

  return unless raw_content

  raw_content.deep_transform_keys do |key|
    if key[0] == '_'
      # rubocop:disable Performance/ArraySemiInfiniteRangeSlice
      key[1..]
      # rubocop:enable Performance/ArraySemiInfiniteRangeSlice
    else
      key
    end
  end
end