navigable-server 0.5.0 → 0.6.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 +4 -4
- data/README.md +71 -16
- data/lib/navigable/server/endpoint.rb +56 -32
- data/lib/navigable/server/response.rb +1 -1
- data/lib/navigable/server/version.rb +1 -1
- data/navigable-server.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5938d19f2fef581f5329cb345a2141b9994d13de64a04a364a273095b52f3001
|
4
|
+
data.tar.gz: 8463bc00fa4035f7d4d31212eef94c34c2bad9d21649cec59983e709e19a3651
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a79b3ed8a998efde770f54d2352774a2365ca9213ff49520810486e225f2b9a9fa6c8ce07ea5a18575fc580b620834aab1f319f44302ee960bff72bfb8a4665b
|
7
|
+
data.tar.gz: 78299c73d9e5fe6c941dfd3d85e8e1f44f8d932e9b3b969092a1f234fcf67098d3d0ea132ff337420ad36595607044238a7c9f695ca403731aa868e5093dc6e1
|
data/README.md
CHANGED
@@ -30,29 +30,20 @@ A simple, highly-performant, Rack-based router.
|
|
30
30
|
**[Navigable Server][server]** *(coming soon)*<br>
|
31
31
|
A Rack-based server for building Ruby and Navigable web applications.
|
32
32
|
|
33
|
-
</td>
|
34
|
-
</tr>
|
35
|
-
<tr height="140">
|
36
|
-
<td width="130"><img alt="Telescope" src="https://raw.githubusercontent.com/first-try-software/navigable/main/assets/telescope.png"></td>
|
37
|
-
<td>
|
38
|
-
|
39
|
-
**Navigable API** *(coming soon)*<br>
|
40
|
-
An extension of Navigable Server for building restful JSON APIs.
|
41
|
-
|
42
33
|
</td>
|
43
34
|
</tr>
|
44
35
|
<tr height="140">
|
45
36
|
<td width="130"><img alt="Map" src="https://raw.githubusercontent.com/first-try-software/navigable/main/assets/map.png"></td>
|
46
37
|
<td>
|
47
38
|
|
48
|
-
**Navigable GraphQL** *(coming soon)*<br>
|
39
|
+
**[Navigable GraphQL][graphql]** *(coming soon)*<br>
|
49
40
|
An extension of Navigable Server for building GraphQL APIs.
|
50
41
|
|
51
42
|
</td>
|
52
43
|
</tr>
|
53
44
|
</table>
|
54
45
|
|
55
|
-
<br
|
46
|
+
<br>
|
56
47
|
|
57
48
|
## Installation
|
58
49
|
|
@@ -133,19 +124,81 @@ class ShowTreasureMapEndpoint
|
|
133
124
|
end
|
134
125
|
end
|
135
126
|
```
|
136
|
-
|
127
|
+
Alternatively, you can declare that your endpoint executes a specific Navigable command by calling the `executes` method, like this:
|
137
128
|
|
138
|
-
|
129
|
+
```ruby
|
130
|
+
class RecruitSwabbieEndpoint
|
131
|
+
extend Navigable::Server::Endpoint
|
139
132
|
|
140
|
-
|
133
|
+
responds_to :get, '/ahoy'
|
134
|
+
executes :recruit_swabbie
|
135
|
+
end
|
136
|
+
```
|
137
|
+
This tells the server to automatically execute the command associated with the key `:recruit_swabbie`. The command might look something like this:
|
141
138
|
|
142
|
-
|
139
|
+
```ruby
|
140
|
+
class RecruitSwabbie
|
141
|
+
extend Navigable::Command
|
142
|
+
|
143
|
+
corresponds_to :recruit_swabbie
|
144
|
+
|
145
|
+
def execute
|
146
|
+
return failed(recruit) unless swabbie_material?
|
147
|
+
|
148
|
+
successfully recruited_swabbie
|
149
|
+
end
|
150
|
+
|
151
|
+
private
|
152
|
+
|
153
|
+
def recruit
|
154
|
+
Swabbie.new(params)
|
155
|
+
end
|
156
|
+
|
157
|
+
def swabbie_material?
|
158
|
+
!recruit.drunk? && !recruit.pirate? && !recruit.seasick?
|
159
|
+
end
|
160
|
+
|
161
|
+
def recruited_swabbie
|
162
|
+
SwabbieRepository.create(recruit)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
```
|
166
|
+
Finally, you can use a Resolver class to handle requests for specific MIME types. Here's a `JSONResolver` class that prepares the data from the command to be returned as JSON:
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
class JSONResolver
|
170
|
+
extend Navigable::Resolver
|
171
|
+
|
172
|
+
resolves 'application/json'
|
173
|
+
|
174
|
+
def resolve
|
175
|
+
@response
|
176
|
+
end
|
177
|
+
|
178
|
+
def on_success(recruit)
|
179
|
+
@response = { json: recruit }
|
180
|
+
end
|
181
|
+
|
182
|
+
def on_failure(recruit)
|
183
|
+
@response = { json: { errors: errors(recruit) } }
|
184
|
+
end
|
185
|
+
|
186
|
+
private
|
187
|
+
|
188
|
+
def errors(recruit)
|
189
|
+
errors = []
|
190
|
+
errors << 'They are a drunken mess!' if recruit.drunk?
|
191
|
+
errors << 'They are wanted for piracy on three continents!' if recruit.pirate?
|
192
|
+
errors << 'One step aboard and they turned blue and tossed!' if recruit.seasick?
|
193
|
+
end
|
194
|
+
end
|
195
|
+
```
|
196
|
+
Visit the Navigable Wiki for more information on [Resolvers][resolvers].
|
143
197
|
|
144
198
|
## Contributing
|
145
199
|
|
146
200
|
Bug reports and pull requests are welcome on GitHub at https://github.com/first-try-software/navigable-server. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/first-try-software/navigable-server/blob/master/CODE_OF_CONDUCT.md).
|
147
201
|
|
148
|
-
|
149
202
|
## License
|
150
203
|
|
151
204
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -157,3 +210,5 @@ Everyone interacting in the Navigable::Server project's codebases, issue tracker
|
|
157
210
|
[navigable]: https://github.com/first-try-software/navigable
|
158
211
|
[router]: https://github.com/first-try-software/navigable-router
|
159
212
|
[server]: https://github.com/first-try-software/navigable-server
|
213
|
+
[resolvers]: https://github.com/first-try-software/navigable/wiki/Resolvers
|
214
|
+
[graphql]: https://github.com/first-try-software/navigable-graphql
|
@@ -4,52 +4,76 @@ module Navigable
|
|
4
4
|
module Server
|
5
5
|
module Endpoint
|
6
6
|
EXECUTE_NOT_IMPLEMENTED_MESSAGE = 'Endpoint classes must either call `executes` or implement an `execute` method.'
|
7
|
+
UNAUTHENTICATED = { status: 401, text: 'Unauthorized' }.freeze
|
8
|
+
UNAUTHORIZED = { status: 403, text: 'Forbidden' }.freeze
|
7
9
|
|
8
10
|
def self.extended(base)
|
9
|
-
base.
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
base.extend(ClassMethods)
|
12
|
+
base.include(InstanceMethods)
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
def responds_to(verb, path)
|
17
|
+
Navigable::Server.add_endpoint(verb: verb, path: path, endpoint_class: self)
|
18
|
+
end
|
13
19
|
|
14
|
-
|
15
|
-
|
16
|
-
end
|
20
|
+
def executes(command_key)
|
21
|
+
@command_key = command_key
|
17
22
|
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module InstanceMethods
|
26
|
+
attr_reader :request
|
18
27
|
|
19
|
-
|
20
|
-
|
28
|
+
def inject(request: Request.new)
|
29
|
+
@request = request
|
30
|
+
end
|
21
31
|
|
22
|
-
|
23
|
-
|
24
|
-
end
|
32
|
+
def execute
|
33
|
+
raise NotImplementedError.new(EXECUTE_NOT_IMPLEMENTED_MESSAGE) unless command_key
|
25
34
|
|
26
|
-
|
27
|
-
|
35
|
+
return unauthenticated unless authenticated?
|
36
|
+
return unauthorized unless authorized?
|
28
37
|
|
29
|
-
|
30
|
-
|
38
|
+
dispatch
|
39
|
+
end
|
31
40
|
|
32
|
-
|
41
|
+
private
|
33
42
|
|
34
|
-
|
35
|
-
|
36
|
-
|
43
|
+
def dispatch
|
44
|
+
Navigable::Dispatcher.dispatch(command_key, params: params, resolver: resolver)
|
45
|
+
end
|
37
46
|
|
38
|
-
|
39
|
-
|
40
|
-
|
47
|
+
def command_key
|
48
|
+
self.class.instance_variable_get(:@command_key)
|
49
|
+
end
|
41
50
|
|
42
|
-
|
43
|
-
|
44
|
-
|
51
|
+
def params
|
52
|
+
request.params
|
53
|
+
end
|
45
54
|
|
46
|
-
|
47
|
-
|
48
|
-
|
55
|
+
def preferred_media_type
|
56
|
+
request.headers[:preferred_media_type]
|
57
|
+
end
|
58
|
+
|
59
|
+
def resolver
|
60
|
+
Manufacturable.build_one(Resolver::TYPE, preferred_media_type) || Navigable::NullResolver.new
|
61
|
+
end
|
62
|
+
|
63
|
+
def unauthenticated
|
64
|
+
UNAUTHENTICATED
|
65
|
+
end
|
66
|
+
|
67
|
+
def unauthorized
|
68
|
+
UNAUTHORIZED
|
69
|
+
end
|
70
|
+
|
71
|
+
def authenticated?
|
72
|
+
true
|
73
|
+
end
|
49
74
|
|
50
|
-
|
51
|
-
|
52
|
-
end
|
75
|
+
def authorized?
|
76
|
+
true
|
53
77
|
end
|
54
78
|
end
|
55
79
|
end
|
data/navigable-server.gemspec
CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.require_paths = ["lib"]
|
25
25
|
|
26
26
|
spec.add_dependency 'json', '~> 2.3'
|
27
|
-
spec.add_dependency 'navigable', '~> 1.
|
27
|
+
spec.add_dependency 'navigable', '~> 1.5'
|
28
28
|
spec.add_dependency 'navigable-router', '~>0.2'
|
29
29
|
spec.add_dependency 'rack', '~> 2.2'
|
30
30
|
spec.add_dependency 'rack-abstract-format', '~> 0.9.9'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: navigable-server
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alan Ridlehoover
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2020-
|
12
|
+
date: 2020-12-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
@@ -31,14 +31,14 @@ dependencies:
|
|
31
31
|
requirements:
|
32
32
|
- - "~>"
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '1.
|
34
|
+
version: '1.5'
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
39
|
- - "~>"
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '1.
|
41
|
+
version: '1.5'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: navigable-router
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|