pretzel 0.1.2
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/.gitignore +34 -0
- data/LICENSE +22 -0
- data/README.md +2 -0
- data/Rakefile +15 -0
- data/lib/pretzel/core.rb +128 -0
- data/lib/pretzel/json.rb +16 -0
- data/lib/pretzel/templating.rb +31 -0
- data/lib/pretzel/version.rb +3 -0
- data/pretzel.gemspec +23 -0
- data/test/assets/test.css +1 -0
- data/test/scrap.rb +28 -0
- data/test/setup_environment.rb +6 -0
- data/test/test_convenience.rb +66 -0
- data/test/test_filters.rb +41 -0
- data/test/test_json.rb +43 -0
- data/test/test_map.rb +51 -0
- data/test/test_middleware.rb +33 -0
- data/test/test_routing.rb +32 -0
- data/test/test_templating.rb +83 -0
- data/test/views/complex/subfolder.liquid +1 -0
- data/test/views/complex/view_with_locals.liquid +1 -0
- data/test/views/complex/view_with_partial.liquid +1 -0
- data/test/views/includes/_partial.liquid +1 -0
- data/test/views/index.liquid +1 -0
- data/test/views/layout.liquid +3 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7d4b91f04b6560e42b18bd3879f18bef72404f1f
|
4
|
+
data.tar.gz: a4fb8cc81e6aaa6c61ba1d2c5459de761fda4bce
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e4e6fcdc2c2602efb6ac107514df1fa0283d46b714bb30674f4fd8d5afbb9299b4f5de5b3248166aa0ce66d25bd18bbecb1dce43581d1e9cf7dd1fac6960aa42
|
7
|
+
data.tar.gz: c76cbfedea4b99b7bea1a64cefe833338f4dc5965b13f36d4ee9c237dd1c15fd4a42b4e1f40b8170ea9b6b0109f0ee23751f64026ad02c566ae8a15775f1e99a
|
data/.gitignore
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/lib/bundler/man/
|
26
|
+
|
27
|
+
# for a library or gem, you might want to ignore these files since the code is
|
28
|
+
# intended to run in multiple environments; otherwise, check them in:
|
29
|
+
# Gemfile.lock
|
30
|
+
# .ruby-version
|
31
|
+
# .ruby-gemset
|
32
|
+
|
33
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
34
|
+
.rvmrc
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Pretzelhands
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "rake/testtask"
|
3
|
+
|
4
|
+
task :test do
|
5
|
+
Dir.chdir("test")
|
6
|
+
end
|
7
|
+
|
8
|
+
Rake::TestTask.new(:test) do |t|
|
9
|
+
t.libs << '../lib'
|
10
|
+
t.libs << '../test'
|
11
|
+
t.test_files = FileList['test_*.rb']
|
12
|
+
t.verbose = false
|
13
|
+
end
|
14
|
+
|
15
|
+
task :default => :test
|
data/lib/pretzel/core.rb
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
require "rack"
|
2
|
+
require "forwardable"
|
3
|
+
require "securerandom"
|
4
|
+
|
5
|
+
require_relative "version"
|
6
|
+
|
7
|
+
module Pretzel
|
8
|
+
class Core
|
9
|
+
attr_reader :request, :response, :parameters
|
10
|
+
class << self
|
11
|
+
extend Forwardable
|
12
|
+
def_delegators :builder, :map, :use
|
13
|
+
|
14
|
+
def token
|
15
|
+
@token ||= SecureRandom.hex(32)
|
16
|
+
end
|
17
|
+
|
18
|
+
def routes
|
19
|
+
@routes ||= Hash.new { |hash, key| hash[key] = [] }
|
20
|
+
end
|
21
|
+
|
22
|
+
def filters
|
23
|
+
@filters ||= Hash.new
|
24
|
+
end
|
25
|
+
|
26
|
+
alias_method :new!, :new
|
27
|
+
def new(*args, &block)
|
28
|
+
builder.run new!(*args, &block)
|
29
|
+
builder
|
30
|
+
end
|
31
|
+
|
32
|
+
%w(get post delete put options head).each do |verb|
|
33
|
+
define_method(verb) do |route, &block|
|
34
|
+
routes[verb.to_sym] << [compile_route(route), block]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
%w(before after).each do |filter|
|
39
|
+
define_method(filter) do |&block|
|
40
|
+
filters[filter.to_sym] ||= Array.new
|
41
|
+
filters[filter.to_sym] << block
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# For all regex lovers.
|
46
|
+
private
|
47
|
+
def compile_route(pattern)
|
48
|
+
keys = []
|
49
|
+
pattern = pattern[0...-1] if pattern.end_with?("/")
|
50
|
+
pattern.gsub!(/(:\w+)/) do # Get all colon-prefixed params
|
51
|
+
keys << $1[1..-1] # Put the name of them into the keys array.
|
52
|
+
"([^/?#]+)" # Substitute with a capture group.
|
53
|
+
end
|
54
|
+
# Return the regex and the param names.
|
55
|
+
if pattern != "/" # No trailing slash for the root route.
|
56
|
+
[%r{^#{pattern}\/?$}, keys]
|
57
|
+
else
|
58
|
+
[%r{^#{pattern}$}, keys]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def builder
|
63
|
+
@builder ||= Rack::Builder.new
|
64
|
+
end
|
65
|
+
end # end self
|
66
|
+
|
67
|
+
def initialize
|
68
|
+
self.class.use Rack::Session::Cookie, secret: self.class.token
|
69
|
+
self.class.use Rack::Static, :urls => ["/assets"]
|
70
|
+
end
|
71
|
+
|
72
|
+
def call(env)
|
73
|
+
dup.call!(env)
|
74
|
+
end
|
75
|
+
|
76
|
+
def call!(env)
|
77
|
+
@request = Rack::Request.new(env)
|
78
|
+
@response = Rack::Response.new
|
79
|
+
@parameters = request.params
|
80
|
+
|
81
|
+
evaluate_route
|
82
|
+
|
83
|
+
@response.finish
|
84
|
+
end
|
85
|
+
|
86
|
+
def halt(*res)
|
87
|
+
response.status = res.detect{|x| x.is_a?(Fixnum) } || 200
|
88
|
+
response.header.merge!(res.detect{|x| x.is_a?(Hash) } || {})
|
89
|
+
response.body = [res.detect{|x| x.is_a?(String) } || ""]
|
90
|
+
throw :halt, response
|
91
|
+
end
|
92
|
+
|
93
|
+
def status(status)
|
94
|
+
response.status = status
|
95
|
+
end
|
96
|
+
|
97
|
+
def session
|
98
|
+
request.session || raise("Rack::Session not set.")
|
99
|
+
end
|
100
|
+
|
101
|
+
def redirect(url)
|
102
|
+
response.redirect url
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
def evaluate_route
|
107
|
+
catch(:halt) do
|
108
|
+
response["Server"] = "Ruby/#{RUBY_VERSION}"
|
109
|
+
response["X-Generated-By"] = "Pretzel/#{Pretzel::VERSION}"
|
110
|
+
|
111
|
+
self.class.routes[request.request_method.downcase.to_sym].each do |route, block|
|
112
|
+
if matching_route = request.path_info.match(route[0])
|
113
|
+
if (route_captures = matching_route.captures) && !route_captures.empty?
|
114
|
+
route_parameters = Hash[*route[1].zip(route_captures).flatten]
|
115
|
+
@parameters = route_parameters.merge(parameters)
|
116
|
+
end
|
117
|
+
|
118
|
+
self.class.filters[:before].each { |filter| instance_exec(&filter) } unless self.class.filters[:before].nil?
|
119
|
+
response.write instance_eval(&block)
|
120
|
+
self.class.filters[:after].each { |filter| instance_exec(&filter) } unless self.class.filters[:after].nil?
|
121
|
+
return
|
122
|
+
end
|
123
|
+
end
|
124
|
+
halt 404, "Not found."
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end # end class
|
128
|
+
end # end module
|
data/lib/pretzel/json.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require "liquid"
|
2
|
+
|
3
|
+
module Pretzel
|
4
|
+
module Extension
|
5
|
+
module Templating
|
6
|
+
def self.included(_class)
|
7
|
+
Liquid::Template.file_system = Liquid::LocalFileSystem.new("#{Dir.pwd}/views/includes")
|
8
|
+
end
|
9
|
+
|
10
|
+
def render(path, locals = {})
|
11
|
+
response["Content-Type"] = "text/html"
|
12
|
+
layout_file = File.expand_path("#{Dir.pwd}/views/layout.liquid")
|
13
|
+
raw_file = File.expand_path("#{Dir.pwd}/views/#{path.to_s}.liquid")
|
14
|
+
|
15
|
+
template = Liquid::Template.parse(File.read(raw_file))
|
16
|
+
|
17
|
+
if File.exist?(layout_file)
|
18
|
+
# Always render the layout, unless explicitly told otherwise.
|
19
|
+
unless locals["layout"] == false
|
20
|
+
layout = Liquid::Template.parse(File.read(layout_file))
|
21
|
+
layout.render({ "template" => template.render(locals) })
|
22
|
+
else
|
23
|
+
template.render(locals)
|
24
|
+
end
|
25
|
+
else
|
26
|
+
template.render(locals)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end # end module
|
30
|
+
end # end module
|
31
|
+
end # end module
|
data/pretzel.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative "lib/pretzel/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "pretzel"
|
5
|
+
s.version = Pretzel::VERSION
|
6
|
+
s.licenses = "MIT"
|
7
|
+
s.summary = "The Pretzel web application core"
|
8
|
+
s.description = "Web development suite based on the ideas of Nancy and Sinatra"
|
9
|
+
s.authors = ["Richard Blechinger"]
|
10
|
+
s.email = ["richard@blechi.at"]
|
11
|
+
s.homepage = "http://github.com/pretzelhands/pretzel"
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
|
17
|
+
s.add_runtime_dependency("rack", "~> 1.6")
|
18
|
+
s.add_runtime_dependency("thin", "~> 1.6")
|
19
|
+
s.add_runtime_dependency("liquid", "~> 3.0")
|
20
|
+
|
21
|
+
s.add_development_dependency("rack-test", "~> 0.6")
|
22
|
+
s.add_development_dependency("minitest", "~> 5.5")
|
23
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
h1 { color: red; }
|
data/test/scrap.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# This file is intended for testing various features as they are
|
2
|
+
# implemented. The code here is not intended to be sensible.
|
3
|
+
|
4
|
+
require_relative "setup_environment"
|
5
|
+
require "pretzel/templating"
|
6
|
+
require "pretzel/json"
|
7
|
+
|
8
|
+
class ScrapApplication < Pretzel::Core
|
9
|
+
include Pretzel::Extension::Templating
|
10
|
+
include Pretzel::Extension::JSON
|
11
|
+
end
|
12
|
+
|
13
|
+
ScrapApplication.get "/" do
|
14
|
+
json({ :hello => "world" }, 201)
|
15
|
+
end
|
16
|
+
|
17
|
+
ScrapApplication.get "/yolo/" do
|
18
|
+
render :index
|
19
|
+
end
|
20
|
+
|
21
|
+
ScrapApplication.get "/test/:name" do
|
22
|
+
render :"complex/view_with_locals", {
|
23
|
+
"name" => parameters["name"]
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
Rack::Handler::Thin.run ScrapApplication.new
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require_relative "setup_environment"
|
2
|
+
|
3
|
+
class ConvenienceApplication < Pretzel::Core; end
|
4
|
+
|
5
|
+
# Testing convenience methods
|
6
|
+
ConvenienceApplication.get "/status" do
|
7
|
+
status 201
|
8
|
+
end
|
9
|
+
|
10
|
+
ConvenienceApplication.get "/redirect" do
|
11
|
+
redirect "/redirected"
|
12
|
+
end
|
13
|
+
|
14
|
+
ConvenienceApplication.get "/redirected" do
|
15
|
+
"Redirected to here"
|
16
|
+
end
|
17
|
+
|
18
|
+
ConvenienceApplication.get "/stop" do
|
19
|
+
halt 500, "Woops"
|
20
|
+
"Not seen"
|
21
|
+
end
|
22
|
+
|
23
|
+
ConvenienceApplication.get "/session/set" do
|
24
|
+
session[:test] = "Test variable"
|
25
|
+
"Session variable set"
|
26
|
+
end
|
27
|
+
|
28
|
+
ConvenienceApplication.get "/session/get" do
|
29
|
+
session[:test] = "Test variable"
|
30
|
+
session[:test]
|
31
|
+
end
|
32
|
+
|
33
|
+
class TestConvenience < Minitest::Test
|
34
|
+
include Rack::Test::Methods
|
35
|
+
|
36
|
+
def app
|
37
|
+
ConvenienceApplication.new
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_status
|
41
|
+
get "/status"
|
42
|
+
assert_equal 201, last_response.status
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_halt
|
46
|
+
get "/stop"
|
47
|
+
assert_equal 500, last_response.status
|
48
|
+
assert_equal "Woops", last_response.body
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_redirect
|
52
|
+
get "/redirect"
|
53
|
+
follow_redirect!
|
54
|
+
assert last_response.body.include? "Redirected"
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_session_setter
|
58
|
+
get "/session/set"
|
59
|
+
assert_equal "Session variable set", last_response.body
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_session_getter
|
63
|
+
get "/session/get"
|
64
|
+
assert_equal "Test variable", last_response.body
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative "setup_environment"
|
2
|
+
|
3
|
+
class FilterApplication < Pretzel::Core; end
|
4
|
+
|
5
|
+
FilterApplication.before do
|
6
|
+
if request.path_info.eql? "/before"
|
7
|
+
response.write "BEFORE::"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
FilterApplication.after do
|
12
|
+
if request.path_info.eql? "/after"
|
13
|
+
response.write "::AFTER"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
FilterApplication.get "/before" do
|
18
|
+
"TEST"
|
19
|
+
end
|
20
|
+
|
21
|
+
FilterApplication.get "/after" do
|
22
|
+
"TEST"
|
23
|
+
end
|
24
|
+
|
25
|
+
class TestFilters < Minitest::Test
|
26
|
+
include Rack::Test::Methods
|
27
|
+
|
28
|
+
def app
|
29
|
+
FilterApplication.new
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_before
|
33
|
+
get "/before"
|
34
|
+
assert_equal "BEFORE::TEST", last_response.body
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_after
|
38
|
+
get "/after"
|
39
|
+
assert_equal "TEST::AFTER", last_response.body
|
40
|
+
end
|
41
|
+
end
|
data/test/test_json.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require_relative "setup_environment"
|
2
|
+
require "pretzel/json"
|
3
|
+
require "rack"
|
4
|
+
|
5
|
+
class JSONApplication < Pretzel::Core
|
6
|
+
include Pretzel::Extension::JSON
|
7
|
+
end
|
8
|
+
|
9
|
+
JSONApplication.get "/" do
|
10
|
+
json({ :hello => "world"})
|
11
|
+
end
|
12
|
+
|
13
|
+
JSONApplication.get "/with-status" do
|
14
|
+
json({ :hello => "status"}, 400)
|
15
|
+
end
|
16
|
+
|
17
|
+
class TestJSON < Minitest::Test
|
18
|
+
include Rack::Test::Methods
|
19
|
+
|
20
|
+
def app
|
21
|
+
JSONApplication.new
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_content_type
|
25
|
+
get "/"
|
26
|
+
assert_equal last_response["Content-Type"], "application/json"
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_status
|
30
|
+
get "/"
|
31
|
+
assert_equal last_response.status, 200
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_serialization
|
35
|
+
get "/"
|
36
|
+
assert last_response.body.eql? %({"hello":"world"})
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_custom_status
|
40
|
+
get "/with-status"
|
41
|
+
assert_equal last_response.status, 400
|
42
|
+
end
|
43
|
+
end
|
data/test/test_map.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require_relative "setup_environment"
|
2
|
+
|
3
|
+
class MapApplication < Pretzel::Core; end
|
4
|
+
class SubApplication < Pretzel::Core; end
|
5
|
+
|
6
|
+
MapApplication.get "/" do
|
7
|
+
"MapApplication main"
|
8
|
+
end
|
9
|
+
|
10
|
+
MapApplication.get "/test" do
|
11
|
+
"MapApplication test"
|
12
|
+
end
|
13
|
+
|
14
|
+
SubApplication.get "/" do
|
15
|
+
"SubApplication main"
|
16
|
+
end
|
17
|
+
|
18
|
+
SubApplication.get "/test" do
|
19
|
+
"SubApplication test"
|
20
|
+
end
|
21
|
+
|
22
|
+
MapApplication.map "/sub" do
|
23
|
+
run SubApplication.new
|
24
|
+
end
|
25
|
+
|
26
|
+
class TestMapping < Minitest::Test
|
27
|
+
include Rack::Test::Methods
|
28
|
+
def app
|
29
|
+
MapApplication.new
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_map_main
|
33
|
+
get "/"
|
34
|
+
assert_equal "MapApplication main", last_response.body
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_map_route
|
38
|
+
get "/test"
|
39
|
+
assert_equal "MapApplication test", last_response.body
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_sub_main
|
43
|
+
get "/sub"
|
44
|
+
assert_equal "SubApplication main", last_response.body
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_sub_route
|
48
|
+
get "/sub/test"
|
49
|
+
assert_equal "SubApplication test", last_response.body
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative "setup_environment"
|
2
|
+
|
3
|
+
class Middleware
|
4
|
+
def initialize(app)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
status, headers, res = @app.call(env)
|
10
|
+
[status, headers, [res.body.first.capitalize]]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class MiddlewareApplication < Pretzel::Core
|
15
|
+
use Middleware
|
16
|
+
end
|
17
|
+
|
18
|
+
MiddlewareApplication.get "/" do
|
19
|
+
"hello"
|
20
|
+
end
|
21
|
+
|
22
|
+
class TestMiddleware < Minitest::Test
|
23
|
+
include Rack::Test::Methods
|
24
|
+
|
25
|
+
def app
|
26
|
+
MiddlewareApplication.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_middleware
|
30
|
+
get "/"
|
31
|
+
assert_equal "Hello", last_response.body
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative "setup_environment"
|
2
|
+
|
3
|
+
class RoutingApplication < Pretzel::Core; end
|
4
|
+
|
5
|
+
# Intense case of DRY below here.
|
6
|
+
# Setting up HTTP methods.
|
7
|
+
%w(GET POST DELETE PUT OPTIONS HEAD).each do |verb|
|
8
|
+
method = RoutingApplication.method(verb.downcase)
|
9
|
+
method.call("/") do
|
10
|
+
"Hello, #{verb}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# GET and POST parameters.
|
15
|
+
%w(GET POST).each do |verb|
|
16
|
+
method = RoutingApplication.method(verb.downcase)
|
17
|
+
method.call("/parameter/:param") do
|
18
|
+
%(Hello, #{parameters["param"]})
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class TestRouting < Minitest::Test
|
23
|
+
%w(GET POST DELETE PUT OPTIONS HEAD).each do |verb|
|
24
|
+
define_method("test_#{verb.downcase}") do
|
25
|
+
req = Rack::MockRequest.new(RoutingApplication.new)
|
26
|
+
req_method = req.method(verb.downcase)
|
27
|
+
res = req_method.call("/")
|
28
|
+
|
29
|
+
assert_equal "Hello, #{verb}", res.body
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require_relative "setup_environment"
|
2
|
+
require "pretzel/templating"
|
3
|
+
require "rack"
|
4
|
+
|
5
|
+
class TemplatingApplication < Pretzel::Core
|
6
|
+
include Pretzel::Extension::Templating
|
7
|
+
end
|
8
|
+
|
9
|
+
TemplatingApplication.get "/" do
|
10
|
+
render :index
|
11
|
+
end
|
12
|
+
|
13
|
+
TemplatingApplication.get "/string" do
|
14
|
+
render "index"
|
15
|
+
end
|
16
|
+
|
17
|
+
TemplatingApplication.get "/subfolder" do
|
18
|
+
render "complex/subfolder"
|
19
|
+
end
|
20
|
+
|
21
|
+
TemplatingApplication.get "/locals" do
|
22
|
+
render "complex/view_with_locals", {
|
23
|
+
"name" => "Richard"
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
TemplatingApplication.get "/partial" do
|
28
|
+
render "complex/view_with_partial"
|
29
|
+
end
|
30
|
+
|
31
|
+
TemplatingApplication.get "/no-layout" do
|
32
|
+
render :index, {
|
33
|
+
"layout" => false
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
class TestTemplating < Minitest::Test
|
38
|
+
include Rack::Test::Methods
|
39
|
+
|
40
|
+
def app
|
41
|
+
TemplatingApplication.new
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_content_type
|
45
|
+
get "/"
|
46
|
+
assert_equal last_response.header["Content-Type"], "text/html"
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_layout
|
50
|
+
get "/"
|
51
|
+
assert last_response.body.include? "layout"
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_with_symbol
|
55
|
+
get "/"
|
56
|
+
assert last_response.body.include? "World!"
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_with_string
|
60
|
+
get "/string"
|
61
|
+
assert last_response.body.include? "World!"
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_from_subfolder
|
65
|
+
get "/subfolder"
|
66
|
+
assert last_response.body.include? "subfolder"
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_with_locals
|
70
|
+
get "/locals"
|
71
|
+
assert last_response.body.include? "Richard"
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_with_partial
|
75
|
+
get "/partial"
|
76
|
+
assert last_response.body.include? "included"
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_no_layout
|
80
|
+
get "/no-layout"
|
81
|
+
assert_equal last_response.body.include?("layout"), false
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<h1>Hello, subfolder!</h1>
|
@@ -0,0 +1 @@
|
|
1
|
+
<h1>Hello, {{ name }}</h1>
|
@@ -0,0 +1 @@
|
|
1
|
+
{% include "partial" %}
|
@@ -0,0 +1 @@
|
|
1
|
+
<h1>Hello, included file!</h1>
|
@@ -0,0 +1 @@
|
|
1
|
+
<h1>Hello, World!</h1>
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pretzel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Richard Blechinger
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-01 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
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: thin
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: liquid
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rack-test
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.6'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.6'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.5'
|
83
|
+
description: Web development suite based on the ideas of Nancy and Sinatra
|
84
|
+
email:
|
85
|
+
- richard@blechi.at
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- LICENSE
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- lib/pretzel/core.rb
|
95
|
+
- lib/pretzel/json.rb
|
96
|
+
- lib/pretzel/templating.rb
|
97
|
+
- lib/pretzel/version.rb
|
98
|
+
- pretzel.gemspec
|
99
|
+
- test/assets/test.css
|
100
|
+
- test/scrap.rb
|
101
|
+
- test/setup_environment.rb
|
102
|
+
- test/test_convenience.rb
|
103
|
+
- test/test_filters.rb
|
104
|
+
- test/test_json.rb
|
105
|
+
- test/test_map.rb
|
106
|
+
- test/test_middleware.rb
|
107
|
+
- test/test_routing.rb
|
108
|
+
- test/test_templating.rb
|
109
|
+
- test/views/complex/subfolder.liquid
|
110
|
+
- test/views/complex/view_with_locals.liquid
|
111
|
+
- test/views/complex/view_with_partial.liquid
|
112
|
+
- test/views/includes/_partial.liquid
|
113
|
+
- test/views/index.liquid
|
114
|
+
- test/views/layout.liquid
|
115
|
+
homepage: http://github.com/pretzelhands/pretzel
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata: {}
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
requirements: []
|
134
|
+
rubyforge_project:
|
135
|
+
rubygems_version: 2.2.2
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: The Pretzel web application core
|
139
|
+
test_files:
|
140
|
+
- test/assets/test.css
|
141
|
+
- test/scrap.rb
|
142
|
+
- test/setup_environment.rb
|
143
|
+
- test/test_convenience.rb
|
144
|
+
- test/test_filters.rb
|
145
|
+
- test/test_json.rb
|
146
|
+
- test/test_map.rb
|
147
|
+
- test/test_middleware.rb
|
148
|
+
- test/test_routing.rb
|
149
|
+
- test/test_templating.rb
|
150
|
+
- test/views/complex/subfolder.liquid
|
151
|
+
- test/views/complex/view_with_locals.liquid
|
152
|
+
- test/views/complex/view_with_partial.liquid
|
153
|
+
- test/views/includes/_partial.liquid
|
154
|
+
- test/views/index.liquid
|
155
|
+
- test/views/layout.liquid
|