Module: Bookflow::Utils

Defined in:
lib/bookflow/utils.rb

Class Method Summary collapse

Class Method Details

.format_timestamp(time) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/bookflow/utils.rb', line 22

def format_timestamp(time)
  value =
    case time
    when Time
      time
    else
      Time.parse(time.to_s)
    end
  value.getlocal.strftime("%Y-%m-%d %H:%M:%S %z")
end

.output_filename(title:, asset_id:, used_filenames: {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bookflow/utils.rb', line 33

def output_filename(title:, asset_id:, used_filenames: {})
  base = slugify(title)
  base = slugify(asset_id) if base.empty?
  base = "book" if base.empty?

  first_choice = "#{base}.md"
  return first_choice unless used_filenames.key?(first_choice)

  suffix = short_asset_id(asset_id)
  candidate = "#{base}-#{suffix}.md"
  counter = 2
  while used_filenames.key?(candidate)
    candidate = "#{base}-#{suffix}-#{counter}.md"
    counter += 1
  end
  candidate
end

.short_asset_id(asset_id) ⇒ Object



17
18
19
20
# File 'lib/bookflow/utils.rb', line 17

def short_asset_id(asset_id)
  text = asset_id.to_s
  text[-8, 8] || text
end

.slugify(value) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/bookflow/utils.rb', line 7

def slugify(value)
  ascii = value.to_s.unicode_normalize(:nfkd).encode(
    "ASCII",
    invalid: :replace,
    undef: :replace,
    replace: ""
  )
  ascii.downcase.gsub(/[^a-z0-9]+/, "-").gsub(/\A-+|-+\z/, "")
end