baptist 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +44 -0
- data/Rakefile +9 -0
- data/baptist.gemspec +24 -0
- data/lib/baptist/version.rb +3 -0
- data/lib/baptist.rb +111 -0
- data/test/baptist_test.rb +33 -0
- data/test/encoding_test.rb +12 -0
- metadata +66 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm 1.9.2@baptist --create
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Niklas Holmgren, Sutajio
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
Generates a well-formed and unique URI from an array of strings.
|
2
|
+
|
3
|
+
Usage
|
4
|
+
-----
|
5
|
+
|
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'
|
9
|
+
|
10
|
+
Percent encoding
|
11
|
+
----------------
|
12
|
+
|
13
|
+
Baptist will percent encode any character (except the delimiter characters explicitly added by Baptist) that is not an unreserved URI character according to RFC3986.
|
14
|
+
|
15
|
+
Uniqueness
|
16
|
+
----------
|
17
|
+
|
18
|
+
To guarantee the generated URI is unique:
|
19
|
+
|
20
|
+
Baptist.generate('Arthur Russell', :multiplier => '*') do |uri|
|
21
|
+
Resource.find_by_uri(uri)
|
22
|
+
end
|
23
|
+
|
24
|
+
Will take the <code>:multiplier</code> character and multiply it for each time
|
25
|
+
the block returns <code>false</code> and add that to the end of the URI. So if
|
26
|
+
there were three name collisions when running the code above the resulting
|
27
|
+
URI would be <code>'Arthur-Russell-\*\*\*'</code>.
|
28
|
+
|
29
|
+
The default multiplier is simply the Integer 1, which will result in an
|
30
|
+
incrementing number being added to the end of the URI.
|
31
|
+
|
32
|
+
Options
|
33
|
+
-------
|
34
|
+
|
35
|
+
* <code>:space</code> - Space character (default: '-')
|
36
|
+
* <code>:separator</code> - Separator character (default: '/')
|
37
|
+
* <code>:multiplier</code> - The object to multiply with to find a unique URI (default: 1)
|
38
|
+
* <code>:encoding</code> - Force this encoding (default: 'UTF-8')
|
39
|
+
|
40
|
+
Author
|
41
|
+
------
|
42
|
+
|
43
|
+
Baptist was created by Niklas Holmgren (niklas@sutajio.se) and released under
|
44
|
+
the MIT license.
|
data/Rakefile
ADDED
data/baptist.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "baptist/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "baptist"
|
7
|
+
s.version = Baptist::VERSION
|
8
|
+
s.authors = ["Niklas Holmgren"]
|
9
|
+
s.email = ["niklas@sutajio.se"]
|
10
|
+
s.homepage = "https://github.com/sutajio/baptist"
|
11
|
+
s.summary = %q{A tool for generating unique and well-formed URIs.}
|
12
|
+
s.description = %q{Baptist creates well-formed relative URIs from a set of strings.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "baptist"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
data/lib/baptist.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
require "baptist/version"
|
2
|
+
|
3
|
+
# A tool for generating unique and well-formed URIs.
|
4
|
+
module Baptist
|
5
|
+
|
6
|
+
SEPARATOR = '/'
|
7
|
+
SPACE = '-'
|
8
|
+
MULTIPLIER = 1
|
9
|
+
ENCODING = 'UTF-8'
|
10
|
+
|
11
|
+
# Generates a well-formed and unique URI from an array of strings.
|
12
|
+
#
|
13
|
+
# === Usage
|
14
|
+
#
|
15
|
+
# Baptist.generate('Arthur Russell') # => 'Arthur-Russell'
|
16
|
+
# Baptist.generate('Arthur Russell', :space => '_') # => 'Arthur_Russell'
|
17
|
+
# Baptist.generate(['Arthur Russell', 'Calling Out of Context']) # => 'Arthur-Russell/Calling-Out-of-Context'
|
18
|
+
#
|
19
|
+
# === Uniqueness
|
20
|
+
#
|
21
|
+
# To guarantee the generated URI is unique:
|
22
|
+
#
|
23
|
+
# Baptist.generate('Arthur Russell', :multiplier => '*') do |uri|
|
24
|
+
# Resource.find_by_uri(uri)
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# Will take the <tt>:multiplier</tt> character and multiply it for each time
|
28
|
+
# the block returns <tt>false</tt> and add that to the end of the URI. So if
|
29
|
+
# there were three name collisions when running the code above the resulting
|
30
|
+
# URI would be 'Arthur-Russell-***'.
|
31
|
+
#
|
32
|
+
# The default multiplier is simply the Integer 1, which will result in an
|
33
|
+
# incrementing number being added to the end of the URI.
|
34
|
+
#
|
35
|
+
# === Options
|
36
|
+
#
|
37
|
+
# :space - Space character (default: '-')
|
38
|
+
# :separator - Separator character (default: '/')
|
39
|
+
# :multiplier - The object to multiply with to find a unique URI (default: 1)
|
40
|
+
# :encoding - Force this encoding (default: 'UTF-8')
|
41
|
+
#
|
42
|
+
def generate(names, options = {})
|
43
|
+
options = { :space => SPACE,
|
44
|
+
:separator => SEPARATOR,
|
45
|
+
:multiplier => MULTIPLIER,
|
46
|
+
:encoding => ENCODING }.merge(options)
|
47
|
+
|
48
|
+
names = names.is_a?(Array) ? names : [names]
|
49
|
+
names = names.map do |name|
|
50
|
+
escape(name, options)
|
51
|
+
end
|
52
|
+
|
53
|
+
uri = names.join(options[:separator]) +
|
54
|
+
(options[:modifier] ? "#{options[:space]}(#{escape(options[:modifier], options)})" : '')
|
55
|
+
|
56
|
+
if block_given?
|
57
|
+
(1..100).each do |i|
|
58
|
+
s = uri + options[:space] + (options[:multiplier] * i).to_s
|
59
|
+
if yield(s)
|
60
|
+
return s
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
uri
|
66
|
+
end
|
67
|
+
module_function :generate
|
68
|
+
|
69
|
+
protected
|
70
|
+
|
71
|
+
def escape(s, options = {})
|
72
|
+
s = encode(s, options[:encoding])
|
73
|
+
regexp = case
|
74
|
+
when RUBY_VERSION >= "1.9" && s.encoding === Encoding.find('UTF-8')
|
75
|
+
/([^ a-zA-Z0-9_.-]+)/u
|
76
|
+
else
|
77
|
+
/([^ a-zA-Z0-9_.-]+)/n
|
78
|
+
end
|
79
|
+
s.to_s.gsub(regexp) {
|
80
|
+
'%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
|
81
|
+
}.tr(' ', options[:space])
|
82
|
+
end
|
83
|
+
module_function :escape
|
84
|
+
|
85
|
+
# Converts string to given encoding; uses String#encode under Ruby 1.9 and
|
86
|
+
# does nothing under 1.8.
|
87
|
+
if ''.respond_to?(:encode)
|
88
|
+
def encode(s, encoding)
|
89
|
+
s.to_s.encode(encoding)
|
90
|
+
end
|
91
|
+
else
|
92
|
+
def encode(s, encoding)
|
93
|
+
s.to_s
|
94
|
+
end
|
95
|
+
end
|
96
|
+
module_function :encode
|
97
|
+
|
98
|
+
# Return the bytesize of String; uses String#size under Ruby 1.8 and
|
99
|
+
# String#bytesize under 1.9.
|
100
|
+
if ''.respond_to?(:bytesize)
|
101
|
+
def bytesize(string)
|
102
|
+
string.bytesize
|
103
|
+
end
|
104
|
+
else
|
105
|
+
def bytesize(string)
|
106
|
+
string.size
|
107
|
+
end
|
108
|
+
end
|
109
|
+
module_function :bytesize
|
110
|
+
|
111
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'baptist'
|
5
|
+
|
6
|
+
class BaptistTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def test_spaces
|
9
|
+
assert_equal 'Arthur-Russell', Baptist.generate('Arthur Russell')
|
10
|
+
assert_equal 'Arthur_Russell', Baptist.generate('Arthur Russell', :space => '_')
|
11
|
+
assert_equal 'Maher-Shalal-Hash-Baz', Baptist.generate('Maher Shalal Hash Baz')
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_multiple_names
|
15
|
+
assert_equal 'Arthur-Russell/Calling-Out-of-Context', Baptist.generate(['Arthur Russell', 'Calling Out of Context'])
|
16
|
+
assert_equal 'Arthur-Russell|Calling-Out-of-Context', Baptist.generate(['Arthur Russell', 'Calling Out of Context'], :separator => '|')
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_strange_characters
|
20
|
+
assert_equal 'Tr%C3%A4d%2C-Gr%C3%A4s-och-Stenar', Baptist.generate('Träd, Gräs och Stenar')
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_modifier
|
24
|
+
assert_equal 'Rihanna/Loud-(Explicit)', Baptist.generate(['Rihanna', 'Loud'], :modifier => 'Explicit')
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_unique
|
28
|
+
assert_equal 'John-Doe-3', Baptist.generate('John Doe') {|uri| uri == 'John-Doe-3' }
|
29
|
+
assert_equal 'John-Doe-1000', Baptist.generate('John Doe', :multiplier => 10) {|uri| uri == 'John-Doe-1000' }
|
30
|
+
assert_equal 'John-Doe-***', Baptist.generate('John Doe', :multiplier => '*') {|uri| uri == 'John-Doe-***' }
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: baptist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Niklas Holmgren
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-12-15 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Baptist creates well-formed relative URIs from a set of strings.
|
17
|
+
email:
|
18
|
+
- niklas@sutajio.se
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- .rvmrc
|
28
|
+
- Gemfile
|
29
|
+
- LICENSE
|
30
|
+
- README.md
|
31
|
+
- Rakefile
|
32
|
+
- baptist.gemspec
|
33
|
+
- lib/baptist.rb
|
34
|
+
- lib/baptist/version.rb
|
35
|
+
- test/baptist_test.rb
|
36
|
+
- test/encoding_test.rb
|
37
|
+
homepage: https://github.com/sutajio/baptist
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project: baptist
|
60
|
+
rubygems_version: 1.8.11
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: A tool for generating unique and well-formed URIs.
|
64
|
+
test_files:
|
65
|
+
- test/baptist_test.rb
|
66
|
+
- test/encoding_test.rb
|