rasti-web 1.1.0 → 1.2.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: 8be22c6b9730ecebfc6deb7449fc33fc580eedfa
4
- data.tar.gz: 260d7b82a6a975f7872c0e6d5a2cff49f526272f
3
+ metadata.gz: f5b7da3fc4edfaad86b1d790a3c003196931296d
4
+ data.tar.gz: c5609146785dc24b28067ed485becd91f46b613d
5
5
  SHA512:
6
- metadata.gz: 2255e469c1232c3f66a83e17aea842e34f55d51e7c23656be60f30b90785c864e08aed79dd6f6a6f7ebc72256ab95bfc218c6a077418ec0c09c3821e16fdc119
7
- data.tar.gz: ee292a32f9ec7142d1bb0ab386d635fb128bcc277e44bda50430761b183d361d699bdce02e531766bf5513a4ae08011081ddda445c1258146387e95af144a4c8
6
+ metadata.gz: df8e6c78eca2b2dcfd8ff4cddcb80dbb3263fe3c3282e93fb8e1a091b6c3315bac8ced0777357d55067c839749e9b220b11f295efe8d0474202e258afff1e4bd
7
+ data.tar.gz: 5146ccb76aadf74162fc60e270dd21aed3d8bff3c10cccc23cffacf8dac3f2ab8764562dea8c7927bba5bb536c38ef94506097c8b818a995eb924dd3b1fc70b2
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.3.0
1
+ ruby-2.4.0
data/.travis.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
+
2
3
  rvm:
3
4
  - 1.9.3
4
5
  - 2.0
@@ -6,6 +7,16 @@ rvm:
6
7
  - 2.2
7
8
  - 2.3.0
8
9
  - 2.4.0
9
- - jruby
10
+ - jruby-1.7.25
11
+ - jruby-9.1.7.0
12
+ - ruby-head
13
+ - jruby-head
14
+
15
+ matrix:
16
+ fast_finish: true
17
+ allow_failures:
18
+ - rvm: ruby-head
19
+ - rvm: jruby-head
20
+
10
21
  before_install:
11
22
  - gem install bundler
data/README.md CHANGED
@@ -62,6 +62,38 @@ class UsersController < Rasti::Web::Controller
62
62
  end
63
63
  ```
64
64
 
65
+ ### Hooks
66
+
67
+ ```ruby
68
+ class UsersController < Rasti::Web::Controller
69
+
70
+ before_action do |action_name|
71
+ end
72
+
73
+ before_action :action_name do
74
+ end
75
+
76
+ after_action do |action_name|
77
+ end
78
+
79
+ after_action :action_name do
80
+ end
81
+
82
+ end
83
+ ```
84
+
85
+ ### Error handling
86
+
87
+ ```ruby
88
+ class UsersController < Rasti::Web::Controller
89
+
90
+ rescue_from StandardError do |ex|
91
+ render.status 500, 'Unexpected error'
92
+ end
93
+
94
+ end
95
+ ```
96
+
65
97
  ## Contributing
66
98
 
67
99
  1. Fork it ( https://github.com/gabynaiman/rasti-web/fork )
data/Rakefile CHANGED
@@ -3,7 +3,8 @@ require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new(:spec) do |t|
5
5
  t.libs << 'spec'
6
- t.pattern = 'spec/**/*_spec.rb'
6
+ t.libs << 'lib'
7
+ t.pattern = ENV['DIR'] ? File.join(ENV['DIR'], '**', '*_spec.rb') : 'spec/**/*_spec.rb'
7
8
  t.verbose = false
8
9
  t.warning = false
9
10
  t.loader = nil if ENV['TEST']
@@ -16,26 +16,65 @@ module Rasti
16
16
  end
17
17
 
18
18
  class << self
19
+
19
20
  def action(action_name)
20
21
  raise "Undefined action '#{action_name}' in #{name}" unless instance_methods.include? action_name.to_sym
21
22
 
22
23
  Endpoint.new do |req, res, render|
23
24
  controller = new req, res, render
24
25
  begin
26
+ call_before_action controller, action_name
25
27
  controller.public_send action_name
26
28
  rescue => ex
27
- exception_class = handled_exceptions.detect { |klass| ex.is_a? klass }
28
- if exception_class
29
- controller.instance_exec ex, &exception_handlers[exception_class]
30
- else
31
- raise ex
32
- end
29
+ call_exception_handler controller, ex
30
+ ensure
31
+ call_after_action controller, action_name
33
32
  end
34
33
  end
35
34
  end
36
35
 
37
36
  alias_method :>>, :action
38
37
 
38
+ def before_action(action_name=nil, &block)
39
+ before_action_hooks[action_name] = block
40
+ end
41
+
42
+ def after_action(action_name=nil, &block)
43
+ after_action_hooks[action_name] = block
44
+ end
45
+
46
+ def rescue_from(exception_class, &block)
47
+ exception_handlers[exception_class] = block
48
+ end
49
+
50
+ def call_before_action(controller, action_name)
51
+ hook = before_action_hooks[action_name] || before_action_hooks[nil]
52
+ controller.instance_exec action_name, &hook if hook
53
+ end
54
+
55
+ def call_after_action(controller, action_name)
56
+ hook = after_action_hooks[action_name] || after_action_hooks[nil]
57
+ controller.instance_exec action_name, &hook if hook
58
+ end
59
+
60
+ def call_exception_handler(controller, exception)
61
+ exception_class = handled_exceptions.detect { |klass| exception.is_a? klass }
62
+ exception_handler = exception_handlers[exception_class]
63
+ if exception_handler
64
+ controller.instance_exec exception, &exception_handler
65
+ else
66
+ raise exception
67
+ end
68
+ end
69
+
70
+ def before_action_hooks
71
+ @before_action_hooks ||= superclass.respond_to?(:before_action_hooks) ? superclass.before_action_hooks : {}
72
+ end
73
+
74
+ def after_action_hooks
75
+ @after_action_hooks ||= superclass.respond_to?(:after_action_hooks) ? superclass.after_action_hooks : {}
76
+ end
77
+
39
78
  def exception_handlers
40
79
  @exception_handlers ||= superclass.respond_to?(:exception_handlers) ? superclass.exception_handlers : {}
41
80
  end
@@ -44,9 +83,6 @@ module Rasti
44
83
  @handled_exceptions ||= ClassAncestrySort.desc exception_handlers.keys
45
84
  end
46
85
 
47
- def rescue_from(exception_class, &block)
48
- exception_handlers[exception_class] = block
49
- end
50
86
  end
51
87
 
52
88
  end
@@ -81,7 +81,7 @@ module Rasti
81
81
  end
82
82
 
83
83
  def extract_status(args)
84
- args.detect { |a| a.is_a? Fixnum }
84
+ args.detect { |a| a.is_a? Integer }
85
85
  end
86
86
 
87
87
  def extract_headers(args)
@@ -1,5 +1,5 @@
1
1
  module Rasti
2
2
  module Web
3
- VERSION = '1.1.0'
3
+ VERSION = '1.2.0'
4
4
  end
5
5
  end
@@ -1,7 +1,28 @@
1
1
  require 'minitest_helper'
2
2
 
3
3
  class TestController < Rasti::Web::Controller
4
+
4
5
  CustomError = Class.new StandardError
6
+
7
+ def self.hooks_log
8
+ @hooks_log ||= []
9
+ end
10
+
11
+ before_action do |action_name|
12
+ self.class.hooks_log << "Before all: #{action_name}"
13
+ end
14
+
15
+ after_action do |action_name|
16
+ self.class.hooks_log << "After all: #{action_name}"
17
+ end
18
+
19
+ before_action :test do
20
+ self.class.hooks_log << 'Before single: test'
21
+ end
22
+
23
+ after_action :test do
24
+ self.class.hooks_log << 'After single: test'
25
+ end
5
26
 
6
27
  def test
7
28
  render.html 'Test HTML'
@@ -30,6 +51,10 @@ class TestController < Rasti::Web::Controller
30
51
  end
31
52
 
32
53
  describe Rasti::Web::Controller do
54
+
55
+ before do
56
+ TestController.hooks_log.clear
57
+ end
33
58
 
34
59
  it 'Action endpoint' do
35
60
  action = TestController.action :test
@@ -40,11 +65,17 @@ describe Rasti::Web::Controller do
40
65
  status.must_equal 200
41
66
  headers['Content-Type'].must_equal 'text/html; charset=utf-8'
42
67
  response.body.must_equal ['Test HTML']
68
+
69
+ TestController.hooks_log.must_equal [
70
+ 'Before single: test',
71
+ 'After single: test'
72
+ ]
43
73
  end
44
74
 
45
75
  it 'Invalid action' do
46
76
  error = proc { TestController.action :invalid }.must_raise RuntimeError
47
77
  error.message.must_equal "Undefined action 'invalid' in TestController"
78
+ TestController.hooks_log.must_be_empty
48
79
  end
49
80
 
50
81
  it 'Rescue explicit exception' do
@@ -54,6 +85,11 @@ describe Rasti::Web::Controller do
54
85
 
55
86
  status.must_equal 500
56
87
  response.body.must_equal ['Explicit error']
88
+
89
+ TestController.hooks_log.must_equal [
90
+ 'Before all: explicit_fail',
91
+ 'After all: explicit_fail'
92
+ ]
57
93
  end
58
94
 
59
95
  it 'Rescue implicit exception' do
@@ -63,6 +99,11 @@ describe Rasti::Web::Controller do
63
99
 
64
100
  status.must_equal 500
65
101
  response.body.must_equal ['Implicit error']
102
+
103
+ TestController.hooks_log.must_equal [
104
+ 'Before all: implicit_fail',
105
+ 'After all: implicit_fail'
106
+ ]
66
107
  end
67
108
 
68
109
  it 'Unexpected exception' do
@@ -71,6 +112,11 @@ describe Rasti::Web::Controller do
71
112
 
72
113
  error = proc { action.call env }.must_raise RuntimeError
73
114
  error.message.must_equal 'Unexpected error'
115
+
116
+ TestController.hooks_log.must_equal [
117
+ 'Before all: exception',
118
+ 'After all: exception'
119
+ ]
74
120
  end
75
121
 
76
122
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasti-web
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-18 00:00:00.000000000 Z
11
+ date: 2017-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -300,7 +300,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
300
300
  version: '0'
301
301
  requirements: []
302
302
  rubyforge_project:
303
- rubygems_version: 2.5.1
303
+ rubygems_version: 2.6.8
304
304
  signing_key:
305
305
  specification_version: 4
306
306
  summary: Web blocks to build robust applications