navigable-server 0.5.0 → 0.6.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
  SHA256:
3
- metadata.gz: 68e55e159229ccccc9d9054c051e9fdcd1edeb0a4919df0349afd1fa6950da7f
4
- data.tar.gz: a86086f6f97ec97c1f072cc5538d60a7573ca27e913feb9704c3f25bf9eff4d4
3
+ metadata.gz: 5938d19f2fef581f5329cb345a2141b9994d13de64a04a364a273095b52f3001
4
+ data.tar.gz: 8463bc00fa4035f7d4d31212eef94c34c2bad9d21649cec59983e709e19a3651
5
5
  SHA512:
6
- metadata.gz: ade898aefa8686fe83f748f3493b71b5acc578b99324a5abb1251943dc73ac61d43400c6d8bbe7dbfb454a1a58854f2c69bbdad54d313acc72a17210fee22396
7
- data.tar.gz: 0adfc4624b53cb5a3f172f8bf3700a713616469813747479743d01f084ff808ff95ac6be7a8ea3aee9b4bdaaead139078097dcfa868a7afb3c4ba0f7bccba9ea
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><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
- If you are considering creating a JSON API with `Navigable::Server`, you should know about `Navigable::API` and `Navigable::GraphQL`. Both of these gems extend `Navigable::Server` in ways that bring all of Navigable together from `Commands` and `Observers`, to `Endpoints` and `Resolvers`.
127
+ Alternatively, you can declare that your endpoint executes a specific Navigable command by calling the `executes` method, like this:
137
128
 
138
- ## Development
129
+ ```ruby
130
+ class RecruitSwabbieEndpoint
131
+ extend Navigable::Server::Endpoint
139
132
 
140
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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
- 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).
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.instance_eval do
10
- def responds_to(verb, path)
11
- Navigable::Server.add_endpoint(verb: verb, path: path, endpoint_class: self)
12
- end
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
- def executes(command_key)
15
- @command_key = command_key
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
- base.class_eval do
20
- attr_reader :request
28
+ def inject(request: Request.new)
29
+ @request = request
30
+ end
21
31
 
22
- def inject(request: Request.new)
23
- @request = request
24
- end
32
+ def execute
33
+ raise NotImplementedError.new(EXECUTE_NOT_IMPLEMENTED_MESSAGE) unless command_key
25
34
 
26
- def execute
27
- raise NotImplementedError.new(EXECUTE_NOT_IMPLEMENTED_MESSAGE) unless command_key
35
+ return unauthenticated unless authenticated?
36
+ return unauthorized unless authorized?
28
37
 
29
- dispatch
30
- end
38
+ dispatch
39
+ end
31
40
 
32
- private
41
+ private
33
42
 
34
- def dispatch
35
- Navigable::Dispatcher.dispatch(command_key, params: params, resolver: resolver)
36
- end
43
+ def dispatch
44
+ Navigable::Dispatcher.dispatch(command_key, params: params, resolver: resolver)
45
+ end
37
46
 
38
- def command_key
39
- self.class.instance_variable_get(:@command_key)
40
- end
47
+ def command_key
48
+ self.class.instance_variable_get(:@command_key)
49
+ end
41
50
 
42
- def params
43
- request.params
44
- end
51
+ def params
52
+ request.params
53
+ end
45
54
 
46
- def preferred_media_type
47
- request.headers[:preferred_media_type]
48
- end
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
- def resolver
51
- Manufacturable.build_one(Resolver::TYPE, preferred_media_type) || Navigable::NullResolver.new
52
- end
75
+ def authorized?
76
+ true
53
77
  end
54
78
  end
55
79
  end
@@ -38,7 +38,7 @@ module Navigable
38
38
  end
39
39
 
40
40
  def content_length
41
- content.bytesize
41
+ content.bytesize.to_s
42
42
  end
43
43
 
44
44
  def content
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Navigable
4
4
  module Server
5
- VERSION = "0.5.0"
5
+ VERSION = "0.6.0"
6
6
  end
7
7
  end
@@ -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.3'
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.5.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-09-29 00:00:00.000000000 Z
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.3'
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.3'
41
+ version: '1.5'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: navigable-router
44
44
  requirement: !ruby/object:Gem::Requirement