Module: Baptist

Defined in:
lib/baptist.rb,
lib/baptist/version.rb

Overview

A tool for generating unique and well-formed URIs.

Constant Summary collapse

SEPARATOR =
'/'
SPACE =
'-'
MULTIPLIER =
1
ENCODING =
'UTF-8'
VERSION =
"1.2.0"

Class Method Summary collapse

Class Method Details

.bytesize(string) ⇒ Object



115
116
117
# File 'lib/baptist.rb', line 115

def bytesize(string)
  string.bytesize
end

.encode(s, encoding) ⇒ Object



102
103
104
# File 'lib/baptist.rb', line 102

def encode(s, encoding)
  s.to_s.encode(encoding)
end

.escape(s, options = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/baptist.rb', line 85

def escape(s, options = {})
  s = encode(s, options[:encoding])
  regexp = case
    when RUBY_VERSION >= "1.9" && s.encoding === Encoding.find('UTF-8')
      /([^ a-zA-Z0-9_\.\-!~*'()]+)/u
    else
      /([^ a-zA-Z0-9_\.\-!~*'()]+)/n
    end
  s.to_s.gsub(regexp) {
    '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
  }.tr(' ', options[:space])
end

.generate(names = [], options = {}) ⇒ Object

Generates a well-formed and unique URI from an array of strings.

Usage

Baptist.generate                                               # => 'hMAkUyhyqdPkSDWHaUtptQ'
Baptist.generate('Arthur Russell')                             # => 'Arthur-Russell'
Baptist.generate('Arthur Russell', :space => '_')              # => 'Arthur_Russell'
Baptist.generate(['Arthur Russell', 'Calling Out of Context']) # => 'Arthur-Russell/Calling-Out-of-Context'
Baptist.generate(['Rihanna', 'Loud'], :modifier => 'Explicit') # => 'Rihanna/Loud-(Explicit)'

Uniqueness

To guarantee the generated URI is unique:

Baptist.generate('Arthur Russell', :multiplier => '*') do |uri|
  Resource.find_by_uri(uri)
end

Will take the :multiplier character and multiply it for each time the block returns false and add that to the end of the URI. So if there were three name collisions when running the code above the resulting URI would be ‘Arthur-Russell-***’.

The default multiplier is simply the Integer 1, which will result in an incrementing number being added to the end of the URI.

Options

:space - Space character (default: '-')
:separator - Separator character (default: '/')
:modifier - Will add a modifier string in parentheses at the end of the generated URI
:multiplier - The object to multiply with to find a unique URI (default: 1)
:encoding - Force this encoding (default: 'UTF-8')


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/baptist.rb', line 47

def generate(names = [], options = {})
  options = { :space => SPACE,
              :separator => SEPARATOR,
              :multiplier => MULTIPLIER,
              :encoding => ENCODING }.merge(options)

  names = (names.is_a?(Array) ? names : [names]).compact
  names = [generate_name] if names.empty?
  names = names.map do |name|
    escape(name, options)
  end

  uri = names.join(options[:separator]) +
    (options[:modifier] ? "#{options[:space]}(#{escape(options[:modifier], options)})" : '')

  if block_given?
    return uri if yield(uri)
    (1..100).each do |i|
      s = uri + options[:space] + (options[:multiplier] * i).to_s
      if yield(s)
        return s
      end
    end
  end

  uri
end

.generate_nameObject



78
79
80
81
82
# File 'lib/baptist.rb', line 78

def generate_name
  Base64.encode64(
    Digest::MD5.digest("#{Time.now}-#{(0...50).map{ ('a'..'z').to_a[rand(26)] }.join}")
  ).gsub('/','x').gsub('+','y').gsub('=','').strip
end