class Msgtrail::Slug

Public Class Methods

bucketnameify(string) click to toggle source

Create S3-safe slug.

S3 bucket names must comply to these naming rules:

  • must be between 3 and 63 characters long

  • must be lowercase a-z

  • may use numbers 0-9

  • may use periods & dashes (and thus not _ or / or )

  • must start with letter or number

  • may not end with dash

  • may not have consecutive periods

  • may not use dashes adjacent to periods (i.e. no .- or -.)

  • may not not be formatted as a IP address

Reference: tinyurl.com/yycoeogm

Note: although periods are allowed this methods rejects them for sake of simplicity

# File lib/msgtrail/slug.rb, line 36
def self.bucketnameify(string)
  #
  # Steps:
  # - change to lowercase
  # - remove periods
  # - truncate to 63 characters
  # - replace invalid characters
  # - condense multiple dashes into one dash
  # - replace leading dashes
  # - replace trailing dashes
  # - pad string with zeros if shorter than 3 characters
  #
  string.downcase
        .gsub(/\.*/, '')
        .slice(0..62)
        .gsub(/[^a-z,0-9,-]/, '-')
        .gsub(/-+/, '-')
        .gsub(/\A-*/, '')
        .gsub(/-*\Z/, '')
        .ljust(3, '0')
end
generate(title, date, time) click to toggle source
# File lib/msgtrail/slug.rb, line 4
def self.generate(title, date, time)
  ymd = date.split(/\D/).map(&:to_i)
  hm = time.split(/\D/).map(&:to_i)
  datestamp = sprintf("%04d%02d%02d-%02d%02d", ymd[0], ymd[1], ymd[2], hm[0], hm[1])
  bucketnameify("#{datestamp}-#{slugify(title)}")
end
slugify(title) click to toggle source

Create URL-safe slug

# File lib/msgtrail/slug.rb, line 12
def self.slugify(title)
  URI.encode_www_form_component(title)
     .gsub(/%[0-9A-F]{2}/, '-')
     .gsub(/-+/, '-')
     .downcase
end