sapp 0.1.0
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 +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +292 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/sapp/base.rb +61 -0
- data/lib/sapp/handler.rb +47 -0
- data/lib/sapp/path/base.rb +136 -0
- data/lib/sapp/path/request.rb +107 -0
- data/lib/sapp/resources.rb +35 -0
- data/lib/sapp/response.rb +42 -0
- data/lib/sapp/route_map.rb +114 -0
- data/lib/sapp/router.rb +48 -0
- data/lib/sapp/routes.rb +56 -0
- data/lib/sapp/version.rb +3 -0
- data/lib/sapp.rb +6 -0
- data/sapp.gemspec +29 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c3113b85ab5e758b91667c80d84dc1e60c9712b4
|
4
|
+
data.tar.gz: 8eec613f494e6d40c27dc479ad4bb73a293c138e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 329dfd3cacb98e05b6cddb238044677283fd0a2624596c93873d9994bde59207de23155010c4c998ec17e5c90dacca563f054173601ef932be708b5787930fd6
|
7
|
+
data.tar.gz: c85f4766210506891481f850390967a49f79a14cf97dd01b407a6fd1225c84eb3cdee6f39eea3c0ca4bc542e8e20e6d97a82b0b66d261c588907eabdecdaed2a
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 hayduke19us
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,292 @@
|
|
1
|
+
# Sapp
|
2
|
+

|
3
|
+
|
4
|
+
Sapp is a simple application framework for [Rack]( http://rack.github.io/ ).
|
5
|
+
It attempts to simplify the routing process by handling route matching early
|
6
|
+
in the process.
|
7
|
+
|
8
|
+
### Architecture
|
9
|
+
|
10
|
+
**Module::Class( Mixins )**
|
11
|
+
|
12
|
+
- Sapp
|
13
|
+
- Base( Resources, Routes )
|
14
|
+
- Router
|
15
|
+
- RouteMap
|
16
|
+
- Path
|
17
|
+
- Base
|
18
|
+
- Request
|
19
|
+
- Handler
|
20
|
+
- Response
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
|
24
|
+
Add this line to your application's Gemfile:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
|
28
|
+
gem 'sapp'
|
29
|
+
|
30
|
+
```
|
31
|
+
|
32
|
+
And then execute:
|
33
|
+
|
34
|
+
$ bundle
|
35
|
+
|
36
|
+
Or install it yourself as:
|
37
|
+
|
38
|
+
$ gem install sapp
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
Sapp is meant to be sublcassed.
|
43
|
+
|
44
|
+
``` ruby
|
45
|
+
|
46
|
+
class UserEndpoints < Sapp::Base
|
47
|
+
|
48
|
+
routes go here...
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
```
|
53
|
+
|
54
|
+
### Routes
|
55
|
+
There are a few different ways to declare routes.
|
56
|
+
|
57
|
+
Sinatra style blocks:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
|
61
|
+
get '/users' do
|
62
|
+
'All users'
|
63
|
+
end
|
64
|
+
|
65
|
+
```
|
66
|
+
|
67
|
+
Rails style CRUD methods:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
|
71
|
+
index 'users' do
|
72
|
+
'Get all users'
|
73
|
+
end
|
74
|
+
|
75
|
+
show 'user' do
|
76
|
+
'Get a user'
|
77
|
+
end
|
78
|
+
|
79
|
+
update 'user' do
|
80
|
+
'Patch a user'
|
81
|
+
end
|
82
|
+
|
83
|
+
create 'user' do
|
84
|
+
'Post a new user'
|
85
|
+
end
|
86
|
+
|
87
|
+
Delete 'user' do
|
88
|
+
'Delete a user'
|
89
|
+
end
|
90
|
+
|
91
|
+
```
|
92
|
+
|
93
|
+
Rails style resources:
|
94
|
+
Declaring a route with resources defines a route in Sapp::RouteMap.routes.
|
95
|
+
However the Proc handling the response is empty.
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
|
99
|
+
resources 'user'
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
To update the response add CRUD methods.
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
|
107
|
+
resources 'user'
|
108
|
+
|
109
|
+
index 'users' do
|
110
|
+
'All users'
|
111
|
+
end
|
112
|
+
|
113
|
+
```
|
114
|
+
|
115
|
+
Direct Mapping:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
|
119
|
+
route 'GET', '/users' do
|
120
|
+
'Get all users'
|
121
|
+
end
|
122
|
+
|
123
|
+
add 'GET', '/users' do
|
124
|
+
`Get all users`
|
125
|
+
end
|
126
|
+
|
127
|
+
```
|
128
|
+
|
129
|
+
Name spacing and opinionated nesting
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
|
133
|
+
namespace 'user'
|
134
|
+
|
135
|
+
# '/user/:id'
|
136
|
+
|
137
|
+
get '/:id' do
|
138
|
+
|
139
|
+
'One User'
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
# /user/posts/:id
|
144
|
+
|
145
|
+
namespace 'posts', nested: true
|
146
|
+
|
147
|
+
get '/:id' do
|
148
|
+
|
149
|
+
'One Post'
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
```
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
### Response Body
|
158
|
+
The response may be a **String**, **Hash**, or **Array**
|
159
|
+
|
160
|
+
- String: returns string.
|
161
|
+
- Hash: returns Json object.
|
162
|
+
- Array: returns array *must be a rack tuple*
|
163
|
+
|
164
|
+
**Rack tuple:**
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
|
168
|
+
[status, {headers}, [ body ]]
|
169
|
+
|
170
|
+
```
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
|
174
|
+
get '/users' do
|
175
|
+
"Get all users"
|
176
|
+
end
|
177
|
+
|
178
|
+
# Returns: [ 200, {}, [ 'Get all users' ]]
|
179
|
+
|
180
|
+
get '/users' do
|
181
|
+
[ 200, {}, ['Get all users' ]]
|
182
|
+
end
|
183
|
+
|
184
|
+
# Returns: [ 200, {}, [ 'Get all users' ]]
|
185
|
+
|
186
|
+
get '/users' do
|
187
|
+
{ name: frank, height: 6.1 }
|
188
|
+
end
|
189
|
+
|
190
|
+
# Returns: [ 200, {}, [ "{\"name\":\"frank\",\"height\":6.1}" ]]
|
191
|
+
|
192
|
+
```
|
193
|
+
|
194
|
+
### Set status:
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
|
198
|
+
get '/users' do
|
199
|
+
set_status 200
|
200
|
+
end
|
201
|
+
|
202
|
+
```
|
203
|
+
|
204
|
+
### Params
|
205
|
+
Params is a Hash storing body params and url symbol values.
|
206
|
+
It serves the same purpose regardless of the request method.
|
207
|
+
Access params normally.
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
|
211
|
+
# Expects body params { name: 'a name', height: 6.1 }
|
212
|
+
post '/user', do
|
213
|
+
name = params[:name]
|
214
|
+
height = params[:height]
|
215
|
+
end
|
216
|
+
|
217
|
+
get '/user/:id', do
|
218
|
+
id = params[:id]
|
219
|
+
end
|
220
|
+
|
221
|
+
```
|
222
|
+
|
223
|
+
### Multiple Applications
|
224
|
+
|
225
|
+
If you intend on using multiple Controllers for routing use the Sapp::Router.
|
226
|
+
Initialize Sapp::Router in config.ru or in a seperate file that represents your
|
227
|
+
routes.
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
|
231
|
+
# users.rb
|
232
|
+
|
233
|
+
require 'sapp'
|
234
|
+
|
235
|
+
class Users < Sapp::Base
|
236
|
+
|
237
|
+
resources 'users'
|
238
|
+
|
239
|
+
index 'users' do
|
240
|
+
|
241
|
+
'All Users'
|
242
|
+
|
243
|
+
end
|
244
|
+
|
245
|
+
end
|
246
|
+
|
247
|
+
# posts.rb
|
248
|
+
|
249
|
+
class Posts < Sapp::Base
|
250
|
+
|
251
|
+
resources 'post'
|
252
|
+
|
253
|
+
index 'posts' do
|
254
|
+
|
255
|
+
'All Posts'
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
end
|
260
|
+
|
261
|
+
end
|
262
|
+
|
263
|
+
#config.ru
|
264
|
+
|
265
|
+
use RackcommonLogger
|
266
|
+
|
267
|
+
app = Sapp::Router.new do
|
268
|
+
|
269
|
+
create Users
|
270
|
+
|
271
|
+
create Posts
|
272
|
+
|
273
|
+
end
|
274
|
+
|
275
|
+
run app
|
276
|
+
|
277
|
+
```
|
278
|
+
|
279
|
+
## Development
|
280
|
+
|
281
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
282
|
+
|
283
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
284
|
+
|
285
|
+
## Contributing
|
286
|
+
|
287
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sapp. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
288
|
+
|
289
|
+
## License
|
290
|
+
|
291
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
292
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sapp"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/sapp/base.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require_relative 'router'
|
2
|
+
require_relative 'routes'
|
3
|
+
require_relative 'response'
|
4
|
+
require_relative 'handler'
|
5
|
+
require_relative 'resources'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
require File.expand_path('../', __FILE__) + '/path/request'
|
9
|
+
|
10
|
+
module Sapp
|
11
|
+
# Order of routing operations.
|
12
|
+
# 1. Create Request.
|
13
|
+
# 2. Parse request path.
|
14
|
+
# 3. If path found unwrap handler in context of params and status.
|
15
|
+
# 4. If path not found return a 404.
|
16
|
+
class Base
|
17
|
+
# All RESTful verb methods in Sapp::Routes
|
18
|
+
extend Routes
|
19
|
+
|
20
|
+
# Support for Rails like resources with and CRUD methods
|
21
|
+
extend Resources
|
22
|
+
|
23
|
+
# Single Application
|
24
|
+
def self.call env
|
25
|
+
req = Rack::Request.new env
|
26
|
+
req_path = create_path req.path, req.request_method, routes
|
27
|
+
|
28
|
+
if req_path.path?
|
29
|
+
find_path req_path, req
|
30
|
+
else
|
31
|
+
not_found! req_path.verb, req_path.original
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
# Multiple applications to be used with router
|
37
|
+
def self.run req
|
38
|
+
req_path = create_path req.path, req.request_method, routes
|
39
|
+
|
40
|
+
if req_path.path?
|
41
|
+
find_path req_path, req
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.find_path req_path, req
|
46
|
+
handler = Sapp::Handler.new req_path.handler, req, req_path.keys
|
47
|
+
unwrapped = handler.unwrap
|
48
|
+
response = Sapp::Response.new handler.status, unwrapped
|
49
|
+
|
50
|
+
response.process_handler
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.create_path path, method, routes
|
54
|
+
req_path = Sapp::Path::Request.new path, method, routes
|
55
|
+
req_path.parse
|
56
|
+
req_path
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
data/lib/sapp/handler.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module Sapp
|
2
|
+
|
3
|
+
# In charge of checking for a route match and
|
4
|
+
# setting the status, contains the unwrap method
|
5
|
+
# to call the handler proc in the context of this class.
|
6
|
+
# We need access to #params and #status
|
7
|
+
class Handler
|
8
|
+
|
9
|
+
def initialize handler, request, keys
|
10
|
+
@handler = handler
|
11
|
+
@request = request
|
12
|
+
@keys = keys
|
13
|
+
end
|
14
|
+
|
15
|
+
def unwrap
|
16
|
+
add_keys_to_params
|
17
|
+
instance_eval(&handler)
|
18
|
+
end
|
19
|
+
|
20
|
+
def status
|
21
|
+
@status
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def add_keys_to_params
|
27
|
+
params.merge! keys
|
28
|
+
end
|
29
|
+
|
30
|
+
def params
|
31
|
+
@request.params
|
32
|
+
end
|
33
|
+
|
34
|
+
def set_status code
|
35
|
+
@status = code
|
36
|
+
end
|
37
|
+
|
38
|
+
def handler
|
39
|
+
@handler
|
40
|
+
end
|
41
|
+
|
42
|
+
def keys
|
43
|
+
@keys
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
module Sapp
|
2
|
+
module Path
|
3
|
+
# An Object that builds a path structure for matching. Is used during
|
4
|
+
# the addition of routes.
|
5
|
+
class Base
|
6
|
+
attr_reader :original, :keys, :paths, :stream, :controller, :options
|
7
|
+
|
8
|
+
def initialize path, options: {}
|
9
|
+
@original = set_original options, path
|
10
|
+
@options = options
|
11
|
+
@keys = Hash.new
|
12
|
+
@paths = Hash.new
|
13
|
+
@stream = Array.new
|
14
|
+
@counter = 0
|
15
|
+
end
|
16
|
+
|
17
|
+
def parse
|
18
|
+
extract_keys_and_paths
|
19
|
+
set_controller
|
20
|
+
path = create_path
|
21
|
+
|
22
|
+
options? ? path.merge(options) : path
|
23
|
+
end
|
24
|
+
|
25
|
+
def set_original options, path
|
26
|
+
namespaces = options[:namespaces]
|
27
|
+
|
28
|
+
begin
|
29
|
+
if nested_deeply? namespaces
|
30
|
+
raise ArgumentError, "Routes nested too deeply"
|
31
|
+
else
|
32
|
+
concat_namespace_and_path path, namespaces
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def nested_deeply? namespaces
|
38
|
+
namespaces? namespaces && namespaces.count > 2
|
39
|
+
end
|
40
|
+
|
41
|
+
def concat_namespace_and_path path, namespaces
|
42
|
+
if namespaces? namespaces
|
43
|
+
x = namespace_to_path namespaces[0]
|
44
|
+
y = nested?(namespaces) ? namespace_to_path(namespaces[1]) : ""
|
45
|
+
|
46
|
+
x + path + y
|
47
|
+
else
|
48
|
+
path
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def namespace_to_path namespaces
|
53
|
+
'/' + namespaces.join('/')
|
54
|
+
end
|
55
|
+
|
56
|
+
def options?
|
57
|
+
options.any?
|
58
|
+
end
|
59
|
+
|
60
|
+
def nested? namespaces
|
61
|
+
namespaces[1]
|
62
|
+
end
|
63
|
+
|
64
|
+
def namespaces? namespaces
|
65
|
+
namespaces && namespaces.any?
|
66
|
+
end
|
67
|
+
|
68
|
+
def namespaces
|
69
|
+
options[:namespaces]
|
70
|
+
end
|
71
|
+
|
72
|
+
def setup_extraction
|
73
|
+
path = original.split('/')
|
74
|
+
path.delete("")
|
75
|
+
path
|
76
|
+
end
|
77
|
+
|
78
|
+
def extract_keys_and_paths
|
79
|
+
setup_extraction.each do |values|
|
80
|
+
extract_keys values
|
81
|
+
extract_paths values
|
82
|
+
count
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def extract_keys key
|
87
|
+
if key.match(/\A:/)
|
88
|
+
stream << 0
|
89
|
+
keys[counter] = key
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def extract_paths path
|
94
|
+
unless path.match(/\A:/)
|
95
|
+
stream << 1
|
96
|
+
paths[counter] = path
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def create_path
|
101
|
+
{
|
102
|
+
controller: @controller,
|
103
|
+
stream: stream,
|
104
|
+
keys: keys,
|
105
|
+
methods: paths,
|
106
|
+
original: original,
|
107
|
+
}
|
108
|
+
end
|
109
|
+
|
110
|
+
def counter
|
111
|
+
@counter
|
112
|
+
end
|
113
|
+
|
114
|
+
private
|
115
|
+
|
116
|
+
def set_controller
|
117
|
+
if @controller = paths[0]
|
118
|
+
paths.delete 0
|
119
|
+
else
|
120
|
+
raise ArgumentError, "A Path can't begin with a symbol"
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def count
|
125
|
+
@counter += 1
|
126
|
+
end
|
127
|
+
|
128
|
+
def reset_counter
|
129
|
+
@counter = 0
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require_relative 'base'
|
2
|
+
|
3
|
+
module Sapp
|
4
|
+
module Path
|
5
|
+
class Request < Base
|
6
|
+
|
7
|
+
attr_reader :routes, :verb, :handler, :keys, :path
|
8
|
+
|
9
|
+
def initialize original, verb, routes
|
10
|
+
@original = original
|
11
|
+
@verb = verb
|
12
|
+
@routes = routes
|
13
|
+
@keys = Hash.new
|
14
|
+
@handler = nil
|
15
|
+
end
|
16
|
+
|
17
|
+
# Sets controller, path keys and handler
|
18
|
+
def parse
|
19
|
+
set_controller
|
20
|
+
|
21
|
+
if path?
|
22
|
+
@path = find_path.uniq.first
|
23
|
+
@keys = extract_keys
|
24
|
+
@handler = @path[:handler]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns hash of matched paths by stream count
|
29
|
+
def match_by_stream_count
|
30
|
+
paths.select { |p| check_stream? p, sort_path }
|
31
|
+
end
|
32
|
+
|
33
|
+
# Returns hash of matched paths by stream count and method name
|
34
|
+
def match_by_methods_and_stream
|
35
|
+
match_by_stream_count.select { |p| check_methods? p, sort_path }
|
36
|
+
end
|
37
|
+
|
38
|
+
# Matches by method names and stream count, returns best match
|
39
|
+
def find_path
|
40
|
+
match_by_methods_and_stream
|
41
|
+
end
|
42
|
+
|
43
|
+
# Return paths from request controller
|
44
|
+
def paths
|
45
|
+
find_controller[:paths]
|
46
|
+
end
|
47
|
+
|
48
|
+
# Parses path by '/' and creates a numbered dictionary EX: { 0 => 'user' }
|
49
|
+
def sort_path
|
50
|
+
reset_counter
|
51
|
+
array = Array.new
|
52
|
+
|
53
|
+
setup_extraction.each do |v|
|
54
|
+
array << [counter, v]
|
55
|
+
count
|
56
|
+
end
|
57
|
+
|
58
|
+
@sort_path ||= Hash[array]
|
59
|
+
end
|
60
|
+
|
61
|
+
# Remove the first key:value(controller) return a hash with keys from path
|
62
|
+
def extract_keys
|
63
|
+
hash = Hash.new
|
64
|
+
|
65
|
+
sort_path.each do |k, v|
|
66
|
+
if path[:keys].include?(k) && k > 0
|
67
|
+
hash[extract_key(k)] = v
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
hash
|
72
|
+
end
|
73
|
+
|
74
|
+
def key? k
|
75
|
+
k > 0
|
76
|
+
end
|
77
|
+
|
78
|
+
def extract_key k
|
79
|
+
key = path[:keys][k]
|
80
|
+
key.tr(':', '').to_sym
|
81
|
+
end
|
82
|
+
|
83
|
+
def find_controller
|
84
|
+
routes[verb][controller]
|
85
|
+
end
|
86
|
+
|
87
|
+
def check_stream? p, hash
|
88
|
+
p[:stream].count == hash.values.count
|
89
|
+
end
|
90
|
+
|
91
|
+
def check_methods? p, hash
|
92
|
+
(p[:methods].values - hash.values).empty?
|
93
|
+
end
|
94
|
+
|
95
|
+
# Sets the controller based off the first key:value pair
|
96
|
+
def set_controller
|
97
|
+
@controller = sort_path[0]
|
98
|
+
end
|
99
|
+
|
100
|
+
# Returns boolean, is handler defined?
|
101
|
+
def path?
|
102
|
+
find_controller && find_path.any?
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Sapp
|
2
|
+
# A mixin for Sapp::Base. Provides Rails like resource
|
3
|
+
# methods for creating routes
|
4
|
+
module Resources
|
5
|
+
|
6
|
+
def resources name
|
7
|
+
add "GET", "/#{name}s"
|
8
|
+
add "GET", "/#{name}/:id"
|
9
|
+
add "POST", "/#{name}"
|
10
|
+
add "PATCH", "/#{name}/:id"
|
11
|
+
add "DELETE", "/#{name}/:id"
|
12
|
+
end
|
13
|
+
|
14
|
+
def index name, &block
|
15
|
+
add "GET", "/#{name}", &block
|
16
|
+
end
|
17
|
+
|
18
|
+
def show name, &block
|
19
|
+
add "GET", "/#{name}/:id", &block
|
20
|
+
end
|
21
|
+
|
22
|
+
def update name, &block
|
23
|
+
add "PATCH", "/#{name}/:id", &block
|
24
|
+
end
|
25
|
+
|
26
|
+
def create name, &block
|
27
|
+
add "POST", "/#{name}", &block
|
28
|
+
end
|
29
|
+
|
30
|
+
def destroy name, &block
|
31
|
+
add "DELETE", "/#{name}", &block
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Sapp
|
2
|
+
|
3
|
+
# In charge of creating the tuple
|
4
|
+
# deciding Content-Type and returning status.
|
5
|
+
class Response
|
6
|
+
|
7
|
+
attr_reader :handler, :status, :headers
|
8
|
+
|
9
|
+
def initialize status, handler
|
10
|
+
@status = status
|
11
|
+
@handler = handler
|
12
|
+
@headers = Hash.new
|
13
|
+
end
|
14
|
+
|
15
|
+
# With defaults
|
16
|
+
def process_handler
|
17
|
+
case handler
|
18
|
+
when String
|
19
|
+
create_tuple 200, handler
|
20
|
+
when Array
|
21
|
+
handler
|
22
|
+
when Hash
|
23
|
+
add_header 'Content-Type', 'application/json'
|
24
|
+
create_tuple 200, handler.to_json
|
25
|
+
else
|
26
|
+
[500, {}, ["response must be a string, tuple, or hash"]]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def add_header key, value
|
33
|
+
@headers[key] = value
|
34
|
+
end
|
35
|
+
|
36
|
+
# If status at initialization use, else use default
|
37
|
+
def create_tuple default_status, body
|
38
|
+
[status ? status : default_status, headers, [body] ]
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require File.expand_path('../', __FILE__) + '/path/base'
|
2
|
+
|
3
|
+
module Sapp
|
4
|
+
class RouteMap
|
5
|
+
|
6
|
+
attr_reader :routes
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@routes = Hash.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def namespaces
|
13
|
+
@namespaces ||= Array.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_namespace names, nest
|
17
|
+
if nest && namespaces.any?
|
18
|
+
@namespaces = namespaces << names
|
19
|
+
else
|
20
|
+
@namespaces = [names]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def empty_proc
|
25
|
+
@empty_proc ||= Proc.new { "Placeholder" }
|
26
|
+
end
|
27
|
+
|
28
|
+
def options
|
29
|
+
options = Hash.new
|
30
|
+
options[:namespaces] = namespaces if namespaces
|
31
|
+
options
|
32
|
+
end
|
33
|
+
|
34
|
+
def add verb, path, &handler
|
35
|
+
path_hash = Sapp::Path::Base.new(path, options: options ).parse
|
36
|
+
|
37
|
+
verbs = get_or_create_verb verb
|
38
|
+
controller = get_or_create_controller path_hash, verbs
|
39
|
+
|
40
|
+
if path_exist? controller, path_hash[:original]
|
41
|
+
update_path controller, path_hash, handler
|
42
|
+
else
|
43
|
+
create_set controller, path_hash, &handler
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
def update_path controller, path_hash, handler
|
49
|
+
map = controller[:paths].collect do |path|
|
50
|
+
if path[:original] == path_hash[:original]
|
51
|
+
add_handler(path_hash, handler)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
controller[:paths] = map
|
55
|
+
end
|
56
|
+
|
57
|
+
def path_exist? controller, path
|
58
|
+
controller.any? && controller[:index].include?(path)
|
59
|
+
end
|
60
|
+
|
61
|
+
def create_set controller, path_hash, &handler
|
62
|
+
get_or_create_paths controller
|
63
|
+
get_or_create_index controller
|
64
|
+
add_path path_hash, controller, &handler
|
65
|
+
add_path_to_index controller, path_hash[:original]
|
66
|
+
end
|
67
|
+
|
68
|
+
def add_path_to_index controller, path
|
69
|
+
controller[:index] << path
|
70
|
+
end
|
71
|
+
|
72
|
+
def get_or_create_verb verb
|
73
|
+
routes[verb] ||= Hash.new
|
74
|
+
end
|
75
|
+
|
76
|
+
def get_or_create_controller path_hash, verbs
|
77
|
+
controller = path_hash[:controller]
|
78
|
+
verbs[controller] ||= Hash.new
|
79
|
+
end
|
80
|
+
|
81
|
+
def get_or_create_paths controller
|
82
|
+
controller[:paths] ||= Array.new
|
83
|
+
end
|
84
|
+
|
85
|
+
def get_or_create_index controller
|
86
|
+
controller[:index] ||= Array.new
|
87
|
+
end
|
88
|
+
|
89
|
+
def add_stream path_hash, controller
|
90
|
+
controller[:streams] << path_hash[:stream]
|
91
|
+
end
|
92
|
+
|
93
|
+
def add_path path_hash, controller, &handler
|
94
|
+
if block_given?
|
95
|
+
controller[:paths] << add_handler(path_hash, handler)
|
96
|
+
else
|
97
|
+
controller[:paths] << add_handler(path_hash, empty_proc)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def add_handler path_hash, handler
|
102
|
+
path_hash.merge handler: handler
|
103
|
+
end
|
104
|
+
|
105
|
+
def remove verb, path=nil
|
106
|
+
if path
|
107
|
+
routes[verb].delete path
|
108
|
+
else
|
109
|
+
routes.delete verb
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
data/lib/sapp/router.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
module Sapp
|
2
|
+
class Router
|
3
|
+
|
4
|
+
def initialize &block
|
5
|
+
@apps = Array.new
|
6
|
+
instance_eval(&block) if block
|
7
|
+
end
|
8
|
+
|
9
|
+
def call env
|
10
|
+
request = Rack::Request.new env
|
11
|
+
found = Array.new
|
12
|
+
|
13
|
+
@apps.each do |a|
|
14
|
+
path = a.run(request)
|
15
|
+
found << path if path
|
16
|
+
end
|
17
|
+
|
18
|
+
begin
|
19
|
+
duplicate_apps! if found.count > 1
|
20
|
+
rescue => e
|
21
|
+
puts e.message
|
22
|
+
end
|
23
|
+
|
24
|
+
found.any? ? found.first : [ 404, {}, ["Not found"] ]
|
25
|
+
end
|
26
|
+
|
27
|
+
def apps
|
28
|
+
@apps
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def create app
|
33
|
+
@apps << app
|
34
|
+
end
|
35
|
+
|
36
|
+
def duplicate_apps!
|
37
|
+
raise %[
|
38
|
+
It seems you have multiple applications, with duplicate
|
39
|
+
routes.
|
40
|
+
|
41
|
+
--> Apps: #{apps.join(', ')}
|
42
|
+
|
43
|
+
Check those classes, and remove duplicate routes.
|
44
|
+
]
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
data/lib/sapp/routes.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require_relative 'route_map'
|
2
|
+
|
3
|
+
module Sapp
|
4
|
+
|
5
|
+
# Use exclusively as a mixin for Base
|
6
|
+
module Routes
|
7
|
+
|
8
|
+
|
9
|
+
def route_map
|
10
|
+
@route_map ||= RouteMap.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def namespace *names, nest: false
|
14
|
+
route_map.set_namespace names, nest
|
15
|
+
end
|
16
|
+
|
17
|
+
def routes
|
18
|
+
route_map.routes
|
19
|
+
end
|
20
|
+
|
21
|
+
def not_found! verb, path
|
22
|
+
[404, {}, ["Oops! No route for #{verb} #{path}"]]
|
23
|
+
end
|
24
|
+
|
25
|
+
def add verb, path, &handler
|
26
|
+
route_map.add verb, path, &handler
|
27
|
+
end
|
28
|
+
|
29
|
+
def get path, &handler
|
30
|
+
add "GET", path, &handler
|
31
|
+
end
|
32
|
+
|
33
|
+
def post path, &handler
|
34
|
+
add "POST", path, &handler
|
35
|
+
end
|
36
|
+
|
37
|
+
def put path, &handler
|
38
|
+
add "PUT", path, &handler
|
39
|
+
end
|
40
|
+
|
41
|
+
def patch path, &handler
|
42
|
+
add "PATCH", path, &handler
|
43
|
+
end
|
44
|
+
|
45
|
+
def delete path, &handler
|
46
|
+
add "DELETE", path, &handler
|
47
|
+
end
|
48
|
+
|
49
|
+
def head path, &handler
|
50
|
+
add "HEAD", path, &handler
|
51
|
+
end
|
52
|
+
|
53
|
+
alias_method :route, :add
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
data/lib/sapp/version.rb
ADDED
data/lib/sapp.rb
ADDED
data/sapp.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sapp/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sapp"
|
8
|
+
spec.version = Sapp::VERSION
|
9
|
+
spec.authors = ["hayduke19us"]
|
10
|
+
spec.email = ["hayduke19us@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simple application framework for Rack.}
|
13
|
+
spec.description = %q{ Supports Sinatra like blocks and Rails like
|
14
|
+
resources.}
|
15
|
+
spec.homepage = "http://github.com/haydukeus/sapp"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "minitest"
|
26
|
+
spec.add_development_dependency "minitest-focus"
|
27
|
+
spec.add_development_dependency "rack-test"
|
28
|
+
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sapp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hayduke19us
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest-focus
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack-test
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: |2-
|
84
|
+
Supports Sinatra like blocks and Rails like
|
85
|
+
resources.
|
86
|
+
email:
|
87
|
+
- hayduke19us@gmail.com
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- ".travis.yml"
|
94
|
+
- CODE_OF_CONDUCT.md
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- lib/sapp.rb
|
102
|
+
- lib/sapp/base.rb
|
103
|
+
- lib/sapp/handler.rb
|
104
|
+
- lib/sapp/path/base.rb
|
105
|
+
- lib/sapp/path/request.rb
|
106
|
+
- lib/sapp/resources.rb
|
107
|
+
- lib/sapp/response.rb
|
108
|
+
- lib/sapp/route_map.rb
|
109
|
+
- lib/sapp/router.rb
|
110
|
+
- lib/sapp/routes.rb
|
111
|
+
- lib/sapp/version.rb
|
112
|
+
- sapp.gemspec
|
113
|
+
homepage: http://github.com/haydukeus/sapp
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata: {}
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.4.5
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: A simple application framework for Rack.
|
137
|
+
test_files: []
|