Class: Rawfeed::Utils
- Inherits:
-
Object
- Object
- Rawfeed::Utils
- 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
-
.confirm(message) ⇒ String
Prompts the user for a Yes/No confirmation.
-
.create_directory(path) ⇒ void
Safely creates a directory if it does not already exist.
-
.datetime_generator(parameter) ⇒ String
Generates a formatted datetime string.
-
.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.
-
.slug_generator(parameter) ⇒ String
Generates a URL-friendly slug from a string.
Class Method Details
.confirm(message) ⇒ String
Prompts the user for a Yes/No confirmation. Forces the user to input ‘y’ or ‘n’.
57 58 59 60 61 62 63 64 65 |
# File 'lib/rawfeed/core/utils.rb', line 57 def self.confirm() print "#{} [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.
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.
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.
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, , type) self.create_directory(directory) # abort("Rake aborted: #{directory} directory not found.") unless FileTest.directory?(directory) begin print "#{}\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).
36 37 38 |
# File 'lib/rawfeed/core/utils.rb', line 36 def self.slug_generator(parameter) parameter.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '') end |