rack-app 0.12.1 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5759d181f7bd7eaddf70e9b1172c74dc22e50d2d
4
- data.tar.gz: c59a6ef74c8663237bbe91501b8a32b09c55ab2c
3
+ metadata.gz: 58f26a39292b8d52e2bfeabf12e5bdeb865cd9e4
4
+ data.tar.gz: f1fa441f35fe56d10ede7e4731281210f8b5d352
5
5
  SHA512:
6
- metadata.gz: 6b767a6f85cfa4e294e51fb6fb8b1c242675c7e2ff8555cf5e469f72cf40cfabab20123047be01b4e62636a24347b8d823bf6e0a4eaae8f14fd0fc775889e8b4
7
- data.tar.gz: 478c39eadfa8cee23a268520a184a0e6bb1a809ed5edea3725b6c824e71ba85ee4d6aa58eb34444e3ffdad1ec83123b3a1a4b3cb5cf5c7db8809adcd16b05a9b
6
+ metadata.gz: 5fa94365b0a18d26dc3f666a38bafe6686227d68599dce43a1d941aaac736ec678831537ed5dd00f404c44f166c9eff17a43bb36633f79ea95ac70d39ee46980
7
+ data.tar.gz: 462d5fca3c4429ccdc942159666539500850801cc571e6ad75db1ff73790952d20e46f42b9e2f1e0beecef5f6bb96e41e332f47797da1f88390a01fa386a40e9
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.12.1
1
+ 0.13.0
@@ -7,12 +7,14 @@ class Rack::App
7
7
  require 'rack/app/constants'
8
8
 
9
9
  require 'rack/app/params'
10
- require 'rack/app/utils'
11
10
  require 'rack/app/router'
12
11
  require 'rack/app/endpoint'
12
+ require 'rack/app/serializer'
13
+ require 'rack/app/error_handler'
13
14
  require 'rack/app/endpoint/not_found'
14
15
  require 'rack/app/request_configurator'
15
16
 
17
+ require 'rack/app/utils'
16
18
  require 'rack/app/file'
17
19
 
18
20
  class << self
@@ -70,6 +72,15 @@ class Rack::App
70
72
  end
71
73
  end
72
74
 
75
+ def error(*exception_classes,&block)
76
+ @error_handler ||= Rack::App::ErrorHandler.new
77
+ unless block.nil?
78
+ @error_handler.register_handler(exception_classes,block)
79
+ end
80
+
81
+ return @error_handler
82
+ end
83
+
73
84
  def router
74
85
  @router ||= Rack::App::Router.new
75
86
  end
@@ -81,7 +92,8 @@ class Rack::App
81
92
  :request_method => request_method,
82
93
  :request_path => request_path,
83
94
  :description => @last_description,
84
- :serializer => @serializer,
95
+ :serializer => serializer,
96
+ :error_handler => error,
85
97
  :app_class => self
86
98
  }
87
99
 
@@ -104,8 +116,14 @@ class Rack::App
104
116
  return nil
105
117
  end
106
118
 
107
- def serializer(&code)
108
- @serializer = code
119
+ def serializer(&definition_how_to_serialize)
120
+ @serializer ||= Rack::App::Serializer.new
121
+
122
+ unless definition_how_to_serialize.nil?
123
+ @serializer.set_serialization_logic(definition_how_to_serialize)
124
+ end
125
+
126
+ return @serializer
109
127
  end
110
128
 
111
129
  end
@@ -5,8 +5,9 @@ class Rack::App::Endpoint
5
5
  def initialize(properties)
6
6
  @properties = properties
7
7
  @logic_block = properties[:user_defined_logic]
8
- @serializer = properties[:serializer] || default_serializer
8
+ @serializer = properties[:serializer]
9
9
  @api_class = properties[:app_class]
10
+ @error_handler = properties[:error_handler]
10
11
 
11
12
  @path_params_matcher = {}
12
13
  end
@@ -23,7 +24,7 @@ class Rack::App::Endpoint
23
24
  request_handler.response = response
24
25
  request.env['rack.app.path_params_matcher']= @path_params_matcher.dup
25
26
 
26
- call_return = request_handler.instance_exec(&@logic_block)
27
+ call_return = @error_handler.execute_with_error_handling { request_handler.instance_exec(&@logic_block) }
27
28
 
28
29
  return call_return if is_a_rack_response_finish?(call_return)
29
30
  add_response_body_if_missing(call_return, response)
@@ -39,7 +40,7 @@ class Rack::App::Endpoint
39
40
  protected
40
41
 
41
42
  def add_response_body_if_missing(call_return, response)
42
- response.write(String(@serializer.call(call_return))) if response.body.empty?
43
+ response.write(String(@serializer.serialize(call_return))) if response.body.empty?
43
44
  end
44
45
 
45
46
  def is_a_rack_response_finish?(call_return)
@@ -49,8 +50,4 @@ class Rack::App::Endpoint
49
50
  call_return[1].is_a?(Hash)
50
51
  end
51
52
 
52
- def default_serializer
53
- lambda { |o| o.to_s }
54
- end
55
-
56
53
  end
@@ -8,7 +8,8 @@ not_found_properties = {
8
8
  :request_method => 'GET',
9
9
  :request_path => '\404',
10
10
  :description => 'page not found',
11
- :serializer => lambda { |o| String(o) },
11
+ :serializer => Rack::App::Serializer.new,
12
+ :error_handler => Rack::App::ErrorHandler.new,
12
13
  :app_class => app_class
13
14
  }
14
15
 
@@ -0,0 +1,35 @@
1
+ class Rack::App::ErrorHandler
2
+
3
+ def initialize
4
+ @handlers = {}
5
+ end
6
+
7
+ def register_handler(exception_classes, handler_block)
8
+ exception_classes.each do |exception_class|
9
+ @handlers[exception_class]= handler_block
10
+ end
11
+ nil
12
+ end
13
+
14
+ def execute_with_error_handling
15
+ yield
16
+ rescue *[Exception, @handlers.keys].flatten => ex
17
+ get_handler(ex).call(ex)
18
+ end
19
+
20
+ protected
21
+
22
+ def get_handler(ex)
23
+ explicit(ex) || parent(ex) || raise(ex)
24
+ end
25
+
26
+ def explicit(ex)
27
+ @handlers[ex.class]
28
+ end
29
+
30
+ def parent(ex)
31
+ handler = @handlers.find { |exception_class, handler| ex.class <= exception_class }
32
+ return handler.nil? ? nil : handler.last
33
+ end
34
+
35
+ end
@@ -0,0 +1,15 @@
1
+ class Rack::App::Serializer
2
+
3
+ def initialize
4
+ @proc = lambda { |o| o.to_s }
5
+ end
6
+
7
+ def set_serialization_logic(proc)
8
+ @proc = proc
9
+ end
10
+
11
+ def serialize(object)
12
+ @proc.call(object)
13
+ end
14
+
15
+ end
@@ -2,23 +2,38 @@ require 'uri'
2
2
  require 'rack/app'
3
3
  module Rack::App::Test
4
4
 
5
- # magic ;)
6
5
  def self.included(klass)
7
- class << klass
6
+ klass.__send__(:extend,SpecHelpers)
7
+ end
8
8
 
9
- define_method(:rack_app) do |*args, &constructor|
9
+ module SpecHelpers
10
10
 
11
- rack_app_class = args.shift
12
- subject_app = rack_app_class.is_a?(Class) ? rack_app_class : Class.new(Rack::App)
13
- subject_app.class_eval(&constructor) unless constructor.nil?
11
+ def rack_app(*args,&constructor)
14
12
 
13
+ begin
14
+ let(:rack_app){ rack_app_by(*args,&constructor) }
15
+ rescue NoMethodError
15
16
  define_method(:rack_app) do
16
- subject_app
17
+ rack_app_by(*args,&constructor)
17
18
  end
18
-
19
19
  end
20
20
 
21
21
  end
22
+
23
+ end
24
+
25
+ def rack_app_by(*args,&constructor)
26
+ subject_app = nil
27
+ rack_app_class = args.shift
28
+
29
+ if constructor.nil?
30
+ subject_app = rack_app_class
31
+ else
32
+ subject_app = Class.new(rack_app_class || Rack::App)
33
+ subject_app.class_eval(&constructor)
34
+ end
35
+
36
+ subject_app
22
37
  end
23
38
 
24
39
  [:get, :post, :put, :delete, :options, :patch].each do |request_method|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-app
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.1
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-02-02 00:00:00.000000000 Z
12
+ date: 2016-02-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -91,6 +91,7 @@ files:
91
91
  - lib/rack/app/constants.rb
92
92
  - lib/rack/app/endpoint.rb
93
93
  - lib/rack/app/endpoint/not_found.rb
94
+ - lib/rack/app/error_handler.rb
94
95
  - lib/rack/app/file.rb
95
96
  - lib/rack/app/file/server.rb
96
97
  - lib/rack/app/file/streamer.rb
@@ -100,6 +101,7 @@ files:
100
101
  - lib/rack/app/router.rb
101
102
  - lib/rack/app/router/dynamic.rb
102
103
  - lib/rack/app/router/static.rb
104
+ - lib/rack/app/serializer.rb
103
105
  - lib/rack/app/test.rb
104
106
  - lib/rack/app/utils.rb
105
107
  - lib/rack/app/version.rb