lydia 0.1.3 → 0.1.4
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 +4 -4
- data/.rubocop.yml +61 -0
- data/.travis.yml +3 -8
- data/README.md +93 -22
- data/Rakefile +3 -3
- data/bin/console +3 -3
- data/examples/{example2.rb → config.ru} +2 -0
- data/examples/hello_world.rb +1 -1
- data/lib/lydia.rb +2 -2
- data/lib/lydia/application.rb +8 -11
- data/lib/lydia/delegator.rb +2 -2
- data/lib/lydia/filters.rb +11 -12
- data/lib/lydia/halted.rb +1 -1
- data/lib/lydia/helpers.rb +5 -5
- data/lib/lydia/not_found.rb +1 -1
- data/lib/lydia/request.rb +2 -2
- data/lib/lydia/response.rb +37 -25
- data/lib/lydia/route.rb +30 -22
- data/lib/lydia/router.rb +29 -30
- data/lib/lydia/standard_pages.rb +10 -7
- data/lib/lydia/version.rb +1 -1
- data/lydia.gemspec +9 -5
- data/spec/application_spec.rb +16 -16
- data/spec/delegator_spec.rb +5 -9
- data/spec/filters_spec.rb +16 -18
- data/spec/helpers_spec.rb +14 -14
- data/spec/middleware_spec.rb +9 -9
- data/spec/response_spec.rb +36 -38
- data/spec/router_spec.rb +18 -19
- data/spec/spec_helper.rb +1 -1
- data/spec/templates_spec.rb +5 -5
- metadata +35 -6
data/spec/delegator_spec.rb
CHANGED
@@ -6,26 +6,22 @@ extend Lydia::Delegator
|
|
6
6
|
|
7
7
|
describe 'Delegator' do
|
8
8
|
include Rack::Test::Methods
|
9
|
-
|
9
|
+
|
10
10
|
class TestDelegator
|
11
11
|
extend Lydia::Delegator
|
12
|
-
|
12
|
+
|
13
13
|
get '/hello' do
|
14
14
|
'Hello world!'
|
15
15
|
end
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
def app
|
19
19
|
Lydia::Application.new
|
20
20
|
end
|
21
|
-
|
22
|
-
it 'Delegator initialize correctly' do
|
23
|
-
expect(1).to eq(1)
|
24
|
-
end
|
25
|
-
|
21
|
+
|
26
22
|
it 'Delegates to Application' do
|
27
23
|
get '/hello'
|
28
24
|
expect(last_response.status).to eq(200)
|
29
25
|
expect(last_response.body).to eq('Hello world!')
|
30
26
|
end
|
31
|
-
end
|
27
|
+
end
|
data/spec/filters_spec.rb
CHANGED
@@ -2,50 +2,49 @@ require 'spec_helper'
|
|
2
2
|
require 'rack/test'
|
3
3
|
require 'lydia/application'
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe 'Filters' do
|
6
6
|
include Rack::Test::Methods
|
7
7
|
|
8
|
-
class TestFilters < Lydia::Application
|
9
|
-
|
8
|
+
class TestFilters < Lydia::Application
|
10
9
|
before do
|
11
10
|
@filter = 'before'
|
12
11
|
end
|
13
|
-
|
12
|
+
|
14
13
|
after do
|
15
14
|
@filter = 'after'
|
16
15
|
end
|
17
|
-
|
16
|
+
|
18
17
|
get '/filter' do
|
19
18
|
@filter
|
20
19
|
end
|
21
|
-
|
20
|
+
|
22
21
|
redirect '/redirect', to: '/redirected'
|
23
|
-
|
22
|
+
|
24
23
|
get '/redirected' do
|
25
24
|
'redirected'
|
26
25
|
end
|
27
|
-
|
26
|
+
|
28
27
|
namespace '/namespace' do
|
29
28
|
before do
|
30
29
|
@filter_nested = 'before'
|
31
30
|
end
|
32
|
-
|
31
|
+
|
33
32
|
after do
|
34
33
|
@filter_nested = 'after'
|
35
34
|
end
|
36
|
-
|
35
|
+
|
37
36
|
redirect '/redirect', to: '/redirected'
|
38
|
-
|
37
|
+
|
39
38
|
get '/redirected' do
|
40
39
|
'redirected in namespace'
|
41
40
|
end
|
42
|
-
|
41
|
+
|
43
42
|
get '/filter' do
|
44
43
|
"#{@filter} #{@filter_nested}"
|
45
44
|
end
|
46
45
|
end
|
47
46
|
end
|
48
|
-
|
47
|
+
|
49
48
|
def app
|
50
49
|
TestFilters.new
|
51
50
|
end
|
@@ -64,10 +63,10 @@ describe "Filters" do
|
|
64
63
|
get '/namespace/filter'
|
65
64
|
expect(last_response.status).to eq(200)
|
66
65
|
expect(last_response.body).to eq('before before')
|
67
|
-
end
|
66
|
+
end
|
68
67
|
end
|
69
68
|
end
|
70
|
-
|
69
|
+
|
71
70
|
context 'Redirect' do
|
72
71
|
context 'Root' do
|
73
72
|
it 'redirects' do
|
@@ -76,7 +75,7 @@ describe "Filters" do
|
|
76
75
|
expect(last_response.body).to eq('redirected')
|
77
76
|
end
|
78
77
|
end
|
79
|
-
|
78
|
+
|
80
79
|
context 'Namespace' do
|
81
80
|
it 'redirects' do
|
82
81
|
get '/namespace/redirect'
|
@@ -85,5 +84,4 @@ describe "Filters" do
|
|
85
84
|
end
|
86
85
|
end
|
87
86
|
end
|
88
|
-
|
89
|
-
end
|
87
|
+
end
|
data/spec/helpers_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
require 'rack/test'
|
3
3
|
require 'lydia/application'
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe 'Helpers' do
|
6
6
|
include Rack::Test::Methods
|
7
7
|
|
8
8
|
class TestHelpers < Lydia::Application
|
@@ -10,24 +10,24 @@ describe "Helpers" do
|
|
10
10
|
content_type 'application/json'
|
11
11
|
'body'
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
get '/redirect' do
|
15
15
|
redirect '/new_url'
|
16
|
-
end
|
17
|
-
|
16
|
+
end
|
17
|
+
|
18
18
|
get '/params' do
|
19
19
|
params['key']
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
get '/file' do
|
23
23
|
send_file('test.png')
|
24
24
|
end
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
def app
|
28
28
|
TestHelpers.new
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
it 'responds to content_type' do
|
32
32
|
get '/content_type'
|
33
33
|
expect(last_response.status).to eq(200)
|
@@ -39,17 +39,17 @@ describe "Helpers" do
|
|
39
39
|
get '/redirect'
|
40
40
|
expect(last_response.status).to eq(302)
|
41
41
|
expect(last_response.body).to eq('/new_url')
|
42
|
-
end
|
43
|
-
|
42
|
+
end
|
43
|
+
|
44
44
|
it 'responds to params' do
|
45
|
-
get '/params',
|
45
|
+
get '/params', key: 'value'
|
46
46
|
expect(last_response.status).to eq(200)
|
47
47
|
expect(last_response.body).to eq('value')
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
it 'sends a file' do
|
51
|
-
expect
|
51
|
+
expect do
|
52
52
|
get '/file'
|
53
|
-
|
53
|
+
end.to raise_error(NotImplementedError)
|
54
54
|
end
|
55
|
-
end
|
55
|
+
end
|
data/spec/middleware_spec.rb
CHANGED
@@ -4,7 +4,7 @@ require 'erb'
|
|
4
4
|
require 'json'
|
5
5
|
require 'lydia/application'
|
6
6
|
|
7
|
-
describe
|
7
|
+
describe 'Middleware' do
|
8
8
|
include Rack::Test::Methods
|
9
9
|
|
10
10
|
class UpcaseMiddleware
|
@@ -13,24 +13,24 @@ describe "Middleware" do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def call(env)
|
16
|
-
status, headers, body
|
17
|
-
upcased_body = body.map
|
16
|
+
status, headers, body = @app.call(env)
|
17
|
+
upcased_body = body.map(&:upcase)
|
18
18
|
[status, headers, upcased_body]
|
19
19
|
end
|
20
|
-
end
|
21
|
-
|
20
|
+
end
|
21
|
+
|
22
22
|
class TestMiddleware < Lydia::Application
|
23
23
|
use UpcaseMiddleware
|
24
|
-
|
24
|
+
|
25
25
|
get '/hello' do
|
26
26
|
'Hello world!'
|
27
27
|
end
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
def app
|
31
31
|
TestMiddleware.new
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
context 'Middleware stack' do
|
35
35
|
it 'is correctly handled' do
|
36
36
|
get '/hello'
|
@@ -38,4 +38,4 @@ describe "Middleware" do
|
|
38
38
|
expect(last_response.body).to eq('HELLO WORLD!')
|
39
39
|
end
|
40
40
|
end
|
41
|
-
end
|
41
|
+
end
|
data/spec/response_spec.rb
CHANGED
@@ -2,27 +2,27 @@ require 'spec_helper'
|
|
2
2
|
require 'lydia/response'
|
3
3
|
|
4
4
|
describe 'Response' do
|
5
|
-
let
|
6
|
-
|
7
|
-
context 'initialization' do
|
5
|
+
let(:response) { Lydia::Response.new }
|
6
|
+
|
7
|
+
context 'initialization' do
|
8
8
|
it 'is created' do
|
9
|
-
expect(response).
|
9
|
+
expect(response).not_to be_nil
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
it 'contains default content type' do
|
13
13
|
expect(response.headers).to include('Content-Type' => 'text/html')
|
14
14
|
end
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
context 'build' do
|
18
18
|
it 'responds to build' do
|
19
19
|
expect(response).to respond_to(:build)
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
it 'builds using a string (body)' do
|
23
23
|
body = 'Hello world!'
|
24
24
|
result = response.build(body)
|
25
|
-
expect(result).
|
25
|
+
expect(result).not_to be_nil
|
26
26
|
expect(result).to be_an(Array)
|
27
27
|
expect(result[0]).to eq(200)
|
28
28
|
expect(result[1]).to include('Content-Type' => 'text/html')
|
@@ -30,35 +30,35 @@ describe 'Response' do
|
|
30
30
|
expect(result[2]).to be_an(Array)
|
31
31
|
expect(result[2][0]).to eq(body)
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
it 'builds using a fixnum (status)' do
|
35
35
|
status = 204
|
36
36
|
result = response.build(status)
|
37
|
-
expect(result).
|
37
|
+
expect(result).not_to be_nil
|
38
38
|
expect(result).to be_an(Array)
|
39
39
|
expect(result[0]).to eq(204)
|
40
|
-
expect(result[1]).
|
41
|
-
expect(result[1]).
|
40
|
+
expect(result[1]).not_to include('Content-Type')
|
41
|
+
expect(result[1]).not_to include('Content-Length')
|
42
42
|
expect(result[2]).to be_an(Array)
|
43
43
|
expect(result[2][0]).to be_nil
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
it 'builds using a hash (body)' do
|
47
|
-
body = {hello: 'world'}
|
47
|
+
body = { hello: 'world' }
|
48
48
|
result = response.build(body)
|
49
|
-
expect(result).
|
49
|
+
expect(result).not_to be_nil
|
50
50
|
expect(result).to be_an(Array)
|
51
51
|
expect(result[0]).to eq(200)
|
52
52
|
expect(result[1]).to include('Content-Type' => 'application/json')
|
53
53
|
expect(result[1]).to include('Content-Length' => body.to_json.length.to_s)
|
54
54
|
expect(result[2]).to be_an(Array)
|
55
55
|
expect(result[2][0]).to eq(body.to_json)
|
56
|
-
end
|
57
|
-
|
56
|
+
end
|
57
|
+
|
58
58
|
it 'builds using an array of two (body is array)' do
|
59
59
|
body = [201, ['Body']]
|
60
60
|
result = response.build(body)
|
61
|
-
expect(result).
|
61
|
+
expect(result).not_to be_nil
|
62
62
|
expect(result).to be_an(Array)
|
63
63
|
expect(result[0]).to eq(201)
|
64
64
|
expect(result[1]).to include('Content-Type' => 'text/html')
|
@@ -66,23 +66,23 @@ describe 'Response' do
|
|
66
66
|
expect(result[2]).to be_an(Array)
|
67
67
|
expect(result[2][0]).to eq(body[1][0])
|
68
68
|
end
|
69
|
-
|
70
|
-
it 'builds using an array of two (body is
|
69
|
+
|
70
|
+
it 'builds using an array of two (body is not an array)' do
|
71
71
|
body = [201, 'Body']
|
72
72
|
result = response.build(body)
|
73
|
-
expect(result).
|
73
|
+
expect(result).not_to be_nil
|
74
74
|
expect(result).to be_an(Array)
|
75
75
|
expect(result[0]).to eq(201)
|
76
76
|
expect(result[1]).to include('Content-Type' => 'text/html')
|
77
77
|
expect(result[1]).to include('Content-Length' => body[1].length.to_s)
|
78
78
|
expect(result[2]).to be_an(Array)
|
79
79
|
expect(result[2][0]).to eq(body[1])
|
80
|
-
end
|
81
|
-
|
80
|
+
end
|
81
|
+
|
82
82
|
it 'builds using an array of three' do
|
83
|
-
body = [201, {'Authentication' => '12345'}, 'Body']
|
83
|
+
body = [201, { 'Authentication' => '12345' }, 'Body']
|
84
84
|
result = response.build(body)
|
85
|
-
expect(result).
|
85
|
+
expect(result).not_to be_nil
|
86
86
|
expect(result).to be_an(Array)
|
87
87
|
expect(result[0]).to eq(201)
|
88
88
|
expect(result[1]).to include('Content-Type' => 'text/html')
|
@@ -90,39 +90,37 @@ describe 'Response' do
|
|
90
90
|
expect(result[1]).to include('Authentication' => '12345')
|
91
91
|
expect(result[2]).to be_an(Array)
|
92
92
|
expect(result[2][0]).to eq(body[2])
|
93
|
-
end
|
94
|
-
|
93
|
+
end
|
94
|
+
|
95
95
|
class Stream
|
96
96
|
def each
|
97
|
-
10.times { |i| yield
|
97
|
+
10.times { |i| yield i.to_s }
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
101
|
context 'Stream' do
|
102
|
-
|
103
102
|
it 'builds a valid stream object' do
|
104
103
|
stream = ''
|
105
|
-
Stream.new.each{ |i| stream += i }
|
104
|
+
Stream.new.each { |i| stream += i }
|
106
105
|
expect(stream).to eq('0123456789')
|
107
106
|
end
|
108
|
-
|
107
|
+
|
109
108
|
it 'builds using an object that responds to :each' do
|
110
109
|
result = response.build(Stream.new)
|
111
|
-
expect(result).
|
110
|
+
expect(result).not_to be_nil
|
112
111
|
expect(result).to be_an(Array)
|
113
112
|
expect(result[0]).to eq(200)
|
114
113
|
expect(result[1]).to include('Content-Type' => 'text/html')
|
115
114
|
expect(result[1]).to include('Content-Length')
|
116
115
|
expect(result[2]).to be_an(Array)
|
117
|
-
expect(result[2][0]).
|
116
|
+
expect(result[2][0]).not_to be_nil
|
118
117
|
end
|
119
|
-
|
120
118
|
end
|
121
|
-
|
119
|
+
|
122
120
|
it 'returns ArgumentError if object is not allowed' do
|
123
|
-
expect
|
121
|
+
expect do
|
124
122
|
response.build(nil)
|
125
|
-
|
123
|
+
end.to raise_error(ArgumentError)
|
126
124
|
end
|
127
125
|
end
|
128
|
-
end
|
126
|
+
end
|
data/spec/router_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
require 'rack/test'
|
3
3
|
require 'lydia/router'
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe 'Router' do
|
6
6
|
include Rack::Test::Methods
|
7
7
|
|
8
8
|
class TestRouter < Lydia::Router
|
@@ -15,16 +15,16 @@ describe "Router" do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
get '/500' do
|
18
|
-
raise StandardError
|
18
|
+
raise StandardError, 'Error!'
|
19
19
|
end
|
20
20
|
|
21
|
-
head
|
22
|
-
get
|
23
|
-
patch
|
24
|
-
put
|
25
|
-
post
|
26
|
-
delete
|
27
|
-
options
|
21
|
+
head('/request') { get_response('head') }
|
22
|
+
get('/request') { get_response('get') }
|
23
|
+
patch('/request') { get_response('patch') }
|
24
|
+
put('/request') { get_response('put') }
|
25
|
+
post('/request') { get_response('post') }
|
26
|
+
delete('/request') { get_response('delete') }
|
27
|
+
options('/request') { get_response('options') }
|
28
28
|
|
29
29
|
namespace '/namespace' do
|
30
30
|
get '/hello' do
|
@@ -89,7 +89,7 @@ describe "Router" do
|
|
89
89
|
end
|
90
90
|
|
91
91
|
def get_response(body, status = 200)
|
92
|
-
[status, { 'Content-Type' => 'text/html', 'Content-Length'=> body.length.to_s }, [body]]
|
92
|
+
[status, { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]]
|
93
93
|
end
|
94
94
|
end
|
95
95
|
|
@@ -115,11 +115,12 @@ describe "Router" do
|
|
115
115
|
end
|
116
116
|
|
117
117
|
context 'Response' do
|
118
|
-
it
|
118
|
+
it 'returns a valid response' do
|
119
119
|
get '/'
|
120
|
-
expect(last_response).
|
120
|
+
expect(last_response).not_to be_nil
|
121
121
|
expect(last_response.status).to eq(200)
|
122
|
-
expect(last_response.headers.to_hash).to eq(
|
122
|
+
expect(last_response.headers.to_hash).to eq('Content-Type' => 'text/html',
|
123
|
+
'Content-Length' => '21')
|
123
124
|
expect(last_response.body).to eq('<H1>Hello world!</H1>')
|
124
125
|
end
|
125
126
|
end
|
@@ -169,7 +170,6 @@ describe "Router" do
|
|
169
170
|
end
|
170
171
|
|
171
172
|
context 'Routing' do
|
172
|
-
|
173
173
|
context 'Namespace' do
|
174
174
|
it 'GET /namespace/hello' do
|
175
175
|
get '/namespace/hello'
|
@@ -248,13 +248,13 @@ describe "Router" do
|
|
248
248
|
it 'GET /index.json' do
|
249
249
|
get '/index.json'
|
250
250
|
expect(last_response.status).to eq(200)
|
251
|
-
expect(last_response.body).to eq('json')
|
251
|
+
expect(last_response.body).to eq('json')
|
252
252
|
end
|
253
253
|
end
|
254
254
|
|
255
255
|
context 'Query string params' do
|
256
256
|
it 'GET /querystring_params' do
|
257
|
-
get '/querystring_params',
|
257
|
+
get '/querystring_params', name: 'bob'
|
258
258
|
expect(last_response.status).to eq(200)
|
259
259
|
expect(last_response.body).to eq('bob')
|
260
260
|
end
|
@@ -262,12 +262,12 @@ describe "Router" do
|
|
262
262
|
|
263
263
|
context 'Route not valid' do
|
264
264
|
it 'returns ArgumentError' do
|
265
|
-
expect
|
265
|
+
expect do
|
266
266
|
class WrongRoute < Lydia::Router
|
267
267
|
get Object do
|
268
268
|
end
|
269
269
|
end
|
270
|
-
|
270
|
+
end.to raise_error(ArgumentError)
|
271
271
|
end
|
272
272
|
end
|
273
273
|
end
|
@@ -308,5 +308,4 @@ describe "Router" do
|
|
308
308
|
expect(last_response.status).to eq(200)
|
309
309
|
end
|
310
310
|
end
|
311
|
-
|
312
311
|
end
|