Class: Gitlab::Lint::Client::YamlFile

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/lint/client/yml.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ YamlFile

Returns a new instance of YamlFile.



11
12
13
14
# File 'lib/gitlab/lint/client/yml.rb', line 11

def initialize(file)
  @file = file
  validate!
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



9
10
11
# File 'lib/gitlab/lint/client/yml.rb', line 9

def file
  @file
end

Instance Method Details

#get_contentObject



23
24
25
26
27
28
29
# File 'lib/gitlab/lint/client/yml.rb', line 23

def get_content
  begin
    return YAML.load_file(@file)
  rescue Psych::SyntaxError => error
    puts "Failed to parse the YAML File: #{error.message}"
  end
end

#get_json_contentObject



31
32
33
34
# File 'lib/gitlab/lint/client/yml.rb', line 31

def get_json_content
  content = JSON.generate(get_content())
  return json_ok?(content) ? content : nil
end

#validate!Object

Raises:

  • (NameError)


16
17
18
19
20
21
# File 'lib/gitlab/lint/client/yml.rb', line 16

def validate!
  raise NameError unless @file
  raise ArgumentError unless @file.chars.last(4).join == ".yml" or @file.chars.last(5).join == ".yaml"
  raise IOError unless ::File.exist?(@file)
  raise RuntimeError unless ::File.readable?(@file)
end