Module: DeployInfo::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/deploy-info/util.rb

Overview

> Utility Methods

Instance Method Summary collapse

Instance Method Details

#parse_json_config(file = nil, symbolize = true) ⇒ Object

> Define JSON Parser



24
25
26
27
28
29
30
31
# File 'lib/deploy-info/util.rb', line 24

def parse_json_config(file = nil, symbolize = true)
  return unless file && ::File.exist?(file.to_s)
  begin
    ::JSON.parse(::File.read(file.to_s), symbolize_names: symbolize)
  rescue JSON::ParserError
    return
  end
end

#serialize(response) ⇒ Object

> Serialization <= #



45
46
47
48
# File 'lib/deploy-info/util.rb', line 45

def serialize(response)
  # => Serialize Object into JSON Array
  JSON.pretty_generate(response.map(&:name).sort_by(&:downcase))
end

#serialize_csv(csv) ⇒ Object



50
51
52
53
54
# File 'lib/deploy-info/util.rb', line 50

def serialize_csv(csv)
  # => Serialize a CSV String into an Array
  return unless csv && csv.is_a?(String)
  csv.split(',')
end

#serialize_revisions(branches, tags) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/deploy-info/util.rb', line 56

def serialize_revisions(branches, tags)
  # => Serialize Branches/Tags into JSON Array
  # => Branches = String, Tags = Key/Value
  branches = branches.map(&:name).sort_by(&:downcase)
  tags = tags.map(&:name).sort_by(&:downcase).reverse.map { |tag| { name: "Tag: #{tag}", value: tag } }
  JSON.pretty_generate(branches + tags)
end

#write_json_config(file, object) ⇒ Object

> Define JSON Writer



34
35
36
37
38
39
# File 'lib/deploy-info/util.rb', line 34

def write_json_config(file, object)
  return unless file && object
  begin
    File.open(file, 'w') { |f| f.write(JSON.pretty_generate(object)) }
  end
end