dolphy 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +20 -0
- data/README.md +98 -0
- data/lib/dolphy/core.rb +70 -0
- data/lib/dolphy/request.rb +19 -0
- data/lib/dolphy/response.rb +16 -0
- data/lib/dolphy/router.rb +36 -0
- data/lib/dolphy/settings.rb +26 -0
- data/lib/dolphy/template_engine.rb +22 -0
- data/lib/dolphy/template_engines/erb_engine.rb +13 -0
- data/lib/dolphy/template_engines/haml_engine.rb +13 -0
- data/lib/dolphy/version.rb +3 -0
- data/lib/dolphy.rb +14 -0
- data/spec/dolphy/core_spec.rb +135 -0
- data/spec/dolphy/request_spec.rb +28 -0
- data/spec/dolphy/response_spec.rb +24 -0
- data/spec/dolphy/router_spec.rb +76 -0
- data/spec/dolphy/settings_spec.rb +35 -0
- data/spec/dolphy/template_engine_spec.rb +30 -0
- data/spec/dolphy_spec.rb +44 -0
- data/spec/spec_helper.rb +52 -0
- metadata +184 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8fbd167c947e2d4bbd1a1b10a985c9c467cd0b0f
|
4
|
+
data.tar.gz: 0a3f510552e4e3a79e5cab2d81831f9aa6098fb1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 08ab403c88a8e3c710bc72dbe0816813c4a0b70d759acb80d4e9c8e3c0bd2430ff6ab78dcfa379f5d0086e8d27cef8c34c6db7fcecbd816dd3d06343439bfa44
|
7
|
+
data.tar.gz: f0e12935f25aa99a629a1d9fb61774445747717bebc84cd2e3972878e34f9a7fb508ea02a068dc6568746d519f8db66080024c9f93cf2cc68cffa306e3d10aba
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 Mathias Jean Johansen <mathias@mjj.io>
|
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 NONINFRINGEMENT.
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# Dolphy
|
2
|
+
[![Build Status](https://magnum.travis-ci.com/majjoha/dolphy.svg?token=G5jfvsY2S8tSb5P53rGq)](https://magnum.travis-ci.com/majjoha/dolphy)
|
3
|
+
[![Code Climate](https://codeclimate.com/repos/53cb2ea469568031fe01ad3f/badges/a7a6a867bc5744c524d7/gpa.png)](https://codeclimate.com/repos/53cb2ea469568031fe01ad3f/feed)
|
4
|
+
|
5
|
+
Dolphy is an incredibly small (~200 LOC) web framework written in Ruby and based
|
6
|
+
on Rack. It was originally mimicking the behavior of Sinatra, but as time has
|
7
|
+
passed, it has slightly changed direction and is now more of a mix between
|
8
|
+
different web frameworks.
|
9
|
+
|
10
|
+
It has solely been written for the purpose of learning and is not meant to be
|
11
|
+
used in production.
|
12
|
+
|
13
|
+
There are a lot of things I want to do to improve the code and functionality of
|
14
|
+
this project. I try to list these things in the
|
15
|
+
[issues](https://github.com/majjoha/dolphy/issues). Feel free to fork this
|
16
|
+
repository and contribute if you want to help me implement features or fixing
|
17
|
+
bugs.
|
18
|
+
|
19
|
+
## Requirements
|
20
|
+
* Ruby 2.1.0 or newer.
|
21
|
+
|
22
|
+
## Getting started
|
23
|
+
Add this line to your Gemfile:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
gem 'dolphy'
|
27
|
+
```
|
28
|
+
|
29
|
+
And then run:
|
30
|
+
|
31
|
+
```
|
32
|
+
bundle
|
33
|
+
```
|
34
|
+
|
35
|
+
Or simply install it yourself as:
|
36
|
+
|
37
|
+
```
|
38
|
+
gem install dolphy
|
39
|
+
```
|
40
|
+
|
41
|
+
## Usage
|
42
|
+
```ruby
|
43
|
+
require 'dolphy'
|
44
|
+
|
45
|
+
Dolphy.app do
|
46
|
+
setup do |app|
|
47
|
+
# It is possible to specify the template engine in the config block. If no
|
48
|
+
# template engine is specified, it will simply default to ERB.
|
49
|
+
app.settings[:template_engine] = Dolphy::TemplateEngines::HamlEngine
|
50
|
+
|
51
|
+
# You can also specify a view path where Dolphy will look for views. It is
|
52
|
+
# by default set to `./views/`.
|
53
|
+
app.settings[:view_path] = "./somewhere/else/views/"
|
54
|
+
|
55
|
+
# Configurations are available to the rest of the application defined in the
|
56
|
+
# router block, so we could for instance define a title. Basically, you can
|
57
|
+
# give the settings hash any key value pair that you might find useful to
|
58
|
+
# store for later use.
|
59
|
+
app.settings[:title] = "Building things with Dolphy"
|
60
|
+
end
|
61
|
+
|
62
|
+
Dolphy.router do
|
63
|
+
get '/' do
|
64
|
+
render :index, { title: settings[:title], body: "Hello!" }
|
65
|
+
end
|
66
|
+
|
67
|
+
get '/login' do
|
68
|
+
render :login
|
69
|
+
end
|
70
|
+
|
71
|
+
post '/post' do
|
72
|
+
render :post, body: "hello #{params["message"]["name"]}"
|
73
|
+
end
|
74
|
+
|
75
|
+
get '/hello/:name' do |name|
|
76
|
+
render :hello, name: name
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end.serve!
|
80
|
+
```
|
81
|
+
|
82
|
+
## Inspiration
|
83
|
+
I've been looking in my directions for inspiration. I probably owe some credit
|
84
|
+
to [Camping](http://camping.io), [Cuba](http://cuba.is),
|
85
|
+
[NYNY](http://alisnic.github.io/nyny/), and obviously
|
86
|
+
[Sinatra](http://sinatrarb.com) and [Rails](http://rubyonrails.org).
|
87
|
+
|
88
|
+
|
89
|
+
## Contribute
|
90
|
+
1. [Fork it](https://github.com/majjoha/dolphy/fork).
|
91
|
+
2. Create your feature branch (`git checkout -b my-new-feature`).
|
92
|
+
3. Commit your changes (`git commit -am 'Add some new feature.'`).
|
93
|
+
4. Push to the branch (`git push origin my-new-feature`).
|
94
|
+
5. Create a new pull request.
|
95
|
+
|
96
|
+
## License
|
97
|
+
See [LICENSE](https://github.com/majjoha/dolphy/blob/master/LICENSE).
|
98
|
+
Copyright (c) 2014 Mathias Jean Johansen <<mathias@mjj.io>>
|
data/lib/dolphy/core.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'dolphy/router'
|
2
|
+
require 'dolphy/request'
|
3
|
+
require 'dolphy/template_engine'
|
4
|
+
require 'dolphy/settings'
|
5
|
+
require 'dolphy/response'
|
6
|
+
require 'forwardable'
|
7
|
+
require 'rack'
|
8
|
+
|
9
|
+
module Dolphy
|
10
|
+
class Core
|
11
|
+
extend Forwardable
|
12
|
+
include Dolphy::TemplateEngines
|
13
|
+
|
14
|
+
delegate Dolphy::Router::HTTP_METHODS => :router
|
15
|
+
|
16
|
+
attr_accessor :settings
|
17
|
+
|
18
|
+
def initialize(&block)
|
19
|
+
@router = Dolphy::Router.new
|
20
|
+
@settings = Dolphy::Settings.new
|
21
|
+
instance_eval(&block)
|
22
|
+
end
|
23
|
+
|
24
|
+
def serve!
|
25
|
+
Rack::Server.start(app: self)
|
26
|
+
end
|
27
|
+
|
28
|
+
def setup(&block)
|
29
|
+
instance_eval(&block)
|
30
|
+
end
|
31
|
+
|
32
|
+
def render(template_name, locals = {})
|
33
|
+
Dolphy::TemplateEngine.new(settings[:template_engine],
|
34
|
+
settings[:view_path]).
|
35
|
+
render(template_name, locals)
|
36
|
+
end
|
37
|
+
|
38
|
+
def redirect_to(path, status = 302)
|
39
|
+
response.headers["Location"] = path
|
40
|
+
response.status = status
|
41
|
+
end
|
42
|
+
|
43
|
+
def params
|
44
|
+
request.params
|
45
|
+
end
|
46
|
+
|
47
|
+
# The main logic of Dolphy nests inside the call(env) method. It looks up
|
48
|
+
# the route for the current request method in the routes hash, and if it
|
49
|
+
# finds a route that matches the current path, it evaluates the block and
|
50
|
+
# sets the response accordingly.
|
51
|
+
def call(env)
|
52
|
+
@request = Dolphy::Request.new(env)
|
53
|
+
@response = Dolphy::Response.new
|
54
|
+
|
55
|
+
router.find_route_for(request).each do |matcher, block|
|
56
|
+
if match = router.find_match_data_for(request, with: matcher)
|
57
|
+
response.body << block.call(*match.captures)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
response.status = 404 if response.body.empty?
|
62
|
+
response.body << "Page not found." if response.body.empty?
|
63
|
+
response.finish
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
attr_accessor :response, :router, :request
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rack'
|
2
|
+
|
3
|
+
module Dolphy
|
4
|
+
class Request < Rack::Request
|
5
|
+
attr_accessor :env
|
6
|
+
|
7
|
+
def initialize(env)
|
8
|
+
@env = env
|
9
|
+
end
|
10
|
+
|
11
|
+
def http_method
|
12
|
+
@env['REQUEST_METHOD'].downcase.to_sym
|
13
|
+
end
|
14
|
+
|
15
|
+
def path
|
16
|
+
@env['PATH_INFO']
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Dolphy
|
2
|
+
class Response
|
3
|
+
attr_accessor :status, :headers, :body
|
4
|
+
|
5
|
+
def initialize(status = 200,
|
6
|
+
headers = {"Content-type" => "text/html"})
|
7
|
+
@status = status
|
8
|
+
@headers = headers
|
9
|
+
@body = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def finish
|
13
|
+
[status, headers, body]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Dolphy
|
2
|
+
class Router
|
3
|
+
attr_accessor :routes
|
4
|
+
|
5
|
+
HTTP_METHODS = %i(get post put delete head options patch trace)
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@routes = Hash.new { |h, k| h[k] = [] }
|
9
|
+
end
|
10
|
+
|
11
|
+
def find_route_for(request)
|
12
|
+
routes[request.http_method]
|
13
|
+
end
|
14
|
+
|
15
|
+
def find_match_data_for(request, with:)
|
16
|
+
trim_trailing_slash(request.path_info).match(with)
|
17
|
+
end
|
18
|
+
|
19
|
+
HTTP_METHODS.each do |verb|
|
20
|
+
define_method(verb) do |path, &block|
|
21
|
+
routes[verb] << [matcher(path), block]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def trim_trailing_slash(string)
|
28
|
+
string.gsub(/\/$/, "")
|
29
|
+
end
|
30
|
+
|
31
|
+
def matcher(path)
|
32
|
+
re = path.gsub(/\:[^\/]+/, "([^\/]+)")
|
33
|
+
%r{\A#{trim_trailing_slash(re)}\z}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Dolphy
|
2
|
+
class Settings
|
3
|
+
attr_accessor :settings
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@settings = settings_defaults
|
7
|
+
end
|
8
|
+
|
9
|
+
def [](element)
|
10
|
+
settings[element]
|
11
|
+
end
|
12
|
+
|
13
|
+
def []=(key, value)
|
14
|
+
settings[key] = value
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def settings_defaults
|
20
|
+
{
|
21
|
+
template_engine: Dolphy::TemplateEngines::ErbEngine,
|
22
|
+
view_path: "./views/"
|
23
|
+
}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'tilt'
|
2
|
+
require_relative 'template_engines/erb_engine'
|
3
|
+
require_relative 'template_engines/haml_engine'
|
4
|
+
|
5
|
+
module Dolphy
|
6
|
+
class TemplateEngine
|
7
|
+
include Dolphy::TemplateEngines
|
8
|
+
|
9
|
+
def initialize(template_engine, view_path)
|
10
|
+
@template_engine = template_engine
|
11
|
+
@view_path = view_path
|
12
|
+
end
|
13
|
+
|
14
|
+
def render(template_name, locals)
|
15
|
+
template_engine.render(template_name, locals, view_path)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
attr_accessor :template_engine, :view_path
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'tilt'
|
2
|
+
|
3
|
+
module Dolphy
|
4
|
+
module TemplateEngines
|
5
|
+
class ErbEngine
|
6
|
+
def self.render(template_name, locals = {}, view_path = "./views/")
|
7
|
+
path = File.expand_path("#{view_path}#{template_name.to_s}.erb", Dir.pwd)
|
8
|
+
template = Tilt::ERBTemplate.new(path)
|
9
|
+
template.render(Object.new, locals)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'tilt'
|
2
|
+
|
3
|
+
module Dolphy
|
4
|
+
module TemplateEngines
|
5
|
+
class HamlEngine
|
6
|
+
def self.render(template_name, locals = {}, view_path = "./views/")
|
7
|
+
path = File.expand_path("#{view_path}/#{template_name.to_s}.haml", Dir.pwd)
|
8
|
+
template = Tilt::HamlTemplate.new(path)
|
9
|
+
template.render(Object.new, locals)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/dolphy.rb
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe Dolphy::Core do
|
4
|
+
let(:core) do
|
5
|
+
Dolphy::Core.new do
|
6
|
+
get '/' do
|
7
|
+
'test'
|
8
|
+
end
|
9
|
+
|
10
|
+
get '/empty' do
|
11
|
+
""
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#initialize" do
|
17
|
+
it "is an instance of Dolphy::Core" do
|
18
|
+
expect(core).to be_a(Dolphy::Core)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#setup" do
|
23
|
+
let(:app) do
|
24
|
+
Dolphy::Core.new do
|
25
|
+
setup do |app|
|
26
|
+
app.settings[:title] = "booyah!"
|
27
|
+
end
|
28
|
+
|
29
|
+
get '/' do
|
30
|
+
'test'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it "configures the app" do
|
36
|
+
expect(app.settings.settings).
|
37
|
+
to eq(
|
38
|
+
{
|
39
|
+
template_engine: Dolphy::TemplateEngines::ErbEngine,
|
40
|
+
view_path: "./views/",
|
41
|
+
title: "booyah!"
|
42
|
+
}
|
43
|
+
)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#render" do
|
48
|
+
let(:app) do
|
49
|
+
Dolphy::Core.new do
|
50
|
+
setup do |app|
|
51
|
+
app.settings[:view_path] = "./spec/views/"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it "renders the template with the locals" do
|
57
|
+
expect(app.render :what, body: "wat").to eq "wat"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#call" do
|
62
|
+
context "when the page exists" do
|
63
|
+
let(:env) do
|
64
|
+
{
|
65
|
+
"REQUEST_METHOD" => "GET",
|
66
|
+
"PATH_INFO" => "/"
|
67
|
+
}
|
68
|
+
end
|
69
|
+
|
70
|
+
it "returns an array with a status, headers and the response" do
|
71
|
+
expect(core.call(env)).to eq(
|
72
|
+
[200, {"Content-type" => "text/html"}, ['test']]
|
73
|
+
)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "when the page doesn't exist" do
|
78
|
+
let(:env) do
|
79
|
+
{
|
80
|
+
"REQUEST_METHOD" => "GET",
|
81
|
+
"PATH_INFO" => "/404"
|
82
|
+
}
|
83
|
+
end
|
84
|
+
|
85
|
+
it "returns an array with a status set to 404, headers and the response
|
86
|
+
set to 'Page not found'" do
|
87
|
+
expect(core.call(env)).to(
|
88
|
+
eq [404, {"Content-type" => "text/html"}, ['Page not found.']]
|
89
|
+
)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context "when the page is an empty string" do
|
94
|
+
let(:env) do
|
95
|
+
{
|
96
|
+
"REQUEST_METHOD" => "GET",
|
97
|
+
"PATH_INFO" => '/empty'
|
98
|
+
}
|
99
|
+
end
|
100
|
+
|
101
|
+
it "returns an empty page with a status set to 200" do
|
102
|
+
expect(core.call(env)).to(
|
103
|
+
eq [200, {"Content-type" => "text/html"}, [""]]
|
104
|
+
)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe "#redirect_to" do
|
110
|
+
let(:app) do
|
111
|
+
Dolphy::Core.new do
|
112
|
+
get '/' do
|
113
|
+
'test'
|
114
|
+
end
|
115
|
+
|
116
|
+
get '/test' do
|
117
|
+
redirect_to '/'
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
let(:env) do
|
123
|
+
{
|
124
|
+
"REQUEST_METHOD" => "GET",
|
125
|
+
"PATH_INFO" => "/test"
|
126
|
+
}
|
127
|
+
end
|
128
|
+
|
129
|
+
it "sets the HTTP status to 302" do
|
130
|
+
expect(app.call(env)).to(
|
131
|
+
eq [302, {"Content-type" => "text/html", "Location" => "/"}, [302]]
|
132
|
+
)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe Dolphy::Request do
|
4
|
+
let(:env) do
|
5
|
+
Dolphy::Request.new({
|
6
|
+
"REQUEST_METHOD" => "GET",
|
7
|
+
"PATH_INFO" => "/"
|
8
|
+
})
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#initialize" do
|
12
|
+
it "is an instance of Dolphy::Request" do
|
13
|
+
expect(env).to be_a(Dolphy::Request)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#http_method" do
|
18
|
+
it "returns the HTTP method as a lowercase symbol" do
|
19
|
+
expect(env.http_method).to eq :get
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#path" do
|
24
|
+
it "returns the path information" do
|
25
|
+
expect(env.path).to eq "/"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require_relative '../../lib/dolphy/response'
|
3
|
+
|
4
|
+
describe Dolphy::Response do
|
5
|
+
let(:response) { Dolphy::Response.new }
|
6
|
+
|
7
|
+
describe "#initialize" do
|
8
|
+
it "initializes a response with a status, headers and an empty body" do
|
9
|
+
expect(response.status).to eq 200
|
10
|
+
expect(response.headers).to eq({"Content-type" => "text/html"})
|
11
|
+
expect(response.body).to eq []
|
12
|
+
end
|
13
|
+
|
14
|
+
it "is an instance of Dolphy::Response" do
|
15
|
+
expect(response).to be_a(Dolphy::Response)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#finish" do
|
20
|
+
it "returns an array consisting of the status, headers and body" do
|
21
|
+
expect(response.finish).to eq [200, {"Content-type" => "text/html"}, []]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe Dolphy::Router do
|
4
|
+
let(:router) { Dolphy::Router.new }
|
5
|
+
|
6
|
+
describe "#initialize" do
|
7
|
+
it "initializes an empty router" do
|
8
|
+
expect(router.routes).to eq({})
|
9
|
+
end
|
10
|
+
|
11
|
+
it "is an instance of Dolphy::Router" do
|
12
|
+
expect(router).to be_a(Dolphy::Router)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#find_route_for" do
|
17
|
+
let(:request) do
|
18
|
+
Dolphy::Request.new({
|
19
|
+
"REQUEST_METHOD" => "GET",
|
20
|
+
"PATH_INFO" => "/"
|
21
|
+
})
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns the block associated with the route" do
|
25
|
+
router.get '/' do
|
26
|
+
'test'
|
27
|
+
end
|
28
|
+
|
29
|
+
expect(router.find_route_for(request).flatten.last.call).to eq "test"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#get" do
|
34
|
+
it "adds a route to the router" do
|
35
|
+
router.get '/' do
|
36
|
+
'test'
|
37
|
+
end
|
38
|
+
|
39
|
+
expect(router.routes[:get].flatten.first).to eq /\A\z/
|
40
|
+
expect(router.routes[:get].flatten.last.call).to eq "test"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#post" do
|
45
|
+
it "adds a route to the router" do
|
46
|
+
router.post '/' do
|
47
|
+
'test'
|
48
|
+
end
|
49
|
+
|
50
|
+
expect(router.routes[:post].flatten.first).to eq /\A\z/
|
51
|
+
expect(router.routes[:post].flatten.last.call).to eq "test"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "#put" do
|
56
|
+
it "adds a route to the router" do
|
57
|
+
router.put '/' do
|
58
|
+
'test'
|
59
|
+
end
|
60
|
+
|
61
|
+
expect(router.routes[:put].flatten.first).to eq /\A\z/
|
62
|
+
expect(router.routes[:put].flatten.last.call).to eq "test"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "#delete" do
|
67
|
+
it "adds a route to the router" do
|
68
|
+
router.delete '/' do
|
69
|
+
'test'
|
70
|
+
end
|
71
|
+
|
72
|
+
expect(router.routes[:delete].flatten.first).to eq /\A\z/
|
73
|
+
expect(router.routes[:delete].flatten.last.call).to eq "test"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe Dolphy::Settings do
|
4
|
+
let(:settings) { Dolphy::Settings.new }
|
5
|
+
|
6
|
+
describe "#initialize" do
|
7
|
+
it "initializes an empty configuration hash" do
|
8
|
+
expect(settings.settings).to eq(
|
9
|
+
{
|
10
|
+
template_engine: Dolphy::TemplateEngines::ErbEngine,
|
11
|
+
view_path: "./views/"
|
12
|
+
}
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "is an instance of Dolphy::Settings" do
|
17
|
+
expect(settings).to be_a(Dolphy::Settings)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#[]" do
|
22
|
+
it "finds the selected element from the settings hash" do
|
23
|
+
expect(
|
24
|
+
settings[:template_engine]
|
25
|
+
).to eq Dolphy::TemplateEngines::ErbEngine
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#[]=" do
|
30
|
+
it "sets the key value pair in the settings hash properly" do
|
31
|
+
settings[:checkout] = 12
|
32
|
+
expect(settings[:checkout]).to be 12
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe Dolphy::TemplateEngine do
|
4
|
+
let(:template_engine) do
|
5
|
+
Dolphy::TemplateEngine.new(
|
6
|
+
Dolphy::TemplateEngines::ErbEngine,
|
7
|
+
"./spec/views/"
|
8
|
+
)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#initialize" do
|
12
|
+
it "is an instance of Dolphy::TemplateEngine" do
|
13
|
+
expect(template_engine).to be_a(Dolphy::TemplateEngine)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#render" do
|
18
|
+
it "renders the page with the locals" do
|
19
|
+
expect(template_engine.render(:what, body: "wat")).to eq "wat"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "renders the page without the locals" do
|
23
|
+
expect(template_engine.render(:not_found, {})).to eq <<-HTML
|
24
|
+
<img src="http://upload.wikimedia.org/wikipedia/en/thumb/8/86/Eric_Dolphy.jpg/220px-Eric_Dolphy.jpg" alt="">
|
25
|
+
<h1>Dolphy says no!</h1>
|
26
|
+
<h2>HTTP 404 Page not found.</h2>
|
27
|
+
HTML
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/dolphy_spec.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Dolphy do
|
4
|
+
include Rack::Test::Methods
|
5
|
+
|
6
|
+
describe "#get" do
|
7
|
+
it "returns a page saying Hello on a correct path" do
|
8
|
+
visit '/'
|
9
|
+
expect(page).to have_content "Hello"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "does not have this route" do
|
13
|
+
visit '/fail'
|
14
|
+
expect(page).to have_content "Page not found."
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns regular strings (without calling render)" do
|
18
|
+
visit '/hello'
|
19
|
+
expect(page).to have_content "hello world!"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "returns a page using the input from the matcher" do
|
23
|
+
visit '/hello/davis'
|
24
|
+
expect(page).to have_content "hello davis"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#post" do
|
29
|
+
it "returns a page saying Hello Mathias" do
|
30
|
+
visit '/'
|
31
|
+
fill_in('message[name]', with: 'Mathias')
|
32
|
+
click_button('Submit')
|
33
|
+
expect(page).to have_content "Hello Mathias"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#redirect_to" do
|
38
|
+
it "redirects to the given path" do
|
39
|
+
visit '/wat'
|
40
|
+
expect(current_path).to eq "/hello"
|
41
|
+
expect(page).to have_content "hello world!"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
require 'rspec'
|
3
|
+
require 'rack/test'
|
4
|
+
require 'capybara'
|
5
|
+
require './lib/dolphy'
|
6
|
+
|
7
|
+
ENV['RACK_ENV'] = 'test'
|
8
|
+
|
9
|
+
app = Dolphy.app do
|
10
|
+
setup do |app|
|
11
|
+
app.settings[:template_engine] = Dolphy::TemplateEngines::HamlEngine
|
12
|
+
app.settings[:view_path] = "./spec/views/"
|
13
|
+
end
|
14
|
+
|
15
|
+
Dolphy.router do
|
16
|
+
get '/' do
|
17
|
+
render :index, { title: "booyah!", body: "Hello" }
|
18
|
+
end
|
19
|
+
|
20
|
+
post '/post' do
|
21
|
+
render :post, { body: "Hello #{params["message"]["name"]}" }
|
22
|
+
end
|
23
|
+
|
24
|
+
get '/hello' do
|
25
|
+
"hello world!"
|
26
|
+
end
|
27
|
+
|
28
|
+
get '/wat' do
|
29
|
+
redirect_to '/hello'
|
30
|
+
end
|
31
|
+
|
32
|
+
get '/hello/:name' do |name|
|
33
|
+
"hello #{name}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
Capybara.javascript_driver = :webkit
|
39
|
+
Capybara.app = app
|
40
|
+
|
41
|
+
module SpecHelper
|
42
|
+
include Rack::Test::Methods
|
43
|
+
|
44
|
+
def app
|
45
|
+
Capybara.app
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
RSpec.configure do |config|
|
50
|
+
config.include SpecHelper
|
51
|
+
config.include Capybara::DSL
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dolphy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mathias Jean Johansen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: tilt
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: haml
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: erubis
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.7'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.7'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 10.3.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 10.3.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.14'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.14'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: capybara
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '2.2'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '2.2'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: capybara-webkit
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.2'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1.2'
|
125
|
+
description: Dolphy is an incredibly small (~200 LOC) web framework.
|
126
|
+
email: mathias@mjj.io
|
127
|
+
executables: []
|
128
|
+
extensions: []
|
129
|
+
extra_rdoc_files: []
|
130
|
+
files:
|
131
|
+
- LICENSE
|
132
|
+
- README.md
|
133
|
+
- lib/dolphy.rb
|
134
|
+
- lib/dolphy/core.rb
|
135
|
+
- lib/dolphy/request.rb
|
136
|
+
- lib/dolphy/response.rb
|
137
|
+
- lib/dolphy/router.rb
|
138
|
+
- lib/dolphy/settings.rb
|
139
|
+
- lib/dolphy/template_engine.rb
|
140
|
+
- lib/dolphy/template_engines/erb_engine.rb
|
141
|
+
- lib/dolphy/template_engines/haml_engine.rb
|
142
|
+
- lib/dolphy/version.rb
|
143
|
+
- spec/dolphy/core_spec.rb
|
144
|
+
- spec/dolphy/request_spec.rb
|
145
|
+
- spec/dolphy/response_spec.rb
|
146
|
+
- spec/dolphy/router_spec.rb
|
147
|
+
- spec/dolphy/settings_spec.rb
|
148
|
+
- spec/dolphy/template_engine_spec.rb
|
149
|
+
- spec/dolphy_spec.rb
|
150
|
+
- spec/spec_helper.rb
|
151
|
+
homepage: https://github.com/majjoha/dolphy
|
152
|
+
licenses:
|
153
|
+
- MIT
|
154
|
+
metadata: {}
|
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: '0'
|
164
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
|
+
requirements:
|
166
|
+
- - ">="
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '0'
|
169
|
+
requirements: []
|
170
|
+
rubyforge_project:
|
171
|
+
rubygems_version: 2.2.0
|
172
|
+
signing_key:
|
173
|
+
specification_version: 4
|
174
|
+
summary: A small and simple web framework.
|
175
|
+
test_files:
|
176
|
+
- spec/dolphy/core_spec.rb
|
177
|
+
- spec/dolphy/request_spec.rb
|
178
|
+
- spec/dolphy/response_spec.rb
|
179
|
+
- spec/dolphy/router_spec.rb
|
180
|
+
- spec/dolphy/settings_spec.rb
|
181
|
+
- spec/dolphy/template_engine_spec.rb
|
182
|
+
- spec/dolphy_spec.rb
|
183
|
+
- spec/spec_helper.rb
|
184
|
+
has_rdoc:
|