baptist 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,11 +1,20 @@
1
1
  Generates a well-formed and unique URI from an array of strings.
2
2
 
3
+ Setup
4
+ -----
5
+
6
+ $ gem install baptist
7
+
8
+ Or add it to your Gemfile.
9
+
3
10
  Usage
4
11
  -----
5
12
 
6
- Baptist.generate('Arthur Russell') => 'Arthur-Russell'
7
- Baptist.generate('Arthur Russell', :space => '_') => 'Arthur_Russell'
8
- Baptist.generate(['Arthur Russell', 'Calling Out of Context']) => 'Arthur-Russell/Calling-Out-of-Context'
13
+ Baptist.generate # => 'hMAkUyhyqdPkSDWHaUtptQ'
14
+ Baptist.generate('Arthur Russell') # => 'Arthur-Russell'
15
+ Baptist.generate('Arthur Russell', :space => '_') # => 'Arthur_Russell'
16
+ Baptist.generate(['Arthur Russell', 'Calling Out of Context']) # => 'Arthur-Russell/Calling-Out-of-Context'
17
+ Baptist.generate(['Rihanna', 'Loud'], :modifier => 'Explicit') # => 'Rihanna/Loud-(Explicit)'
9
18
 
10
19
  Percent encoding
11
20
  ----------------
@@ -34,6 +43,7 @@ Options
34
43
 
35
44
  * <code>:space</code> - Space character (default: '-')
36
45
  * <code>:separator</code> - Separator character (default: '/')
46
+ * <code>:modifier</code> - Will add a modifier string in paranteses at the end of the generated URI
37
47
  * <code>:multiplier</code> - The object to multiply with to find a unique URI (default: 1)
38
48
  * <code>:encoding</code> - Force this encoding (default: 'UTF-8')
39
49
 
data/lib/baptist.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require "baptist/version"
2
+ require "base64"
3
+ require "digest/md5"
2
4
 
3
5
  # A tool for generating unique and well-formed URIs.
4
6
  module Baptist
@@ -12,9 +14,11 @@ module Baptist
12
14
  #
13
15
  # === Usage
14
16
  #
17
+ # Baptist.generate # => 'hMAkUyhyqdPkSDWHaUtptQ'
15
18
  # Baptist.generate('Arthur Russell') # => 'Arthur-Russell'
16
19
  # Baptist.generate('Arthur Russell', :space => '_') # => 'Arthur_Russell'
17
20
  # Baptist.generate(['Arthur Russell', 'Calling Out of Context']) # => 'Arthur-Russell/Calling-Out-of-Context'
21
+ # Baptist.generate(['Rihanna', 'Loud'], :modifier => 'Explicit') # => 'Rihanna/Loud-(Explicit)'
18
22
  #
19
23
  # === Uniqueness
20
24
  #
@@ -36,16 +40,18 @@ module Baptist
36
40
  #
37
41
  # :space - Space character (default: '-')
38
42
  # :separator - Separator character (default: '/')
43
+ # :modifier - Will add a modifier string in paranteses at the end of the generated URI
39
44
  # :multiplier - The object to multiply with to find a unique URI (default: 1)
40
45
  # :encoding - Force this encoding (default: 'UTF-8')
41
46
  #
42
- def generate(names, options = {})
47
+ def generate(names = [], options = {})
43
48
  options = { :space => SPACE,
44
49
  :separator => SEPARATOR,
45
50
  :multiplier => MULTIPLIER,
46
51
  :encoding => ENCODING }.merge(options)
47
52
 
48
- names = names.is_a?(Array) ? names : [names]
53
+ names = (names.is_a?(Array) ? names : [names]).compact
54
+ names = [generate_name] if names.empty?
49
55
  names = names.map do |name|
50
56
  escape(name, options)
51
57
  end
@@ -68,6 +74,13 @@ module Baptist
68
74
 
69
75
  protected
70
76
 
77
+ def generate_name
78
+ Base64.encode64(
79
+ Digest::MD5.digest("#{Time.now}-#{(0...50).map{ ('a'..'z').to_a[rand(26)] }.join}")
80
+ ).gsub('/','x').gsub('+','y').gsub('=','').strip
81
+ end
82
+ module_function :generate_name
83
+
71
84
  def escape(s, options = {})
72
85
  s = encode(s, options[:encoding])
73
86
  regexp = case
@@ -1,3 +1,3 @@
1
1
  module Baptist
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
data/test/baptist_test.rb CHANGED
@@ -14,6 +14,7 @@ class BaptistTest < Test::Unit::TestCase
14
14
  def test_multiple_names
15
15
  assert_equal 'Arthur-Russell/Calling-Out-of-Context', Baptist.generate(['Arthur Russell', 'Calling Out of Context'])
16
16
  assert_equal 'Arthur-Russell|Calling-Out-of-Context', Baptist.generate(['Arthur Russell', 'Calling Out of Context'], :separator => '|')
17
+ assert_equal 'Arthur-Russell/Calling-Out-of-Context', Baptist.generate(['Arthur Russell', nil, 'Calling Out of Context'])
17
18
  end
18
19
 
19
20
  def test_strange_characters
@@ -30,4 +31,9 @@ class BaptistTest < Test::Unit::TestCase
30
31
  assert_equal 'John-Doe-***', Baptist.generate('John Doe', :multiplier => '*') {|uri| uri == 'John-Doe-***' }
31
32
  end
32
33
 
34
+ def test_generate_name
35
+ assert_equal 22, Baptist.generate.size
36
+ assert Baptist.generate != Baptist.generate
37
+ end
38
+
33
39
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: baptist
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.0
5
+ version: 1.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Niklas Holmgren
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-12-15 00:00:00 Z
13
+ date: 2011-12-16 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: Baptist creates well-formed relative URIs from a set of strings.