hub_kernel-api 0.2.0 → 0.4.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 +11 -0
- data/app/controllers/hub_kernel/api/hub_calls_controller.rb +12 -8
- data/app/controllers/hub_kernel/api/hub_controller.rb +17 -0
- data/app/controllers/hub_kernel/api/hubs_controller.rb +13 -0
- data/config/routes.rb +1 -0
- data/lib/hub_kernel/api/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e54d19b67798c82119ecff94f0f294353cd927da83a55282b2f61ffbf111061d
|
|
4
|
+
data.tar.gz: 1d48fc7eb9520cb6300315462a74159b68a0e236c797f87d97964c4455eefce1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fa3f514ecb094b92a30e80fc390414abf616e68c8b479485462d5f7298d77cab57f3d813ced143fbafc58330f8685a7cac2d94aad14693a006730ff0c0ba6060
|
|
7
|
+
data.tar.gz: 07c3ae8786feb3ad091474071d2dc7699ee8cc0b7ebb798f95cd53c7afaac53096e7df8d261bbd5c1e01ba4f13a613998b40cea868c4afccabcc12a0789c0fe9
|
data/README.md
CHANGED
|
@@ -38,11 +38,22 @@ A hub listed this way answers only at the name given, and the permission check i
|
|
|
38
38
|
asked about it by its own name, such as `ledger:record_spend`. A read answers GET and a write answers POST, and the answer is the method's
|
|
39
39
|
return value under `answer`.
|
|
40
40
|
|
|
41
|
+
A GET at a served hub's own address, `/<hub>`, lists the methods the caller may call on
|
|
42
|
+
the account the host names, each with its name, the values it takes and its verb:
|
|
43
|
+
|
|
44
|
+
```json
|
|
45
|
+
[{ "name": "price_of", "takes": ["item"], "verb": "GET" }]
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
A hub that is not served is answered as not found, and a caller the host's sign-in refuses
|
|
49
|
+
is shown nothing.
|
|
50
|
+
|
|
41
51
|
| Case | Status | Body |
|
|
42
52
|
| --- | --- | --- |
|
|
43
53
|
| The method answers | 200 | `{"answer": ...}` |
|
|
44
54
|
| The hub is not served, the method is not exposed, the verb is wrong, or the host's permission check refuses | 404 | `{"error": "Not found"}` |
|
|
45
55
|
| The hub refuses, or a required value is missing | 422 | `{"error": "<reason>"}` |
|
|
56
|
+
| A permitted call sends a value the method is not listed with | 422 | `{"error": "<method> does not take <values>"}` |
|
|
46
57
|
| A record the call names does not exist | 404 | `{"error": "No <record> has the id <id>"}` |
|
|
47
58
|
|
|
48
59
|
## Installation
|
|
@@ -1,29 +1,33 @@
|
|
|
1
1
|
module HubKernel
|
|
2
2
|
module Api
|
|
3
|
-
class HubCallsController <
|
|
4
|
-
|
|
3
|
+
class HubCallsController < HubController
|
|
4
|
+
wrap_parameters false
|
|
5
|
+
|
|
5
6
|
rescue_from HubKernel::Refused, HubKernel::MissingArgumentError, with: :refused
|
|
6
7
|
rescue_from ActiveRecord::RecordNotFound, with: :missing_record
|
|
7
8
|
|
|
8
9
|
def answer
|
|
9
10
|
raise ActionController::RoutingError, "Not found" unless asked_with_the_right_verb?
|
|
11
|
+
return refuse_unlisted_values if unlisted_values.any?
|
|
10
12
|
|
|
11
13
|
render json: { answer: hub.call_exposed(params[:name], values: values, person: caller_person, account: caller_account) }
|
|
12
14
|
end
|
|
13
15
|
|
|
14
16
|
private
|
|
15
17
|
|
|
16
|
-
def hub = @hub ||= HubKernel::Api.find(params[:hub]) || raise(ActionController::RoutingError, "Not found")
|
|
17
|
-
|
|
18
18
|
def asked_with_the_right_verb? = hub.exposed(params[:name])&.writes == request.post?
|
|
19
19
|
|
|
20
|
-
def
|
|
20
|
+
def refuse_unlisted_values
|
|
21
|
+
raise HubKernel::NotAllowed unless permitted?
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
refused(HubKernel::Refused.new("#{params[:name]} does not take #{unlisted_values.join(", ")}"))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def permitted? = hub.exposures_for(person: caller_person, account: caller_account).any? { |exposure| exposure.name.to_s == params[:name] }
|
|
23
27
|
|
|
24
|
-
def
|
|
28
|
+
def unlisted_values = values.keys - hub.exposed(params[:name]).takes
|
|
25
29
|
|
|
26
|
-
def
|
|
30
|
+
def values = request.query_parameters.merge(request.request_parameters).deep_symbolize_keys
|
|
27
31
|
|
|
28
32
|
def refused(refusal) = render(json: { error: refusal.message }, status: :unprocessable_content)
|
|
29
33
|
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module HubKernel
|
|
2
|
+
module Api
|
|
3
|
+
class HubController < HubKernel::Api.base_controller.constantize
|
|
4
|
+
rescue_from ActionController::RoutingError, HubKernel::NotAllowed, with: :not_found
|
|
5
|
+
|
|
6
|
+
private
|
|
7
|
+
|
|
8
|
+
def hub = @hub ||= HubKernel::Api.find(params[:hub]) || raise(ActionController::RoutingError, "Not found")
|
|
9
|
+
|
|
10
|
+
def caller_person = send(HubKernel::Api.person_method)
|
|
11
|
+
|
|
12
|
+
def caller_account = send(HubKernel::Api.account_method)
|
|
13
|
+
|
|
14
|
+
def not_found = render(json: { error: "Not found" }, status: :not_found)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module HubKernel
|
|
2
|
+
module Api
|
|
3
|
+
class HubsController < HubController
|
|
4
|
+
def show
|
|
5
|
+
render json: hub.exposures_for(person: caller_person, account: caller_account).map { |exposure| listed(exposure) }
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def listed(exposure) = { name: exposure.name, takes: exposure.takes, verb: exposure.writes ? "POST" : "GET" }
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
data/config/routes.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: hub_kernel-api
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- tylercschneider
|
|
@@ -29,14 +29,14 @@ dependencies:
|
|
|
29
29
|
requirements:
|
|
30
30
|
- - "~>"
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: '0.
|
|
32
|
+
version: '0.18'
|
|
33
33
|
type: :runtime
|
|
34
34
|
prerelease: false
|
|
35
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
36
36
|
requirements:
|
|
37
37
|
- - "~>"
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
|
-
version: '0.
|
|
39
|
+
version: '0.18'
|
|
40
40
|
description: 'hub_kernel-api gives any hub built on hub_kernel a JSON API: the host
|
|
41
41
|
lists the hubs it serves, and every call goes through the host''s sign-in, permission
|
|
42
42
|
check and account scope.'
|
|
@@ -50,6 +50,8 @@ files:
|
|
|
50
50
|
- README.md
|
|
51
51
|
- Rakefile
|
|
52
52
|
- app/controllers/hub_kernel/api/hub_calls_controller.rb
|
|
53
|
+
- app/controllers/hub_kernel/api/hub_controller.rb
|
|
54
|
+
- app/controllers/hub_kernel/api/hubs_controller.rb
|
|
53
55
|
- config/routes.rb
|
|
54
56
|
- lib/hub_kernel-api.rb
|
|
55
57
|
- lib/hub_kernel/api.rb
|