class GitLab::CI::Lint::YMLReader

Attributes

file[R]

Public Class Methods

new(file) click to toggle source
# File lib/gitlab/ci/lint/yml.rb, line 9
def initialize file
  @file = file
  validate!
end

Public Instance Methods

get_content() click to toggle source
# 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_content() click to toggle source
# 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!() click to toggle source
# 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

Private Instance Methods

valid_json?(json) click to toggle source
# File lib/gitlab/ci/lint/yml.rb, line 35
def valid_json? json
  begin
    JSON.parse(json)
    return true
  rescue
    return false
  end
end