sinatra_simple_router 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +9 -9
- data/.travis.yml +5 -0
- data/LICENSE.txt +1 -1
- data/README.md +56 -13
- data/Rakefile +1 -2
- data/lib/sinatra_simple_router/controller.rb +8 -9
- data/lib/sinatra_simple_router/version.rb +1 -1
- data/lib/sinatra_simple_router.rb +11 -0
- data/spec/sinatra_simple_router_spec.rb +10 -6
- data/spec/spec_helper.rb +15 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NzdlNDVmMGY3OGYzYTU5NjZjYTk1YWFhZTFhZTYxNmYzNzY5NjY4OQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
7
|
-
|
6
|
+
YWU2ODZiZTZjZTZmMzIxOTBmNjIyZjU4MzIwZGJmZDBlN2Q0MzAzZA==
|
7
|
+
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MDgxOTdhZmY3NDkzOTVjMTFiMjVmMzE1MmY0NzU2YjEzZDkwODQzZTY1Zjlk
|
10
|
+
ZTM5NzU4YjhmNGQ1NGY5Y2YyMDNjNjQ1ZGMzYmI5NDY3OTdkMDBmOGUzMDBl
|
11
|
+
ZGFhZGRlMDNjYmY2NzgyM2Q4MGY2NzBmZDA5Yjc1ZmVkNGJlMDQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ODZkMTRhYzhlMjJkOTc0ZjAxMTVjM2I1ZjFlMzI2MTZkMGE1MDdjM2Y3Y2M4
|
14
|
+
MGI5Njk1OWFlMDMwNTEzMTQyYWMwN2RmNTk3OGMwNmQ4MjZjNjBlZmE3YjY4
|
15
|
+
OGQzZTQwZTFhYjc3OGQ5NzFlOTNkM2QzNmM5NDc3Yjg1YjFhNjg=
|
data/.travis.yml
ADDED
data/LICENSE.txt
CHANGED
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
19
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
20
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
21
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# SinatraSimpleRouter
|
2
2
|
|
3
|
-
|
3
|
+
[![Build Status](https://travis-ci.org/elvio/sinatra_simple_router.png)](https://travis-ci.org/elvio/sinatra_simple_router)
|
4
|
+
[![RubyGem](https://badge.fury.io/rb/sinatra_simple_router.png)](https://rubygems.org/gems/sinatra_simple_router)
|
5
|
+
|
6
|
+
Simple routing abstraction for Sinatra applications.
|
4
7
|
|
5
8
|
## Installation
|
6
9
|
|
@@ -22,19 +25,27 @@ Or install it yourself as:
|
|
22
25
|
require "sinatra"
|
23
26
|
require "sinatra_simple_router"
|
24
27
|
|
25
|
-
class
|
26
|
-
def
|
27
|
-
@
|
28
|
-
erb :"users/show"
|
28
|
+
class OrdersController
|
29
|
+
def initialize(app)
|
30
|
+
@app = app
|
29
31
|
end
|
30
|
-
|
31
|
-
def
|
32
|
-
@
|
33
|
-
@
|
34
|
-
|
32
|
+
|
33
|
+
def show
|
34
|
+
@order = Order.find(params[:id])
|
35
|
+
@app.content_type "application/json"
|
36
|
+
@app.body @order.to_json
|
35
37
|
end
|
36
38
|
end
|
37
39
|
|
40
|
+
class Application < Sinatra::Base
|
41
|
+
include SinatraSimpleRouter
|
42
|
+
match :get, "/orders/:id.json", OrdersController, :show
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
### SinatraSimpleRouter::Controller
|
47
|
+
|
48
|
+
```ruby
|
38
49
|
class ItemsController < SinatraSimpleRouter::Controller
|
39
50
|
def show
|
40
51
|
@item = Item.find(params[:id])
|
@@ -44,13 +55,45 @@ end
|
|
44
55
|
|
45
56
|
class Application < Sinatra::Base
|
46
57
|
include SinatraSimpleRouter
|
47
|
-
|
48
|
-
match :get, "/users/:id", UsersController, :show
|
49
|
-
match :patch, "/users/:id", UsersController, :update
|
50
58
|
match :get, "/items/:id.json", ItemsController, :show
|
51
59
|
end
|
52
60
|
```
|
53
61
|
|
62
|
+
### Versioning URLs
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
require "sinatra"
|
66
|
+
require "sinatra_simple_router"
|
67
|
+
|
68
|
+
class V1::ItemsController < SinatraSimpleRouter::Controller
|
69
|
+
def show
|
70
|
+
@item = Item.find(params[:id])
|
71
|
+
render json: @item
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class V2::ItemsController < SinatraSimpleRouter::Controller
|
76
|
+
def show
|
77
|
+
@item = Item.find(params[:id])
|
78
|
+
render json: ItemDecorator.new(@item).to_json
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class Application < Sinatra::Base
|
83
|
+
include SinatraSimpleRouter
|
84
|
+
|
85
|
+
# maps '/v1/items/:id.json' to V1::ItemsController
|
86
|
+
version :v1 do
|
87
|
+
match :get, "/items/:id.json", V1::ItemsController, :show
|
88
|
+
end
|
89
|
+
|
90
|
+
# maps '/v2/items/:id.json' to V2::ItemsController
|
91
|
+
version :v2 do
|
92
|
+
match :get, "/items/:id.json", V2::ItemsController, :show
|
93
|
+
end
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
54
97
|
## Contributing
|
55
98
|
|
56
99
|
1. Fork it
|
data/Rakefile
CHANGED
@@ -4,19 +4,18 @@ module SinatraSimpleRouter
|
|
4
4
|
@app = app
|
5
5
|
end
|
6
6
|
|
7
|
-
def render(
|
8
|
-
|
9
|
-
|
10
|
-
end
|
7
|
+
def render(attrs = {})
|
8
|
+
code = attrs[:status].to_i
|
9
|
+
status(code) if code > 0
|
11
10
|
|
12
|
-
if
|
11
|
+
if attrs[:html]
|
13
12
|
content_type "text/html"
|
14
|
-
body html.to_s
|
15
|
-
elsif
|
13
|
+
body attrs[:html].to_s
|
14
|
+
elsif attrs[:json]
|
16
15
|
content_type "application/json"
|
17
|
-
body json.to_json
|
16
|
+
body attrs[:json].to_json
|
18
17
|
else
|
19
|
-
raise ArgumentError.new("Unknown format in #{
|
18
|
+
raise ArgumentError.new("Unknown format in #{attrs.inspect}")
|
20
19
|
end
|
21
20
|
end
|
22
21
|
|
@@ -12,9 +12,20 @@ module SinatraSimpleRouter
|
|
12
12
|
|
13
13
|
module ClassMethods
|
14
14
|
def match(method, path, klass, action)
|
15
|
+
if @version
|
16
|
+
path = "/#{@version}#{path}"
|
17
|
+
end
|
18
|
+
|
19
|
+
puts "Registering: #{path}"
|
15
20
|
send(method, path) do
|
16
21
|
klass.new(self).send(action)
|
17
22
|
end
|
18
23
|
end
|
24
|
+
|
25
|
+
def version(version, &block)
|
26
|
+
@version = version
|
27
|
+
instance_eval(&block)
|
28
|
+
@version = nil
|
29
|
+
end
|
19
30
|
end
|
20
31
|
end
|
@@ -18,14 +18,18 @@ module SinatraSimpleRouter
|
|
18
18
|
end
|
19
19
|
|
20
20
|
describe ".match" do
|
21
|
-
before do
|
22
|
-
MyApplication.match(:get, "/", UsersController, :index)
|
23
|
-
end
|
24
|
-
|
25
21
|
it "dispatches the route to the controller class and action" do
|
26
|
-
request = Rack::MockRequest.new(
|
22
|
+
request = Rack::MockRequest.new(SimpleApplication)
|
27
23
|
response = request.get("/")
|
28
|
-
expect(response.body).to eq("
|
24
|
+
expect(response.body).to eq("index")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe ".version" do
|
29
|
+
it "uses the specified version as prefix" do
|
30
|
+
request = Rack::MockRequest.new(VersionedApplication)
|
31
|
+
response = request.post("/v1/subscribe")
|
32
|
+
expect(response.body).to eq("subscribe")
|
29
33
|
end
|
30
34
|
end
|
31
35
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -10,15 +10,28 @@ module SinatraSimpleRouter
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def index
|
13
|
-
"
|
13
|
+
"index"
|
14
|
+
end
|
15
|
+
|
16
|
+
def subscribe
|
17
|
+
"subscribe"
|
14
18
|
end
|
15
19
|
end
|
16
20
|
|
17
21
|
class ItemsController < Controller
|
18
22
|
end
|
19
23
|
|
20
|
-
class
|
24
|
+
class SimpleApplication < Sinatra::Base
|
21
25
|
include SinatraSimpleRouter
|
26
|
+
match(:get, "/", UsersController, :index)
|
27
|
+
end
|
28
|
+
|
29
|
+
class VersionedApplication < Sinatra::Base
|
30
|
+
include SinatraSimpleRouter
|
31
|
+
|
32
|
+
version :v1 do
|
33
|
+
match(:post, "/subscribe", UsersController, :subscribe)
|
34
|
+
end
|
22
35
|
end
|
23
36
|
|
24
37
|
class HasAncestor < Sinatra::Base
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- elvio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -74,6 +74,7 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- .gitignore
|
77
|
+
- .travis.yml
|
77
78
|
- Gemfile
|
78
79
|
- LICENSE.txt
|
79
80
|
- README.md
|
@@ -105,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
106
|
version: '0'
|
106
107
|
requirements: []
|
107
108
|
rubyforge_project:
|
108
|
-
rubygems_version: 2.
|
109
|
+
rubygems_version: 2.2.1
|
109
110
|
signing_key:
|
110
111
|
specification_version: 4
|
111
112
|
summary: Simple routing and controller abstraction to Sinatra applications
|