uri-urn 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 05fa7804f659ad3fc76e295280e767621645071f
4
+ data.tar.gz: 81eaffaa1a1e5c6f707fcb3903c824e10f1277f5
5
+ SHA512:
6
+ metadata.gz: a42f90f530ad629031815c8c4fea7ec97c7205a99e536a82968074feac84e2d4d199261bf7c4beddb4968ed061d1540fa9a91436177e078760d702491fbae4e1
7
+ data.tar.gz: 7efb31337c70e1b03ba47176278d2ad228ba3fde0b4c3ea921367ddaa75d0851415a1502263142fff07be7b6fd1e3651bb7a5e61ca39e2f3578d6ab853c6b833
data/BSDL ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (C) 2014 KITAITI Makoto. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions
5
+ are met:
6
+ 1. Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ 2. Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22
+ SUCH DAMAGE.
@@ -0,0 +1,7 @@
1
+ URI::URN Changelog
2
+ ==================
3
+
4
+ 0.0.1
5
+ -----
6
+
7
+ * Initial release
data/COPYING ADDED
@@ -0,0 +1,56 @@
1
+ Ruby is copyrighted free software by KITAITI Makoto <KitaitiMakoto@gmail.com>.
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a) distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/README.markdown ADDED
@@ -0,0 +1,67 @@
1
+ URI::URN
2
+ ========
3
+
4
+ This library adds URN scheme support for standard bundled URI library described in [RFC 4122][rfc4122].
5
+
6
+ Installation
7
+ ------------
8
+
9
+ Install it yourself as:
10
+
11
+ $ gem install uri-urn
12
+
13
+ Or add this line to your application's Gemfile:
14
+
15
+ gem 'uri-urn'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Usage
22
+ -----
23
+
24
+ For known Namespace IDs such as UUID, URI parses and returns URNs of them.
25
+
26
+ urn = URI.parse('urn:uuid:01787092-e668-4234-bde9-15444f9c5560')
27
+ urn.class # => URI::URN::UUID
28
+ urn.scheme # => "urn"
29
+ urn.nid # => "uuid"
30
+ urn.nss # => "01787092-e668-4234-bde9-15444f9c5560"
31
+
32
+ For unknown Namespace URN, returns URI::URN::Generic object.
33
+
34
+ URI.parse('urn:dummy:specific-string') # => #<URI::URN::Generic:0x007f3028cc3c10 URL:urn:dummy:specific-string>
35
+
36
+ ### Supported Namespaces
37
+
38
+ Currently supported:
39
+
40
+ * UUID
41
+
42
+ ### Adding Namespaces
43
+
44
+ Define subclass of `URI::URN::Generic` and register it:
45
+
46
+ module URI::URN
47
+ class IETF < Generic
48
+ # subclass definition here...
49
+ end
50
+
51
+ @@nids['IETF'] = IETF
52
+ end
53
+
54
+ Now you can parse IETF URN URI as `URI::URN::IETF`:
55
+
56
+ URI.parse('urn:ietf:rfc:4122') # => #<URI::URN::IETF:0x007f6226e02470 URL:urn:ietf:rfc:4122>
57
+
58
+ Contributing
59
+ ------------
60
+
61
+ 1. Clone it ( https://gitorious.org/uri-ext/uri-urn/clone )
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create a new Merge Request
66
+
67
+ [rfc4122]: http://www.ietf.org/rfc/rfc4122.txt
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'rake/testtask'
2
+ require 'rdoc/task'
3
+ require 'rubygems/tasks'
4
+
5
+ task :default => :test
6
+
7
+ GEMSPEC = Gem::Specification.load(File.join(__dir__, 'uri-urn.gemspec'))
8
+
9
+ Rake::TestTask.new do |task|
10
+ task.test_files = GEMSPEC.test_files
11
+ end
12
+
13
+ RDoc::Task.new do |task|
14
+ task.rdoc_files.include GEMSPEC.require_paths.inject([]) {|files, dir| files + Dir.glob("#{dir}/**/*.rb")}, GEMSPEC.extra_rdoc_files
15
+ task.main = 'README.markdown'
16
+ end
17
+
18
+ Gem::Tasks.new
data/lib/uri/urn.rb ADDED
@@ -0,0 +1,123 @@
1
+ require 'uri/generic'
2
+
3
+ module URI
4
+ module URN
5
+ COMPONENT = [:scheme, :nid, :nss]
6
+
7
+ def self.component
8
+ COMPONENT
9
+ end
10
+
11
+ def self.new(*arg)
12
+ nid = (md = arg[6].match(Generic::URN_REGEXP)) && md['nid']
13
+ @@nids[nid.to_s.upcase].new(*arg)
14
+ end
15
+
16
+ def self.build(args)
17
+ tmp = Util.make_components_hash(self, args)
18
+ @@nids[tmp[:nid].to_s.upcase].build(tmp)
19
+ end
20
+
21
+ class Generic < Generic
22
+ COMPONENT = URN::COMPONENT
23
+
24
+ NID_PATTERN = '[\w\d][\w\d\-]{0,31}'.freeze
25
+ URN_CHARS_PATTERN = "[\\w\\d()+,\\-.:=@;$_!*%/?#]|#{URI::PATTERN::ESCAPED}".freeze
26
+ NSS_PATTERN = "(?:#{URN_CHARS_PATTERN})+".freeze
27
+ URN_REGEXP = Regexp.new("\\A(?<nid>#{NID_PATTERN}):(?<nss>#{NSS_PATTERN})\\z").freeze
28
+
29
+ attr_reader :nid, :nss
30
+
31
+ def self.build(args)
32
+ nid = self.to_s.sub(/\A.*::/, '').downcase
33
+ unless nid == 'generic'
34
+ if args.kind_of?(Array)
35
+ args = [nid] + args
36
+ elsif args.kind_of?(Hash)
37
+ args[:nid] = nid
38
+ end
39
+ end
40
+ tmp = Util.make_components_hash(self, args)
41
+ tmp[:scheme] = 'urn'
42
+ tmp[:opaque] = "%{nid}:%{nss}" % tmp
43
+
44
+ return super(tmp)
45
+ end
46
+
47
+ def initialize(*arg)
48
+ super(*arg)
49
+ if arg[-1]
50
+ self.opaque = @opaque
51
+ end
52
+ end
53
+
54
+ def nid=(value)
55
+ check_nid(value)
56
+ set_nid(value)
57
+ value
58
+ end
59
+
60
+ def nss=(value)
61
+ check_nss(value)
62
+ set_nss(value)
63
+ value
64
+ end
65
+
66
+ def opaque=(value)
67
+ check_opaque(value)
68
+
69
+ if URN_REGEXP =~ value
70
+ self.nid = $~['nid']
71
+ self.nss = $~['nss']
72
+ else
73
+ raise InvalidURIError, "bad URI(nid nor nss not set?): #{self}"
74
+ end
75
+
76
+ set_opaque(value)
77
+ value
78
+ end
79
+
80
+ def normalize!
81
+ super
82
+ set_nid(self.nid.downcase)
83
+ end
84
+
85
+ protected
86
+
87
+ def set_nid(nid)
88
+ @nid = nid
89
+ end
90
+
91
+ def set_nss(nss)
92
+ @nss = nss
93
+ end
94
+
95
+ private
96
+
97
+ def check_nid(value)
98
+ if value !~ /\A#{NID_PATTERN}\z/o
99
+ raise InvalidComponentError, "bad component(expected nid component: #{value})"
100
+ end
101
+
102
+ return true
103
+ end
104
+
105
+ def check_nss(value)
106
+ if value !~ /\A#{self.class::NSS_PATTERN}\z/ # Do not set `o'(optimize) switch. This regepx may be overwritten at subclasses
107
+ raise InvalidComponentError, "bad component(expected nss component: #{value})"
108
+ end
109
+
110
+ return true
111
+ end
112
+ end
113
+
114
+ @@nids = Hash.new(URN::Generic)
115
+ def self.nid_list
116
+ @@nids
117
+ end
118
+ end
119
+
120
+ @@schemes['URN'] = URN
121
+ end
122
+
123
+ require 'uri/urn/uuid'
@@ -0,0 +1,28 @@
1
+ require 'uri/urn'
2
+
3
+ module URI
4
+ module URN
5
+ class UUID < Generic
6
+ NSS_PATTERN = "[#{PATTERN::HEX}]{8}-[#{PATTERN::HEX}]{4}-[#{PATTERN::HEX}]{4}-[#{PATTERN::HEX}]{4}-[#{PATTERN::HEX}]{12}".freeze
7
+
8
+ alias uuid nss
9
+
10
+ #
11
+ # Generate UUID and build URI::URN::UUID with it
12
+ #
13
+ # Currently only version 4 is supported
14
+ #
15
+ def self.generate
16
+ require 'securerandom' unless defined? SecureRandom
17
+ build(nss: SecureRandom.uuid)
18
+ end
19
+
20
+ def normalize!
21
+ super
22
+ set_nss(self.nss.downcase)
23
+ end
24
+ end
25
+
26
+ @@nids['UUID'] = UUID
27
+ end
28
+ end
@@ -0,0 +1,53 @@
1
+ require 'test/unit'
2
+ require 'uri/urn'
3
+
4
+ module URI
5
+
6
+ class TestURN < Test::Unit::TestCase
7
+ def setup
8
+ @uri_string = 'urn:nid:nss'
9
+ @uri = URI.parse(@uri_string)
10
+ end
11
+
12
+ def test_build
13
+ urn = nil
14
+ assert_nothing_raised do
15
+ urn = URI::URN.build(['nid', 'nss'])
16
+ end
17
+ assert_equal 'nid', urn.nid
18
+ assert_equal 'nss', urn.nss
19
+ assert_equal 'nid:nss', urn.opaque
20
+ end
21
+
22
+ def test_parse
23
+ assert_nothing_raised do
24
+ URI.parse @uri_string
25
+ end
26
+ [
27
+ 'urn:',
28
+ 'urn:sp ce'
29
+ ].each do |uri_string|
30
+ assert_raise InvalidURIError do
31
+ URI.parse uri_string
32
+ end
33
+ end
34
+ end
35
+
36
+ def test_component
37
+ assert_equal [:scheme, :nid, :nss], @uri.component
38
+ end
39
+
40
+ def test_components
41
+ assert_equal 'urn', @uri.scheme
42
+ assert_equal 'nid', @uri.nid
43
+ assert_equal 'nss', @uri.nss
44
+ end
45
+
46
+ def test_equal
47
+ assert_equal URI.parse('urn:nid:nss'), URI.parse('URN:NID:nss')
48
+
49
+ assert (URI.parse('urn:nid:nss') == URI.parse('URN:NID:nss'))
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,60 @@
1
+ require 'test/unit'
2
+ require 'uri/urn/uuid'
3
+
4
+ module URI::URN
5
+
6
+ class TestUUID < Test::Unit::TestCase
7
+ def setup
8
+ @uuid_string = '01787092-e668-4234-bde9-15444f9c5560'
9
+ end
10
+
11
+ def test_parse
12
+ uri = URI.parse("urn:uuid:#{@uuid_string}")
13
+ assert_instance_of UUID, uri
14
+ assert_raise URI::InvalidURIError do
15
+ URI.parse 'urn:uuid:'
16
+ end
17
+ assert_raise URI::InvalidComponentError do
18
+ URI.parse 'urn:uuid:hello'
19
+ end
20
+ end
21
+
22
+ def test_build
23
+ uri = nil
24
+ assert_nothing_raised do
25
+ uri = URI::URN::UUID.build([@uuid_string])
26
+ end
27
+ assert_equal 'urn', uri.scheme
28
+ assert_equal 'uuid', uri.nid
29
+ assert_equal @uuid_string, uri.uuid
30
+
31
+ assert_nothing_raised do
32
+ uri = URI::URN::UUID.build(nid: 'dummy', nss: @uuid_string)
33
+ end
34
+ assert_equal 'urn', uri.scheme
35
+ assert_equal 'uuid', uri.nid
36
+ assert_equal @uuid_string, uri.nss
37
+
38
+ assert_raise URI::InvalidComponentError do
39
+ URI::URN::UUID.build(['hello'])
40
+ end
41
+ end
42
+
43
+ def test_equal
44
+ assert (URI.parse("urn:uuid:#{@uuid_string}") == URI.parse("urn:uuid:#{@uuid_string}".upcase))
45
+ end
46
+
47
+ def test_components
48
+ uri = URI.parse("urn:uuid:#{@uuid_string}")
49
+ assert_equal "uuid:#{@uuid_string}", uri.opaque
50
+ end
51
+
52
+ def test_generate
53
+ uri = UUID.generate
54
+ assert_equal 'urn', uri.scheme
55
+ assert_equal 'uuid', uri.nid
56
+ assert_match /\A#{UUID::NSS_PATTERN}\z/, uri.uuid
57
+ end
58
+ end
59
+
60
+ end
data/uri-urn.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "uri-urn"
3
+ spec.version = '0.0.1'
4
+ spec.authors = ["KITAITI Makoto"]
5
+ spec.email = ["KitaitiMakoto@gmail.com"]
6
+ spec.summary = %q{Adds URN Scheme support to URI lib.}
7
+ spec.description = %q{This library adds URN scheme support for standard bundled URI library described in RFC 4122.}
8
+ spec.homepage = 'https://gitorious.org/uri-ext'
9
+ spec.license = "Ruby"
10
+
11
+ spec.test_files = Dir.glob('test/**/test_*.rb')
12
+ spec.require_paths = ["lib"]
13
+ spec.extra_rdoc_files = %w[README.markdown CHANGELOG.markdown COPYING BSDL]
14
+ spec.files = spec.require_paths.inject([]) {|files, dir|
15
+ files + Dir.glob("#{dir}/**/*.rb")
16
+ } +
17
+ spec.test_files +
18
+ spec.extra_rdoc_files +
19
+ %w[uri-urn.gemspec Rakefile]
20
+
21
+ spec.add_development_dependency 'rubygems-tasks'
22
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uri-urn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - KITAITI Makoto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubygems-tasks
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: This library adds URN scheme support for standard bundled URI library
28
+ described in RFC 4122.
29
+ email:
30
+ - KitaitiMakoto@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files:
34
+ - README.markdown
35
+ - CHANGELOG.markdown
36
+ - COPYING
37
+ - BSDL
38
+ files:
39
+ - BSDL
40
+ - CHANGELOG.markdown
41
+ - COPYING
42
+ - README.markdown
43
+ - Rakefile
44
+ - lib/uri/urn.rb
45
+ - lib/uri/urn/uuid.rb
46
+ - test/uri/test_urn.rb
47
+ - test/uri/urn/test_uuid.rb
48
+ - uri-urn.gemspec
49
+ homepage: https://gitorious.org/uri-ext
50
+ licenses:
51
+ - Ruby
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.2.2
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Adds URN Scheme support to URI lib.
73
+ test_files:
74
+ - test/uri/test_urn.rb
75
+ - test/uri/urn/test_uuid.rb