class Warnings::Parser

Base parser class to define common methods.

Constants

ERROR_EXT_NOT_JSON
ERROR_FILE_NOT_EXIST
EXT_JSON

Attributes

issues[RW]

All issues found by the parser.

@return [Array<Issue>]

Public Class Methods

new() click to toggle source
# File lib/warnings/parser/parser.rb, line 24
def initialize
  @issues = []
end

Protected Instance Methods

json(file_path) click to toggle source

Parse a file as json content.

@param file_path [String] Path to a file to be read as json. @return [String] Hash of json values.

# File lib/warnings/parser/parser.rb, line 42
def json(file_path)
  raise(format(ERROR_EXT_NOT_JSON, file_path)) unless json?(file_path)

  content = read_file(file_path)
  JSON.parse(content)
end
json?(file_path) click to toggle source

Check if the file is a json file.

@param file_path [String] Path to a file to be read as json. @return [Bool] Whether the file is a json or not.

# File lib/warnings/parser/parser.rb, line 34
def json?(file_path)
  File.extname(file_path).delete('.').downcase.eql?(EXT_JSON)
end
read_lines(file_path) click to toggle source

Read the file into memory and serve each line.

@param file_path [String] Path to a file to be read. @raise If file does not exist. @return [String] Array of line contents.

# File lib/warnings/parser/parser.rb, line 54
def read_lines(file_path)
  raise(format(ERROR_FILE_NOT_EXIST, file_path)) unless File.exist?(file_path)

  File.readlines(file_path, chomp: true)
end

Private Instance Methods

read_file(file_path) click to toggle source

Evaluate and read the file into memory.

@param file_path [String] Path to a file to be read. @raise If file does not exist or ist empty. @return [String] File content.

# File lib/warnings/parser/parser.rb, line 67
def read_file(file_path)
  raise(format(ERROR_FILE_NOT_EXIST, file_path)) unless File.exist?(file_path)

  File.read(file_path)
end