class Chchchanges::Parser

Attributes

entries[RW]
type[RW]
version[RW]

Public Class Methods

new() click to toggle source
# File lib/chchchanges/parser.rb, line 7
def initialize
  @entries = []
  @version = ""
  @type = ""
end

Public Instance Methods

call() click to toggle source
# File lib/chchchanges/parser.rb, line 13
def call
  Dir.mkdir('.changelog_entries') unless Dir.exists?('.changelog_entries')
  parse_changelog
  write_to_files
end

Private Instance Methods

create_entry(line, version, type) click to toggle source
# File lib/chchchanges/parser.rb, line 39
def create_entry(line, version, type)
  description = get_description(line)
  ticket = get_ticket(line)
  info = {
    type: type,
    ticket: ticket,
    url: "",
    description: description,
    version: version,
    tags: []
  }.to_json
  @entries << info
end
get_description(line) click to toggle source
# File lib/chchchanges/parser.rb, line 29
def get_description(line)
  match = line.match(/^.*[\]\-] /)[0]
  line.gsub(match, '')
end
get_ticket(line) click to toggle source
# File lib/chchchanges/parser.rb, line 34
def get_ticket(line)
  match = line.match(/\[(.*?)\]/)
  match ? match[1] : ""
end
get_type(line) click to toggle source
# File lib/chchchanges/parser.rb, line 25
def get_type(line)
  line.gsub('### ', '')
end
get_version(line) click to toggle source
# File lib/chchchanges/parser.rb, line 21
def get_version(line)
  line.match(/\[(.*?)\]/)[1]
end
parse_changelog() click to toggle source
# File lib/chchchanges/parser.rb, line 53
def parse_changelog
  changelog_lines = File.readlines('CHANGELOG.md')
  changelog_lines.each_with_index do |line, index|
    line.strip!
    if line.start_with?('## [')
      @version = get_version(line)
    elsif line.start_with?('###')
      @type = get_type(line)
    elsif line.start_with?('-')
      create_entry(line, version, type)
    elsif line.empty?
    else
      puts "Warning: Unexpected input at line #{index + 1}"
    end
  end
end
write_to_files() click to toggle source
# File lib/chchchanges/parser.rb, line 70
def write_to_files
  @entries = @entries.uniq
  @entries.each do |entry|
    File.write(".changelog_entries/#{SecureRandom.hex}.json", entry)
  end
end