Class: GitLab::CI::Lint::YMLReader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ YMLReader

Returns a new instance of YMLReader.



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

def initialize file
  @file = file
  validate!
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



8
9
10
# File 'lib/gitlab/ci/lint/yml.rb', line 8

def file
  @file
end

Instance Method Details

#get_contentObject



20
21
22
23
24
25
26
# File 'lib/gitlab/ci/lint/yml.rb', line 20

def get_content
  begin
    return YAML.load(File.read(@file))
  rescue ArgumentError => error
    puts "Could not parse the YAML File: #{error.message}"
  end
end

#get_json_contentObject



28
29
30
31
# File 'lib/gitlab/ci/lint/yml.rb', line 28

def get_json_content
  content = JSON.pretty_generate(get_content())
  return valid_json?(content) ? content : nil
end

#validate!Object



14
15
16
17
18
# File 'lib/gitlab/ci/lint/yml.rb', line 14

def validate!
  unless @file.chars.last(4).join == ".yml" or @file.chars.last(5).join == ".yaml"
    raise ArgumentError.new("We need a YML File...")
  end
end