Class: Rawfeed::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/rawfeed/core/utils.rb

Overview

Utility methods for the Rawfeed framework. Provides helper functionality such as directory creation, slug generation, date formatting, user prompts, and file payload compilation.

Class Method Summary collapse

Class Method Details

.confirm(message) ⇒ String

Prompts the user for a Yes/No confirmation. Forces the user to input ‘y’ or ‘n’.

Parameters:

  • message (String)

    The message to display to the user.

Returns:

  • (String)

    Returns ‘y’ or ‘n’ based on user input.



57
58
59
60
61
62
63
64
65
# File 'lib/rawfeed/core/utils.rb', line 57

def self.confirm(message)
  print "#{message} [y/n]: ".blue
  answer = STDIN.gets.chomp.downcase
  until %w[y n].include?(answer)
    print "[!] Please type 'y' or 'n': ".yellow
    answer = STDIN.gets.chomp.downcase
  end
  answer
end

.create_directory(path) ⇒ void

This method returns an undefined value.

Safely creates a directory if it does not already exist.

Parameters:

  • path (String)

    The path of the directory to create.



23
24
25
26
27
28
# File 'lib/rawfeed/core/utils.rb', line 23

def self.create_directory(path)
  unless File.directory?(path)
    FileUtils.mkdir_p(path)
    puts ":: Folder '#{path}', created!".green
  end
end

.datetime_generator(parameter) ⇒ String

Generates a formatted datetime string. Uses the ‘date’ environment variable if present, otherwise uses the current time.

Parameters:

  • parameter (String)

    The strftime formatting string.

Returns:

  • (String)

    The formatted time string.



45
46
47
48
49
50
# File 'lib/rawfeed/core/utils.rb', line 45

def self.datetime_generator(parameter)
  (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime(parameter)
rescue ArgumentError
  puts "[x] Error - date format must be YYYY-MM-DD, please check you typed it correctly!".red
  exit 1
end

.engineer(directory, message, type) ⇒ Array<String>

Scaffolds a new content file (e.g., page, post) by asking the user for a title and generating the required slug and dates.

Parameters:

  • directory (String)

    The directory where the file will be saved.

  • message (String)

    The prompt message to ask the user for a title.

  • type (String)

    The type of content (‘page’ or other for time-prefixed files).

Returns:

  • (Array<String>)

    An array containing [title, date, datetime, filename].



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rawfeed/core/utils.rb', line 74

def self.engineer(directory, message, type)
  self.create_directory(directory)
  # abort("Rake aborted: #{directory} directory not found.") unless FileTest.directory?(directory)
  begin
    print "#{message}\n>>> ".blue
    title = STDIN.gets.chomp
  rescue Interrupt
    puts "\n[!] Approached by the user".yellow
    exit!
  end

  slug     = slug_generator(title)
  date     = datetime_generator('%Y-%m-%d')
  datetime = datetime_generator('%Y-%m-%d %R:%S')

  if type == 'page'
    filename = File.join(directory, "#{slug}.#{CONFIG['markdown_extension']}")
  else
    filename = File.join(directory, "#{date}-#{slug}.#{CONFIG['markdown_extension']}")
  end

  if File.exist?(filename)
    answer = confirm("#{filename} already exists. Do you want to overwrite?")
    abort("Action aborted by user!") if answer == 'n'
  end

  [title, date, datetime, filename]
end

.slug_generator(parameter) ⇒ String

Generates a URL-friendly slug from a string. It converts the string to lowercase, strips whitespace, replaces spaces with hyphens, and removes any non-word characters (except hyphens).

Parameters:

  • parameter (String)

    The input string to sluggify.

Returns:

  • (String)

    The generated slug.



36
37
38
# File 'lib/rawfeed/core/utils.rb', line 36

def self.slug_generator(parameter)
  parameter.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
end