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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ff8bff7f1990c004e388aac6bbfe08f93f6007263653962a93ee7589d185a24
4
- data.tar.gz: f79790b1f3a26080b561ce6968142c39aa584742b0a5e037a231165ed00fd0d2
3
+ metadata.gz: e54d19b67798c82119ecff94f0f294353cd927da83a55282b2f61ffbf111061d
4
+ data.tar.gz: 1d48fc7eb9520cb6300315462a74159b68a0e236c797f87d97964c4455eefce1
5
5
  SHA512:
6
- metadata.gz: 3d9ee144416f2ab53a5f66aa0e590ab7a784cec6519a49d89336c3dfe0df53bf6ce9ace1cda27644e9e5616ccf08f33bde8e5b04a30b8bc356153ccbffbc716f
7
- data.tar.gz: fe2a86a74e9e47f6b78b3093d6ac29bd766d706b3b3024762a89f3e80d2936be896d90633b2860f78700cce2f25674035fd8e939d94472e5bd7967850e81993b
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 < HubKernel::Api.base_controller.constantize
4
- rescue_from ActionController::RoutingError, HubKernel::NotAllowed, with: :not_found
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 values = request.query_parameters.merge(request.request_parameters).deep_symbolize_keys
20
+ def refuse_unlisted_values
21
+ raise HubKernel::NotAllowed unless permitted?
21
22
 
22
- def caller_person = send(HubKernel::Api.person_method)
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 caller_account = send(HubKernel::Api.account_method)
28
+ def unlisted_values = values.keys - hub.exposed(params[:name]).takes
25
29
 
26
- def not_found = render(json: { error: "Not found" }, status: :not_found)
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
@@ -1,3 +1,4 @@
1
1
  HubKernel::Api::Engine.routes.draw do
2
+ get ":hub", to: "hubs#show"
2
3
  match ":hub/:name", to: "hub_calls#answer", via: [ :get, :post ]
3
4
  end
@@ -1,5 +1,5 @@
1
1
  module HubKernel
2
2
  module Api
3
- VERSION = "0.2.0"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  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.2.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.11'
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.11'
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