sinatra_simple_router 0.0.4 → 0.0.5
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 +8 -8
- data/README.md +22 -0
- data/Rakefile +7 -5
- data/lib/sinatra_simple_router.rb +24 -2
- data/lib/sinatra_simple_router/version.rb +1 -1
- data/spec/sinatra_simple_router_spec.rb +11 -1
- data/spec/spec_helper.rb +21 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MzJlMzc1NjVhODRmODZmMDFiNTdjZGVkNDVjNzQxYzE2NDVkMGQyYg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
N2YwMDZkZGQxOTRmNTNlNDM5YmVhYzY2ZmQ0MTA4YWExMmI0MTQ2Ng==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OTZjOWE5NDU4NDUyNjlkZmUyYzAyOGI5MjhiOTQwYjVlODZhNzU5OWNiZDg1
|
10
|
+
ZjQwOWZhOTJhYTE0MmZmMjcyZDFiZDgzNzA2NDkzYTJhNzcyOTczZTMxMmQw
|
11
|
+
M2ZmMWIwOGIyZmZjY2M5NTJkZGJjNjFhZDQ3YjkwNDNhMGZiMTA=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OWNjZDUxMzkyMWM1MGIxN2QzZTdiZTYwNjMzMDk1OTY5MDc5Mzg3NzY4M2Ew
|
14
|
+
MDQ5YTg5OGI0NWZhZTE1MmM2YzZhZjA3NWQ4MWJjZjZjZWRiOGFhZGFmYWRh
|
15
|
+
Y2FjYjc4ZTU4ZjFmZDFiY2FhZTE2OGFmMzljYmQ4YzkxYTE0MjY=
|
data/README.md
CHANGED
@@ -94,6 +94,28 @@ class Application < Sinatra::Base
|
|
94
94
|
end
|
95
95
|
```
|
96
96
|
|
97
|
+
### Rescue exceptions raised in controllers
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
class ItemsController < SinatraSimpleRouter::Controller
|
101
|
+
def create
|
102
|
+
@item = Item.new(params[:item])
|
103
|
+
@item.save!
|
104
|
+
render json: @item
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
class Application < Sinatra::Base
|
109
|
+
include SinatraSimpleRouter
|
110
|
+
|
111
|
+
rescue_exception MongoMapper::DocumentNotValid do |exception, controller|
|
112
|
+
controller.render json: {error: exception.document.errors}, status: 400
|
113
|
+
end
|
114
|
+
|
115
|
+
match :post, "/items.json", ItemsController, :create
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
97
119
|
## Contributing
|
98
120
|
|
99
121
|
1. Fork it
|
data/Rakefile
CHANGED
@@ -13,14 +13,16 @@ task :install => :build do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
task :release => :build do
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
cmds = []
|
17
|
+
cmds << "git tag -a v#{SinatraSimpleRouter::VERSION} -m 'Tagging #{SinatraSimpleRouter::VERSION}'"
|
18
|
+
cmds << "git push --tags"
|
19
|
+
cmds << "gem push sinatra_simple_router-#{SinatraSimpleRouter::VERSION}.gem"
|
20
|
+
cmds << "rm *.gem"
|
21
|
+
system cmds.join(' && ')
|
20
22
|
end
|
21
23
|
|
22
24
|
RSpec::Core::RakeTask.new("spec") do |spec|
|
23
25
|
spec.pattern = "spec/**/*_spec.rb"
|
24
26
|
end
|
25
27
|
|
26
|
-
task :default => :spec
|
28
|
+
task :default => :spec
|
@@ -11,14 +11,36 @@ module SinatraSimpleRouter
|
|
11
11
|
end
|
12
12
|
|
13
13
|
module ClassMethods
|
14
|
+
@@rescued_exceptions = {}
|
15
|
+
|
16
|
+
def rescue_exception(exception, &block)
|
17
|
+
exception_name = exception.name
|
18
|
+
|
19
|
+
if @@rescued_exceptions[exception_name]
|
20
|
+
@@rescued_exceptions[exception_name] << block
|
21
|
+
else
|
22
|
+
@@rescued_exceptions[exception_name] = [block]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
14
26
|
def match(method, path, klass, action)
|
15
27
|
if @version
|
16
28
|
path = "/#{@version}#{path}"
|
17
29
|
end
|
18
30
|
|
19
|
-
puts "Registering: #{path}"
|
20
31
|
send(method, path) do
|
21
|
-
|
32
|
+
begin
|
33
|
+
klass.new(self).send(action)
|
34
|
+
rescue => e
|
35
|
+
handlers = Array(@@rescued_exceptions[e.class.name])
|
36
|
+
if handlers.any?
|
37
|
+
handlers.map do |handler|
|
38
|
+
handler.call(e, klass.new(self))
|
39
|
+
end
|
40
|
+
else
|
41
|
+
raise e
|
42
|
+
end
|
43
|
+
end
|
22
44
|
end
|
23
45
|
end
|
24
46
|
|
@@ -32,5 +32,15 @@ module SinatraSimpleRouter
|
|
32
32
|
expect(response.body).to eq("subscribe")
|
33
33
|
end
|
34
34
|
end
|
35
|
+
|
36
|
+
describe ".rescue_exception" do
|
37
|
+
it "calls the callback block" do
|
38
|
+
RescuedApplication.stub(:handle_exception)
|
39
|
+
request = Rack::MockRequest.new(RescuedApplication)
|
40
|
+
request.post("/subscribe")
|
41
|
+
arguments = [kind_of(ArgumentError), kind_of(SinatraSimpleRouter::UsersController)]
|
42
|
+
expect(RescuedApplication).to have_received(:handle_exception).with(*arguments)
|
43
|
+
end
|
44
|
+
end
|
35
45
|
end
|
36
|
-
end
|
46
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -16,6 +16,14 @@ module SinatraSimpleRouter
|
|
16
16
|
def subscribe
|
17
17
|
"subscribe"
|
18
18
|
end
|
19
|
+
|
20
|
+
def raise_argument_error
|
21
|
+
raise ArgumentError.new
|
22
|
+
end
|
23
|
+
|
24
|
+
def raise_runtime_error
|
25
|
+
raise RuntimeError.new
|
26
|
+
end
|
19
27
|
end
|
20
28
|
|
21
29
|
class ItemsController < Controller
|
@@ -34,6 +42,19 @@ module SinatraSimpleRouter
|
|
34
42
|
end
|
35
43
|
end
|
36
44
|
|
45
|
+
class RescuedApplication < Sinatra::Base
|
46
|
+
include SinatraSimpleRouter
|
47
|
+
|
48
|
+
def handle_exception(e, c)
|
49
|
+
end
|
50
|
+
|
51
|
+
rescue_exception ArgumentError do |exception, controller|
|
52
|
+
handle_exception(exception, controller)
|
53
|
+
end
|
54
|
+
|
55
|
+
match(:post, "/subscribe", UsersController, :raise_argument_error)
|
56
|
+
end
|
57
|
+
|
37
58
|
class HasAncestor < Sinatra::Base
|
38
59
|
end
|
39
60
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra_simple_router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- elvio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|