lydia 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +71 -1
- data/examples/example2.rb +8 -0
- data/lib/lydia/filters.rb +2 -2
- data/lib/lydia/helpers.rb +1 -1
- data/lib/lydia/response.rb +1 -0
- data/lib/lydia/route.rb +4 -3
- data/lib/lydia/router.rb +7 -7
- data/lib/lydia/version.rb +1 -1
- data/lydia.gemspec +2 -1
- data/spec/application_spec.rb +2 -2
- data/spec/helpers_spec.rb +10 -0
- data/spec/router_spec.rb +10 -5
- data/spec/templates/template.haml +1 -0
- data/spec/templates_spec.rb +13 -3
- metadata +22 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90fa262cf72761e1c2ac13b898504e3216950b23
|
4
|
+
data.tar.gz: 637bc940ba3a7cb2a6fe46594315d63c8c233cd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97f5a3dc0bdcf5baf7d5c3d52ce39b247142bf364d43604f2d0c1c3367aff5f18acd2db943b69b9c916f8a1106c1454bce1afcb2230aa131c2c7d2fdb397860c
|
7
|
+
data.tar.gz: 5b104d388ce7ebd061cd01fbdff890365f8fa39c922955263702b9d930c13be6e13779be281d29c678d49d5428d456c9fecef3e40b642739d7a181d806173086
|
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
[![Build Status](https://travis-ci.org/MirkoMignini/lydia.svg)](https://travis-ci.org/MirkoMignini/lydia)
|
2
2
|
[![Coverage Status](https://coveralls.io/repos/MirkoMignini/lydia/badge.svg?branch=master&service=github)](https://coveralls.io/github/MirkoMignini/lydia?branch=master)
|
3
3
|
[![Code Climate](https://codeclimate.com/github/MirkoMignini/lydia/badges/gpa.svg)](https://codeclimate.com/github/MirkoMignini/lydia)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/lydia.svg)](https://badge.fury.io/rb/lydia)
|
5
|
+
[![Dependency Status](https://gemnasium.com/MirkoMignini/lydia.svg)](https://gemnasium.com/MirkoMignini/lydia)
|
6
|
+
[![security](https://hakiri.io/github/MirkoMignini/lydia/master.svg)](https://hakiri.io/github/MirkoMignini/lydia/master)
|
4
7
|
|
5
8
|
# Lydia
|
6
9
|
|
@@ -40,7 +43,74 @@ The goals of this project are:
|
|
40
43
|
|
41
44
|
## Usage
|
42
45
|
|
43
|
-
|
46
|
+
### First example
|
47
|
+
Create a ruby file, fior example hello_world.rb, require 'lydia' and using the routing functions without creating an application object.
|
48
|
+
|
49
|
+
require 'lydia'
|
50
|
+
|
51
|
+
get '/' do
|
52
|
+
'Hello world!'
|
53
|
+
end
|
54
|
+
|
55
|
+
Just run it to start a webrick server that responds hello world to root.
|
56
|
+
|
57
|
+
$ ruby hello_world.rb
|
58
|
+
|
59
|
+
### Application
|
60
|
+
If preferred it's possible to create an application object and run using rackup command, in this case don't require lydia but lydia/application to avoid the server auto start. For example a minimal config.ru file can be:
|
61
|
+
|
62
|
+
require 'lydia/application'
|
63
|
+
|
64
|
+
class App < Lydia::Application
|
65
|
+
get '/' do
|
66
|
+
'Hello world!'
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
run App.new
|
71
|
+
|
72
|
+
Start the server using rackup command:
|
73
|
+
|
74
|
+
$ rackup
|
75
|
+
|
76
|
+
### Router
|
77
|
+
|
78
|
+
#### Stand alone router
|
79
|
+
If needed the router can be used stand alone, for example if best performances are needed, or used via the application class, slower but with a lot of more features.
|
80
|
+
Stand alone example, note that the return type must be in rack standard format, an array of three that is status, header, body (as array):
|
81
|
+
|
82
|
+
require 'lydia/router'
|
83
|
+
|
84
|
+
class App < Lydia::Router
|
85
|
+
get '/' do
|
86
|
+
body = 'Hellow world!'
|
87
|
+
[200, { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
#### HTTP verbs
|
92
|
+
|
93
|
+
#### Parameters
|
94
|
+
|
95
|
+
#### Wildcards
|
96
|
+
|
97
|
+
#### Named route parameters
|
98
|
+
|
99
|
+
#### Regular expressions
|
100
|
+
|
101
|
+
#### Not found routes and errors
|
102
|
+
|
103
|
+
#### Skip to next route
|
104
|
+
|
105
|
+
#### Halting
|
106
|
+
|
107
|
+
### Return types
|
108
|
+
|
109
|
+
### Filters
|
110
|
+
|
111
|
+
### Templates
|
112
|
+
|
113
|
+
### Helpers
|
44
114
|
|
45
115
|
## Contributing
|
46
116
|
|
data/lib/lydia/filters.rb
CHANGED
@@ -13,13 +13,13 @@ module Lydia
|
|
13
13
|
|
14
14
|
%w(before after).each do |filter|
|
15
15
|
define_method(filter) do |pattern = '/*', options = {}, &block|
|
16
|
-
filters[filter.to_sym] << Route.new(@namespace || '', pattern, options, &block)
|
16
|
+
filters[filter.to_sym] << Route.new(filter, @namespace || '', pattern, options, &block)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
def redirect(pattern, options = {})
|
21
21
|
return ArgumentError.new('Options must contains :to') unless options.include?(:to)
|
22
|
-
filters[:redirect] << Route.new(@namespace || '', pattern, options)
|
22
|
+
filters[:redirect] << Route.new('redirect', @namespace || '', pattern, options)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
data/lib/lydia/helpers.rb
CHANGED
data/lib/lydia/response.rb
CHANGED
data/lib/lydia/route.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
module Lydia
|
2
2
|
class Route
|
3
|
-
attr_reader :regexp, :params, :namespace, :pattern, :options, :block
|
3
|
+
attr_reader :regexp, :params, :request_method, :namespace, :pattern, :options, :block
|
4
4
|
|
5
5
|
WILDCARD_REGEX = /\/\*(.*)/.freeze
|
6
6
|
NAMED_SEGMENTS_REGEX = /\/([^\/]*):([^:$\/]+)/.freeze
|
7
7
|
|
8
|
-
def initialize(namespace, pattern, options = {}, &block)
|
8
|
+
def initialize(request_method, namespace, pattern, options = {}, &block)
|
9
|
+
@request_method = request_method
|
9
10
|
@namespace = namespace
|
10
11
|
@pattern = pattern
|
11
12
|
@options = options
|
@@ -21,7 +22,7 @@ module Lydia
|
|
21
22
|
end
|
22
23
|
@regexp = Regexp.new("\\A#{result}\\z")
|
23
24
|
elsif pattern.is_a?(Regexp)
|
24
|
-
@regexp = pattern
|
25
|
+
@regexp = Regexp.new((namespace || '') + pattern.to_s)
|
25
26
|
else
|
26
27
|
raise ArgumentError.new('Pattern must be a string or a regex')
|
27
28
|
end
|
data/lib/lydia/router.rb
CHANGED
@@ -18,20 +18,16 @@ module Lydia
|
|
18
18
|
|
19
19
|
%w(HEAD GET PATCH PUT POST DELETE OPTIONS).each do |request_method|
|
20
20
|
define_method(request_method.downcase) do |pattern, options = {}, &block|
|
21
|
-
routes[request_method] << Route.new(@namespace, pattern, options, &block)
|
21
|
+
routes[request_method] << Route.new(request_method, @namespace, pattern, options, &block)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
def namespace(pattern,
|
25
|
+
def namespace(pattern, _options = {})
|
26
26
|
prev_namespace = @namespace ||= ''
|
27
27
|
@namespace += pattern
|
28
28
|
yield
|
29
29
|
@namespace = prev_namespace
|
30
30
|
end
|
31
|
-
|
32
|
-
def call(env)
|
33
|
-
new.call(env)
|
34
|
-
end
|
35
31
|
end
|
36
32
|
|
37
33
|
def next_route
|
@@ -71,8 +67,12 @@ module Lydia
|
|
71
67
|
internal_server_error(env, exception)
|
72
68
|
end
|
73
69
|
|
70
|
+
def routes
|
71
|
+
self.class.routes
|
72
|
+
end
|
73
|
+
|
74
74
|
def dispatch(env)
|
75
|
-
|
75
|
+
routes[env['REQUEST_METHOD']].each do |route|
|
76
76
|
if route.match?(env)
|
77
77
|
@request.params.merge!(route.params) if route.params
|
78
78
|
catch(:next_route) do
|
data/lib/lydia/version.rb
CHANGED
data/lydia.gemspec
CHANGED
@@ -25,7 +25,8 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.add_development_dependency 'coveralls'
|
26
26
|
spec.add_development_dependency 'rake'
|
27
27
|
spec.add_development_dependency 'rack-test'
|
28
|
+
spec.add_development_dependency 'haml'
|
28
29
|
|
29
|
-
spec.add_dependency 'rack'
|
30
|
+
spec.add_dependency 'rack', '~> 1.6.4'
|
30
31
|
spec.add_dependency 'tilt'
|
31
32
|
end
|
data/spec/application_spec.rb
CHANGED
@@ -17,7 +17,7 @@ describe "Application" do
|
|
17
17
|
use Rack::Lint
|
18
18
|
|
19
19
|
map '/api' do
|
20
|
-
run API
|
20
|
+
run API.new
|
21
21
|
end
|
22
22
|
|
23
23
|
get '/response' do
|
@@ -33,7 +33,7 @@ describe "Application" do
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def app
|
36
|
-
TestApplication
|
36
|
+
TestApplication.new
|
37
37
|
end
|
38
38
|
|
39
39
|
context 'Composition' do
|
data/spec/helpers_spec.rb
CHANGED
@@ -18,6 +18,10 @@ describe "Helpers" do
|
|
18
18
|
get '/params' do
|
19
19
|
params['key']
|
20
20
|
end
|
21
|
+
|
22
|
+
get '/file' do
|
23
|
+
send_file('test.png')
|
24
|
+
end
|
21
25
|
end
|
22
26
|
|
23
27
|
def app
|
@@ -42,4 +46,10 @@ describe "Helpers" do
|
|
42
46
|
expect(last_response.status).to eq(200)
|
43
47
|
expect(last_response.body).to eq('value')
|
44
48
|
end
|
49
|
+
|
50
|
+
it 'sends a file' do
|
51
|
+
expect {
|
52
|
+
get '/file'
|
53
|
+
}.to raise_error(NotImplementedError)
|
54
|
+
end
|
45
55
|
end
|
data/spec/router_spec.rb
CHANGED
@@ -30,6 +30,10 @@ describe "Router" do
|
|
30
30
|
get '/hello' do
|
31
31
|
get_response('Hello from namespace')
|
32
32
|
end
|
33
|
+
|
34
|
+
get %r{/regular$}i do
|
35
|
+
get_response('Regexp inside namespace')
|
36
|
+
end
|
33
37
|
|
34
38
|
namespace '/nested' do
|
35
39
|
get '/hello' do
|
@@ -180,6 +184,11 @@ describe "Router" do
|
|
180
184
|
expect(last_response.status).to eq(200)
|
181
185
|
expect(last_response.body).to eq('Namespace api version 3')
|
182
186
|
end
|
187
|
+
|
188
|
+
it 'GET /namespace/regular' do
|
189
|
+
get '/namespace/regular'
|
190
|
+
expect(last_response.status).to eq(200)
|
191
|
+
end
|
183
192
|
end
|
184
193
|
|
185
194
|
context 'Wildcards' do
|
@@ -283,11 +292,7 @@ describe "Router" do
|
|
283
292
|
end
|
284
293
|
end
|
285
294
|
|
286
|
-
context 'Class methods' do
|
287
|
-
it 'responds to call' do
|
288
|
-
expect(Lydia::Router).to respond_to(:call)
|
289
|
-
end
|
290
|
-
|
295
|
+
context 'Class methods' do
|
291
296
|
it 'is initialized' do
|
292
297
|
get '/'
|
293
298
|
expect(last_response.status).to eq(200)
|
@@ -0,0 +1 @@
|
|
1
|
+
= message
|
data/spec/templates_spec.rb
CHANGED
@@ -6,9 +6,13 @@ describe "Templates" do
|
|
6
6
|
include Rack::Test::Methods
|
7
7
|
|
8
8
|
class TemplatesHelpers < Lydia::Application
|
9
|
-
get '/
|
9
|
+
get '/render_erb' do
|
10
10
|
render 'spec/templates/template.erb', nil, message: 'template'
|
11
11
|
end
|
12
|
+
|
13
|
+
get '/render_haml' do
|
14
|
+
render 'spec/templates/template.haml', Object.new, message: 'template'
|
15
|
+
end
|
12
16
|
end
|
13
17
|
|
14
18
|
def app
|
@@ -16,8 +20,14 @@ describe "Templates" do
|
|
16
20
|
end
|
17
21
|
|
18
22
|
it 'render an erb template' do
|
19
|
-
get '/
|
23
|
+
get '/render_erb'
|
20
24
|
expect(last_response.status).to eq(200)
|
21
25
|
expect(last_response.body).to include('template')
|
22
26
|
end
|
23
|
-
|
27
|
+
|
28
|
+
it 'render an haml template' do
|
29
|
+
get '/render_haml'
|
30
|
+
expect(last_response.status).to eq(200)
|
31
|
+
expect(last_response.body).to include('template')
|
32
|
+
end
|
33
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lydia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mirko Mignini
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -81,19 +81,33 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: haml
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
|
-
type: :
|
90
|
+
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rack
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.6.4
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.6.4
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: tilt
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -124,6 +138,7 @@ files:
|
|
124
138
|
- Rakefile
|
125
139
|
- bin/console
|
126
140
|
- bin/setup
|
141
|
+
- examples/example2.rb
|
127
142
|
- examples/hello_world.rb
|
128
143
|
- lib/lydia.rb
|
129
144
|
- lib/lydia/application.rb
|
@@ -149,6 +164,7 @@ files:
|
|
149
164
|
- spec/router_spec.rb
|
150
165
|
- spec/spec_helper.rb
|
151
166
|
- spec/templates/template.erb
|
167
|
+
- spec/templates/template.haml
|
152
168
|
- spec/templates_spec.rb
|
153
169
|
homepage: https://github.com/MirkoMignini/lydia
|
154
170
|
licenses:
|
@@ -170,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
170
186
|
version: '0'
|
171
187
|
requirements: []
|
172
188
|
rubyforge_project:
|
173
|
-
rubygems_version: 2.5.
|
189
|
+
rubygems_version: 2.5.1
|
174
190
|
signing_key:
|
175
191
|
specification_version: 4
|
176
192
|
summary: Lightweight, fast and easy to use small ruby web framework.
|
@@ -184,4 +200,5 @@ test_files:
|
|
184
200
|
- spec/router_spec.rb
|
185
201
|
- spec/spec_helper.rb
|
186
202
|
- spec/templates/template.erb
|
203
|
+
- spec/templates/template.haml
|
187
204
|
- spec/templates_spec.rb
|