Module: CloudImagesRunDeck::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/cloudimages-rundeck/util.rb

Overview

> Utility Methods

Instance Method Summary collapse

Instance Method Details

#filestring(file, size = 8192) ⇒ Object

> Check if a string is an existing file, and return it’s content



42
43
44
45
46
# File 'lib/cloudimages-rundeck/util.rb', line 42

def filestring(file, size = 8192)
  return unless file
  return file unless file.is_a?(String) && File.file?(file) && File.size(file) <= size
  File.read(file)
end

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

> Define JSON Parser



24
25
26
27
28
29
30
31
# File 'lib/cloudimages-rundeck/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 <= #



52
53
54
55
# File 'lib/cloudimages-rundeck/util.rb', line 52

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

#serialize_csv(csv) ⇒ Object



57
58
59
60
61
# File 'lib/cloudimages-rundeck/util.rb', line 57

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



63
64
65
66
67
68
69
# File 'lib/cloudimages-rundeck/util.rb', line 63

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/cloudimages-rundeck/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