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.1.0"
Class Method Summary collapse
- .bytesize(string) ⇒ Object
- .encode(s, encoding) ⇒ Object
- .escape(s, options = {}) ⇒ Object
-
.generate(names = [], options = {}) ⇒ Object
Generates a well-formed and unique URI from an array of strings.
- .generate_name ⇒ Object
Class Method Details
.bytesize(string) ⇒ Object
114 115 116 |
# File 'lib/baptist.rb', line 114 def bytesize(string) string.bytesize end |
.encode(s, encoding) ⇒ Object
101 102 103 |
# File 'lib/baptist.rb', line 101 def encode(s, encoding) s.to_s.encode(encoding) end |
.escape(s, options = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/baptist.rb', line 84 def escape(s, = {}) s = encode(s, [: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(' ', [: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 paranteses 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 |
# File 'lib/baptist.rb', line 47 def generate(names = [], = {}) = { :space => SPACE, :separator => SEPARATOR, :multiplier => MULTIPLIER, :encoding => ENCODING }.merge() names = (names.is_a?(Array) ? names : [names]).compact names = [generate_name] if names.empty? names = names.map do |name| escape(name, ) end uri = names.join([:separator]) + ([:modifier] ? "#{[:space]}(#{escape([:modifier], )})" : '') if block_given? (1..100).each do |i| s = uri + [:space] + ([:multiplier] * i).to_s if yield(s) return s end end end uri end |
.generate_name ⇒ Object
77 78 79 80 81 |
# File 'lib/baptist.rb', line 77 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 |