hub_kernel-api 0.1.0 → 0.3.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 +9 -1
- data/app/controllers/hub_kernel/api/hub_calls_controller.rb +13 -0
- data/lib/hub_kernel/api/version.rb +1 -1
- data/lib/hub_kernel/api.rb +3 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 86e1e73789993ac38e72f3707d5ff990ad680a180ea5c9b49afb8dbccc4ad2e0
|
|
4
|
+
data.tar.gz: '02937a7e3d0b5eb5979c316a32cb7dede99749b0d559b5846ff090a5c700fc64'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b8c5ba3ed71d2dbb8690ac63f7bded2b1cc9a3d9ef7e3592f65fe533402a6cbf8da8a7193e93e184bea3b6fa2bfa304e421ea16c9601bbbe8382c3524801fd29
|
|
7
|
+
data.tar.gz: 1ff6097cfa70e00947054dc7b3c788923f94ef5e11ec731869f42f30c08f315d2ba498b3452808f5cba9a021cb3336b07aae9849b930f57c8a43293226391430
|
data/README.md
CHANGED
|
@@ -28,7 +28,14 @@ mount HubKernel::Api::Engine => "/api/v1/hubs"
|
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
Each exposed method answers at `/<hub>/<method>`, where `<hub>` is the hub's module name
|
|
31
|
-
underscored.
|
|
31
|
+
underscored. To answer at a name of the host's choosing, list the hub as a one-pair hash:
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
HubKernel::Api.hubs = [ Supplies, { "money" => Billing::Ledger } ]
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
A hub listed this way answers only at the name given, and the permission check is still
|
|
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
|
|
32
39
|
return value under `answer`.
|
|
33
40
|
|
|
34
41
|
| Case | Status | Body |
|
|
@@ -36,6 +43,7 @@ return value under `answer`.
|
|
|
36
43
|
| The method answers | 200 | `{"answer": ...}` |
|
|
37
44
|
| 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"}` |
|
|
38
45
|
| The hub refuses, or a required value is missing | 422 | `{"error": "<reason>"}` |
|
|
46
|
+
| A permitted call sends a value the method is not listed with | 422 | `{"error": "<method> does not take <values>"}` |
|
|
39
47
|
| A record the call names does not exist | 404 | `{"error": "No <record> has the id <id>"}` |
|
|
40
48
|
|
|
41
49
|
## Installation
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
module HubKernel
|
|
2
2
|
module Api
|
|
3
3
|
class HubCallsController < HubKernel::Api.base_controller.constantize
|
|
4
|
+
wrap_parameters false
|
|
5
|
+
|
|
4
6
|
rescue_from ActionController::RoutingError, HubKernel::NotAllowed, with: :not_found
|
|
5
7
|
rescue_from HubKernel::Refused, HubKernel::MissingArgumentError, with: :refused
|
|
6
8
|
rescue_from ActiveRecord::RecordNotFound, with: :missing_record
|
|
7
9
|
|
|
8
10
|
def answer
|
|
9
11
|
raise ActionController::RoutingError, "Not found" unless asked_with_the_right_verb?
|
|
12
|
+
return refuse_unlisted_values if unlisted_values.any?
|
|
10
13
|
|
|
11
14
|
render json: { answer: hub.call_exposed(params[:name], values: values, person: caller_person, account: caller_account) }
|
|
12
15
|
end
|
|
@@ -17,6 +20,16 @@ module HubKernel
|
|
|
17
20
|
|
|
18
21
|
def asked_with_the_right_verb? = hub.exposed(params[:name])&.writes == request.post?
|
|
19
22
|
|
|
23
|
+
def refuse_unlisted_values
|
|
24
|
+
raise HubKernel::NotAllowed unless permitted?
|
|
25
|
+
|
|
26
|
+
refused(HubKernel::Refused.new("#{params[:name]} does not take #{unlisted_values.join(", ")}"))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def permitted? = hub.exposures_for(person: caller_person, account: caller_account).any? { |exposure| exposure.name.to_s == params[:name] }
|
|
30
|
+
|
|
31
|
+
def unlisted_values = values.keys - hub.exposed(params[:name]).takes
|
|
32
|
+
|
|
20
33
|
def values = request.query_parameters.merge(request.request_parameters).deep_symbolize_keys
|
|
21
34
|
|
|
22
35
|
def caller_person = send(HubKernel::Api.person_method)
|
data/lib/hub_kernel/api.rb
CHANGED
|
@@ -9,6 +9,8 @@ module HubKernel
|
|
|
9
9
|
mattr_accessor :person_method
|
|
10
10
|
mattr_accessor :account_method
|
|
11
11
|
|
|
12
|
-
def self.find(name) =
|
|
12
|
+
def self.find(name) = addresses[name]
|
|
13
|
+
|
|
14
|
+
def self.addresses = hubs.reduce({}) { |found, entry| found.merge(entry.is_a?(Hash) ? entry : { entry.name.demodulize.underscore => entry }) }
|
|
13
15
|
end
|
|
14
16
|
end
|
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.3.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.'
|