navigable-router 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: 2fcd2c783613974e0cfc498e7fed135d9c24faaac631c5492f1d80d7b800cfb4
4
+ data.tar.gz: 7e5959b84ec2c0508dab57e67c2a869cb92fb85230b2e7933a9ec698f849a1fe
5
+ SHA512:
6
+ metadata.gz: ba294b063321dcb00cdf45d6130ba16debbd827381c74b7d51c48b4227e9a59526ae60642dcbdb6cf410f4983a801fbe1627ed82ad8b040bf5e28845e9306c05
7
+ data.tar.gz: e2b2b309c9b212c68bfb8b5544c9b837d8487b8ef94f00627ac7dd1d78e3034cb49fabaefd7dc6847017400949e103d380a643454eb48ffa9734f44a5fb71518
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .DS_Store
14
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at navigable@firsttry.software. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Alan Ridlehoover and Fito von Zastrow
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # Navigable::Router
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'navigable-router'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install navigable-router
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/navigable-router. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/navigable-router/blob/master/CODE_OF_CONDUCT.md).
32
+
33
+
34
+ ## License
35
+
36
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
37
+
38
+ ## Code of Conduct
39
+
40
+ Everyone interacting in the Navigable::Router project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/navigable-router/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "navigable/router"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mustermann'
4
+
5
+ require 'navigable/router/version'
6
+ require 'navigable/router/trie'
7
+
8
+ module Navigable
9
+ class Router
10
+ GET = 'GET'
11
+ HEAD = 'HEAD'
12
+ POST = 'POST'
13
+ PUT = 'PUT'
14
+ PATCH = 'PATCH'
15
+ DELETE = 'DELETE'
16
+ REQUEST_METHOD = 'REQUEST_METHOD'
17
+ PATH_INFO = 'PATH_INFO'
18
+ ROUTER_PARAMS = 'router.params'
19
+ DYNAMIC_PREFIX = /:/.freeze
20
+ NOT_FOUND_RESPONSE = [404, { 'Content-Length' => '9' }, ['Not Found']].freeze
21
+
22
+ def initialize
23
+ @static = Hash.new { |h, k| h[k] = {} }
24
+ @dynamic = {}
25
+ end
26
+
27
+ def get(path, to:)
28
+ register_route(GET, path, to)
29
+ register_route(HEAD, path, to)
30
+ end
31
+
32
+ def post(path, to:)
33
+ register_route(POST, path, to)
34
+ end
35
+
36
+ def put(path, to:)
37
+ register_route(PUT, path, to)
38
+ end
39
+
40
+ def patch(path, to:)
41
+ register_route(PATCH, path, to)
42
+ end
43
+
44
+ def delete(path, to:)
45
+ register_route(DELETE, path, to)
46
+ end
47
+
48
+ def call(env)
49
+ endpoint = find_endpoint(env[REQUEST_METHOD], env[PATH_INFO]) { |params| env[ROUTER_PARAMS] = params }
50
+ endpoint ? endpoint.call(env) : NOT_FOUND_RESPONSE
51
+ end
52
+
53
+ private
54
+
55
+ def register_route(verb, path, to)
56
+ dynamic?(path) ? register_dynamic_route(verb, path, to) : register_static_route(verb, path, to)
57
+ end
58
+
59
+ def find_endpoint(verb, path, &block)
60
+ find_static(verb, path, &block) || find_dynamic(verb, path, &block)
61
+ end
62
+
63
+ def find_static(verb, path, &block)
64
+ @static[verb][path].tap { |endpoint| endpoint && block.call({}) }
65
+ end
66
+
67
+ def find_dynamic(verb, path, &block)
68
+ dynamic(verb).find_route(path, &block)
69
+ end
70
+
71
+ def dynamic?(path)
72
+ DYNAMIC_PREFIX.match?(path)
73
+ end
74
+
75
+ def dynamic(verb)
76
+ @dynamic[verb] ||= Trie.new
77
+ end
78
+
79
+ def register_dynamic_route(verb, path, to)
80
+ @dynamic[verb] ||= Trie.new
81
+ @dynamic[verb].add_route(path, to)
82
+ end
83
+
84
+ def register_static_route(verb, path, to)
85
+ @static[verb][path] = to
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Navigable
4
+ class Router
5
+ class Node
6
+ MUSTERMANN_VERSION = '5.0'
7
+ DYNAMIC_PREFIX = /:/.freeze
8
+
9
+ attr_reader :endpoint
10
+
11
+ def initialize
12
+ @endpoint = nil
13
+ @static = nil
14
+ @dynamic = nil
15
+ end
16
+
17
+ def add(segment)
18
+ dynamic?(segment) ? add_dynamic(segment) : add_static(segment)
19
+ end
20
+
21
+ def find(segment)
22
+ find_node(segment) { |params| yield(symbolize_keys(params)) if params && block_given? } unless empty?
23
+ end
24
+
25
+ def attach(endpoint)
26
+ @endpoint = endpoint
27
+ end
28
+
29
+ private
30
+
31
+ def add_static(segment)
32
+ @static ||= {}
33
+ @static[segment] ||= self.class.new
34
+ end
35
+
36
+ def add_dynamic(segment)
37
+ @dynamic ||= {}
38
+ @dynamic[dynamic_segment(segment)] ||= self.class.new
39
+ end
40
+
41
+ def find_node(segment, &block)
42
+ find_static(segment, &block) || find_dynamic(segment, &block)
43
+ end
44
+
45
+ def find_static(segment, &block)
46
+ @static ||= {}
47
+ @static[segment].tap { |node| node && block.call({}) }
48
+ end
49
+
50
+ def find_dynamic(segment, &block)
51
+ @dynamic ||= {}
52
+ matcher = @dynamic.keys.find { |matcher| block.call(matcher.match(segment)&.named_captures) }
53
+ matcher && @dynamic[matcher]
54
+ end
55
+
56
+ def empty?
57
+ !@static && !@dynamic
58
+ end
59
+
60
+ def dynamic?(segment)
61
+ DYNAMIC_PREFIX.match?(segment)
62
+ end
63
+
64
+ def dynamic_segment(segment)
65
+ Mustermann.new(segment, type: :rails, version: MUSTERMANN_VERSION)
66
+ end
67
+
68
+ def symbolize_keys(hash)
69
+ hash.each_with_object({}) { |(key, value), obj| obj[key.to_sym] = value }
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'navigable/router/node'
4
+
5
+ module Navigable
6
+ class Router
7
+ class Trie
8
+ PATH_SEPARATOR = '/'
9
+
10
+ def initialize
11
+ @root = Node.new
12
+ end
13
+
14
+ def add_route(path, endpoint)
15
+ traverse(path) { |segment, node| node.add(segment) }.attach(endpoint)
16
+ end
17
+
18
+ def find_route(path)
19
+ route_params = {}
20
+ node = traverse(path) do |segment, node|
21
+ node.find(segment) { |params| route_params.merge!(params) }
22
+ end
23
+
24
+ yield(route_params) if node && block_given?
25
+
26
+ node&.endpoint
27
+ end
28
+
29
+ private
30
+
31
+ def traverse(path)
32
+ _, *segments = path.split(PATH_SEPARATOR)
33
+ segments.reduce(@root) do |node, segment|
34
+ break unless node
35
+ yield(segment, node)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ module Navigable
2
+ class Router
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'lib/navigable/router/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "navigable-router"
5
+ spec.version = Navigable::Router::VERSION
6
+ spec.authors = ["Alan Ridlehoover", "Fito von Zastrow"]
7
+ spec.email = ["navigable@firsttry.software"]
8
+
9
+ spec.summary = %q{Ahoy! Welcome aboard Navigable!}
10
+ spec.description = %q{A simple, highly-performant, Rack-based router.}
11
+ spec.homepage = "https://firsttry.software"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/first-try-software/navigable-router"
17
+ spec.metadata["changelog_uri"] = "https://github.com/first-try-software/navigable-router/issues"
18
+
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|assets)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency 'mustermann', '~> 1.1'
27
+ spec.add_dependency 'mustermann-contrib', '~> 1.1'
28
+
29
+ spec.add_development_dependency 'rack', '~> 2.2'
30
+ spec.add_development_dependency "bundler", "~> 2.0"
31
+ spec.add_development_dependency "rake", "~> 12.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_development_dependency "rspec_junit_formatter", "~>0.4"
34
+ spec.add_development_dependency "simplecov", "~>0.17"
35
+ end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: navigable-router
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alan Ridlehoover
8
+ - Fito von Zastrow
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2020-09-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mustermann
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.1'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.1'
28
+ - !ruby/object:Gem::Dependency
29
+ name: mustermann-contrib
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.1'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.1'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rack
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '2.2'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '2.2'
56
+ - !ruby/object:Gem::Dependency
57
+ name: bundler
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '2.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '2.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '12.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '12.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '3.0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '3.0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rspec_junit_formatter
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '0.4'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '0.4'
112
+ - !ruby/object:Gem::Dependency
113
+ name: simplecov
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: '0.17'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '0.17'
126
+ description: A simple, highly-performant, Rack-based router.
127
+ email:
128
+ - navigable@firsttry.software
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - CODE_OF_CONDUCT.md
137
+ - Gemfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - lib/navigable/router.rb
144
+ - lib/navigable/router/node.rb
145
+ - lib/navigable/router/trie.rb
146
+ - lib/navigable/router/version.rb
147
+ - navigable-router.gemspec
148
+ homepage: https://firsttry.software
149
+ licenses:
150
+ - MIT
151
+ metadata:
152
+ homepage_uri: https://firsttry.software
153
+ source_code_uri: https://github.com/first-try-software/navigable-router
154
+ changelog_uri: https://github.com/first-try-software/navigable-router/issues
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: 2.3.0
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubygems_version: 3.1.2
171
+ signing_key:
172
+ specification_version: 4
173
+ summary: Ahoy! Welcome aboard Navigable!
174
+ test_files: []