parameterised-paths 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 14909f6ce18269c8afd086847a4d0765fd800fd0cf99e81957ccd4032647f431
4
+ data.tar.gz: f8e54b794bc929bef9b5df5d31caf6ee1d4c6bad5c1f10080622c8e0129f3b56
5
+ SHA512:
6
+ metadata.gz: 16d617b261f7f82f8e91433062988d62ecf183131866bebef9dd33062960f9105909bcbb30c2237eb2d10e6f53d57fd3ea87431b05c9a0a06754de5225cc735e
7
+ data.tar.gz: 0d04fd5014a93564c0d566f109b73083728c9656ba9c248bd8f03cbfa12f11e278fabc714b750e6837dd341e5420a79f651e829991a7a954136c28e5c56c1876
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org/>
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Parameterised
4
+ module Paths
5
+ autoload :VERSION, 'parameterised/paths/version'
6
+ autoload :Path, 'parameterised/paths/path'
7
+ autoload :PathMatch, 'parameterised/paths/path_match'
8
+
9
+ # Shorthand method to create a Path object
10
+ # @param path [String]
11
+ # @return [Path]
12
+ def self.path(path)
13
+ Path.new(path)
14
+ end
15
+
16
+ # Attempts to match two paths together
17
+ # @param path [String]
18
+ # @param other_path [String]
19
+ # @return [PathMatch|nil]
20
+ # A PathMatch if a match is made or nil for no match
21
+ def self.path_match(path, other_path)
22
+ Path.new(path).match(other_path)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+
5
+ module Parameterised
6
+ module Paths
7
+ class Path
8
+ # Path constructor
9
+ # @param path [String]
10
+ # A potentially parameterised path
11
+ def initialize(path)
12
+ @original_path = path
13
+ @parameterised = false
14
+ @params = []
15
+ @path = parse_path(path)
16
+ end
17
+
18
+ # Attempts to match a path against our parameterised path
19
+ # @param path [String]
20
+ # The path we want to attempt to match
21
+ # @return [PathMatch|nil]
22
+ # A PathMatch if we have encountered a match or nil when there
23
+ # if no match
24
+ def match(path)
25
+ path = normalise_path(path)
26
+
27
+ unless @path.is_a?(Regexp)
28
+ return @path == path ? PathMatch.new(@original_path) : nil
29
+ end
30
+
31
+ match = path.match(@path)
32
+
33
+ return nil if match.nil?
34
+
35
+ pos = 0
36
+ param = {}
37
+ match.captures.each do |capture|
38
+ capture.sub!('/', '')
39
+ param[@params[pos]] = capture
40
+ pos += 1
41
+ end
42
+
43
+ PathMatch.new(@original_path, param)
44
+ end
45
+
46
+ private
47
+
48
+ # Normalise the provided path removing problematic characters
49
+ # @param path [String]
50
+ # @return [String]
51
+ def normalise_path(path)
52
+ # Remove windows specific separators
53
+ path = path.gsub('\\', '/')
54
+ Regexp.escape(path)
55
+ end
56
+
57
+ # Attempt to parse our given path
58
+ # @param path [String]
59
+ # The path to parse
60
+ # @return [Regexp|String]
61
+ # The original path if there are no parameters, Regexp if is did
62
+ # contain parameters
63
+ def parse_path(path)
64
+ path = normalise_path(path)
65
+
66
+ # if we do not have param sections, just return the string
67
+ return path unless path.include?('/:')
68
+
69
+ path = path.gsub(%r{/:\w+}) do |m|
70
+ m.sub!('/:', '')
71
+ @params.push(m)
72
+ '(/\w+)'
73
+ end
74
+
75
+ Regexp.new("^#{path}$")
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Parameterised
4
+ module Paths
5
+ # Class describing a path match
6
+ class PathMatch
7
+ attr_reader :path, :params
8
+
9
+ # @param path [String]
10
+ # The original path matched against
11
+ # @param params [Hash]
12
+ # Any parameters matched within the path
13
+ def initialize(path, params = {})
14
+ @path = path
15
+ @params = params
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Parameterised
4
+ module Paths
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parameterised-paths
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Birmingham
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: A little gem for matching potentially parameterised paths
56
+ email:
57
+ - chris.birmingham@hotmail.co.uk
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - lib/parameterised/paths.rb
64
+ - lib/parameterised/paths/path.rb
65
+ - lib/parameterised/paths/path_match.rb
66
+ - lib/parameterised/paths/version.rb
67
+ homepage: https://github.com/chrisBirmingham/parameterised-paths
68
+ licenses:
69
+ - Unlicense
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 2.3.0
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.7.6.2
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: A little gem for matching potentially parameterised paths
91
+ test_files: []