rack_dispatch 0.0.1
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.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +3 -0
- data/lib/rack_dispatch/application.rb +44 -0
- data/lib/rack_dispatch/redirect_handler.rb +19 -0
- data/lib/rack_dispatch/template_handler.rb +28 -0
- data/lib/rack_dispatch/version.rb +3 -0
- data/lib/rack_dispatch.rb +5 -0
- metadata +69 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 15e33270181787eb3d17cea8b09e04c5a88af3c0
|
4
|
+
data.tar.gz: 1db328a2ce599bf593509acff02f55ed11a6461e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 77f8e79cd8582be4a2c76755176432e3ad6d1acd33eafddd5af6b7b52480883e05ca68f795bf50996d0a236c5601dad5305677309085287c95408f7e6f29ba50
|
7
|
+
data.tar.gz: 8de067fec021d7dfd9d22b541c431bb8476201f4ed108d172cb9af43d4c3330c924b8362b13c34912ed9eb18c1142b7d7c69c28ef07d287214ab5a52782f7fbe
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Theodore Kimble
|
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
|
+
module RackDispatch
|
2
|
+
class Application
|
3
|
+
class << self
|
4
|
+
%i[get post put patch delete head options].each do |http_method|
|
5
|
+
define_method(http_method) do |*args|
|
6
|
+
match(http_method, *args)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def default(options = {})
|
11
|
+
@default_handler = handler(options)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
route(Rack::Request.new(env))
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def self.match(method, path, options = {})
|
22
|
+
((@routes ||= {})[[String(method), String(path)]] ||= []) << [options.delete(:with), handler(options)]
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.handler(options = {})
|
26
|
+
redirect, template, to = options.delete(:redirect), options.delete(:template), options.delete(:to)
|
27
|
+
raise ArgumentError, 'must provide :template, :redirect or :to option' unless [redirect, template, to].compact.one?
|
28
|
+
|
29
|
+
if redirect
|
30
|
+
RedirectHandler.new(redirect, options)
|
31
|
+
elsif template
|
32
|
+
TemplateHandler.new(template, options)
|
33
|
+
else
|
34
|
+
to
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def route(request)
|
39
|
+
self.class.instance_variable_get(:@routes)&.[]([request.request_method.downcase, request.path]).find do |constraint, _|
|
40
|
+
constraint.nil? || constraint.call(*constraint.parameters.map(&:last).map { |key| request.params[key.to_s] })
|
41
|
+
end&.last&.call or self.class.instance_variable_get(:@default_handler)&.call
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module RackDispatch
|
2
|
+
class RedirectHandler
|
3
|
+
def initialize(path, status: 302)
|
4
|
+
@path = path
|
5
|
+
@status = status
|
6
|
+
end
|
7
|
+
|
8
|
+
def call
|
9
|
+
[
|
10
|
+
@status,
|
11
|
+
{
|
12
|
+
'Content-Type' => 'text/html',
|
13
|
+
'Location' => @path,
|
14
|
+
},
|
15
|
+
["Redirect to <a href=\"#@path\">#@path</a>"],
|
16
|
+
]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module RackDispatch
|
2
|
+
class TemplateHandler
|
3
|
+
def initialize(path, status: 200, content_type: 'text/html', error: nil)
|
4
|
+
@path = File.expand_path(File.join('templates', path), Dir.pwd)
|
5
|
+
@status = Integer(status)
|
6
|
+
@content_type = String(content_type)
|
7
|
+
@error = error
|
8
|
+
end
|
9
|
+
|
10
|
+
def call
|
11
|
+
[
|
12
|
+
@status,
|
13
|
+
{
|
14
|
+
'Content-Type' => @content_type,
|
15
|
+
},
|
16
|
+
[ERB.new(File.read(@path)).result(OpenStruct.new(template_variables).instance_eval { binding })],
|
17
|
+
]
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def template_variables
|
23
|
+
{
|
24
|
+
error: @error,
|
25
|
+
}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack_dispatch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Theodore Kimble
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
description: |2
|
28
|
+
RackDispatch is a Ruby library that makes it simple to build elegant and
|
29
|
+
powerful web applications with Rack. RackDispatch provides a base class for
|
30
|
+
defining your application topology and handlers to perform common server-
|
31
|
+
side tasks.
|
32
|
+
email:
|
33
|
+
- mail@theodorekimble.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- MIT-LICENSE
|
39
|
+
- README.md
|
40
|
+
- lib/rack_dispatch.rb
|
41
|
+
- lib/rack_dispatch/application.rb
|
42
|
+
- lib/rack_dispatch/redirect_handler.rb
|
43
|
+
- lib/rack_dispatch/template_handler.rb
|
44
|
+
- lib/rack_dispatch/version.rb
|
45
|
+
homepage: https://github.com/theodorekimble/rack_dispatch
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata: {}
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 2.5.1
|
66
|
+
signing_key:
|
67
|
+
specification_version: 4
|
68
|
+
summary: A simple toolkit for building Ruby web application with Rack
|
69
|
+
test_files: []
|