angus 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ class MiddlewareNotFound < StandardError
2
+
3
+ end
@@ -3,6 +3,8 @@ require 'angus-router'
3
3
  require_relative 'response'
4
4
  require_relative 'responses'
5
5
  require_relative 'middleware/exception_handler'
6
+ require_relative 'exceptions'
7
+ MiddlewareNotFound
6
8
 
7
9
  module Angus
8
10
  class RequestHandler
@@ -11,6 +13,8 @@ module Angus
11
13
 
12
14
  DEFAULT_RENDER = :json
13
15
 
16
+ attr_reader :middleware
17
+
14
18
  def initialize
15
19
  @router = Angus::Router.new
16
20
  @middleware = []
@@ -28,7 +32,11 @@ module Angus
28
32
 
29
33
  def to_app
30
34
  inner_app = lambda { |env| self.dup.call!(env) }
31
- @middleware.reverse.inject(inner_app) { |app, middleware| middleware.call(app) }
35
+ @middleware.reverse.inject(inner_app) do |app, middleware|
36
+ klass, args, block = middleware
37
+
38
+ klass.new(app, *args, &block)
39
+ end
32
40
  end
33
41
 
34
42
  def call!(env)
@@ -63,7 +71,23 @@ module Angus
63
71
  end
64
72
 
65
73
  def use(middleware, *args, &block)
66
- @middleware << lambda { |app| middleware.new(app, *args, &block) }
74
+ return if @middleware.map(&:first).include?(middleware)
75
+
76
+ @middleware << [middleware, args, block]
77
+ end
78
+
79
+ def use_before(klass, middleware, *args, &block)
80
+ return if @middleware.map(&:first).include?(middleware)
81
+ index = @middleware.map(&:first).index(klass) or raise MiddlewareNotFound.new(middleware)
82
+
83
+ @middleware.insert(index, [middleware, args, block])
84
+ end
85
+
86
+ def use_after(klass, middleware, *args, &block)
87
+ return if @middleware.map(&:first).include?(middleware)
88
+ index = @middleware.map(&:first).index(klass) or raise MiddlewareNotFound.new(middleware)
89
+
90
+ @middleware.insert(index + 1, [middleware, args, block])
67
91
  end
68
92
 
69
93
  end
data/lib/angus/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Angus
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -1,8 +1,3 @@
1
- #require_relative '../definition_reader'
2
- #require_relative '../responses'
3
- #require_relative '../status_codes'
4
-
5
-
6
1
  require 'spec_helper'
7
2
 
8
3
  require 'angus/middleware/exception_handler'
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ require 'angus/request_handler'
4
+
5
+ work_dir = File.join(File.dirname(__FILE__), '..', 'functional', 'basic')
6
+ describe Angus::RequestHandler, { :work_dir => work_dir } do
7
+
8
+ subject(:handler) { Angus::RequestHandler.new }
9
+
10
+ let(:app) { double(:app) }
11
+
12
+ describe '.new' do
13
+ it 'has a a exception handler middleware' do
14
+ handler.middleware.map(&:first).should include(Angus::Middleware::ExceptionHandler)
15
+ end
16
+ end
17
+
18
+ describe '#use' do
19
+
20
+ let(:other_middleware) { Struct.new(:app) }
21
+
22
+ it 'adds the given middleware after the rest' do
23
+ handler.use(other_middleware)
24
+
25
+ handler.middleware.last.first.should eq(other_middleware)
26
+ end
27
+
28
+ context 'when the given middleware is already present' do
29
+ before { handler.use(other_middleware) }
30
+
31
+ it 'does not modify the middleware' do
32
+ expect {
33
+ handler.use(other_middleware)
34
+ }.to_not change { handler.middleware }
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ describe '#use_after' do
41
+
42
+ let(:other_middleware) { Struct.new(:app) }
43
+
44
+ it 'adds the given middleware after the given class' do
45
+ handler.use_after(Angus::Middleware::ExceptionHandler, other_middleware)
46
+
47
+ handler.middleware.last.first.should eq(other_middleware)
48
+ end
49
+
50
+ context 'when the given class is not present in the middleware' do
51
+ it 'raises an MiddlewareNotFound error' do
52
+ expect {
53
+ handler.use_after(Angus::Middleware, other_middleware)
54
+ }.to raise_error(MiddlewareNotFound)
55
+ end
56
+ end
57
+
58
+ context 'when the given middleware is already present' do
59
+ before { handler.use_after(Angus::Middleware::ExceptionHandler, other_middleware) }
60
+
61
+ it 'does not modify the middleware' do
62
+ expect {
63
+ handler.use_after(Angus::Middleware::ExceptionHandler, other_middleware)
64
+ }.to_not change { handler.middleware }
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ describe '#use_before' do
71
+
72
+ let(:other_middleware) { Struct.new(:app) }
73
+
74
+ it 'adds the given middleware before the given class' do
75
+ handler.use_before(Angus::Middleware::ExceptionHandler, other_middleware)
76
+
77
+ handler.middleware.first.first.should eq(other_middleware)
78
+ end
79
+
80
+ context 'when the given class is not present in the middleware' do
81
+ it 'raises an MiddlewareNotFound error' do
82
+ expect {
83
+ handler.use_before(Angus::Middleware, other_middleware)
84
+ }.to raise_error(MiddlewareNotFound)
85
+ end
86
+ end
87
+
88
+ context 'when the given middleware is already present' do
89
+ before { handler.use_before(Angus::Middleware::ExceptionHandler, other_middleware) }
90
+
91
+ it 'does not modify the middleware' do
92
+ expect {
93
+ handler.use_before(Angus::Middleware::ExceptionHandler, other_middleware)
94
+ }.to_not change { handler.middleware }
95
+ end
96
+ end
97
+
98
+ end
99
+
100
+ end
@@ -1,7 +1,7 @@
1
1
  get_user:
2
- name: Obtener Información de un Usuario
2
+ name: Obtener Informacion de un Usuario
3
3
  description: >
4
- Devuelve información del usuario, como por ejemplo e-mail y fecha de nacimiento.
4
+ Devuelve informacion del usuario, como por ejemplo e-mail y fecha de nacimiento.
5
5
 
6
6
  path: /users/:user_id
7
7
  method: get
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: angus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-11-26 00:00:00.000000000 Z
14
+ date: 2013-11-29 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: thor
@@ -233,6 +233,7 @@ files:
233
233
  - lib/angus/base_resource.rb
234
234
  - lib/angus/command.rb
235
235
  - lib/angus/response.rb
236
+ - lib/angus/exceptions.rb
236
237
  - lib/angus/middleware/exception_handler.rb
237
238
  - lib/angus/rspec/support/examples/describe_errors.rb
238
239
  - lib/angus/rspec/support/matchers/operation_response_matchers.rb
@@ -247,6 +248,7 @@ files:
247
248
  - lib/angus/base.rb
248
249
  - lib/angus.rb
249
250
  - spec/spec_helper.rb
251
+ - spec/angus/request_handler_spec.rb
250
252
  - spec/angus/middleware/exception_handler_spec.rb
251
253
  - spec/angus/rspec/examples/describe_errors_spec.rb
252
254
  - spec/functional/no_resources_spec.rb
@@ -297,6 +299,7 @@ specification_version: 3
297
299
  summary: angus
298
300
  test_files:
299
301
  - spec/spec_helper.rb
302
+ - spec/angus/request_handler_spec.rb
300
303
  - spec/angus/middleware/exception_handler_spec.rb
301
304
  - spec/angus/rspec/examples/describe_errors_spec.rb
302
305
  - spec/functional/no_resources_spec.rb