hub_kernel-api 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f88a656280e3947879dd6de7324478d0e25a728445ce3f1b4cd90d65bf7e7815
4
+ data.tar.gz: a87e442f929a84ed92f128d25824f8e6154a4db93356187a8e29185b02a4541a
5
+ SHA512:
6
+ metadata.gz: 2ad7cd9a06e4fc669dd54ea6288731b2e07d23aef30be8c8170bff74d8f1517428658a2feabea7ca12bb5d8e4b9b6d8ec4481c10b11b36ca5bbc11487cf29bc0
7
+ data.tar.gz: d78ea1612b08e6f4fe694617148ca50a32a1d8d6061066c79fd0dcd5a8ccd2711bd3fc85ad5f1c54693d489bdb872577fc2950ab052c50f1c781d0effac869e8
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright TODO: Write your name
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # hub_kernel-api
2
+
3
+ Serves a hub_kernel hub's exposed methods as a JSON API. The host lists the hubs it
4
+ serves, and every call goes through the host's own sign-in, then hub_kernel's permission
5
+ check and account scope.
6
+
7
+ ## Usage
8
+
9
+ Add the gem, then in an initializer name the controller the API inherits from, the two
10
+ methods on it that give the person and the account a call is made for, and the hubs it
11
+ serves:
12
+
13
+ ```ruby
14
+ HubKernel::Api.base_controller = "Api::HubBaseController"
15
+ HubKernel::Api.person_method = :current_person
16
+ HubKernel::Api.account_method = :current_account
17
+
18
+ Rails.application.config.to_prepare do
19
+ HubKernel::Api.hubs = [ Supplies ]
20
+ end
21
+ ```
22
+
23
+ The base controller's own sign-in runs before every call. hub_kernel's permission check
24
+ and account scope must also be set, as hub_kernel's readme describes. Mount the engine:
25
+
26
+ ```ruby
27
+ mount HubKernel::Api::Engine => "/api/v1/hubs"
28
+ ```
29
+
30
+ Each exposed method answers at `/<hub>/<method>`, where `<hub>` is the hub's module name
31
+ underscored. A read answers GET and a write answers POST, and the answer is the method's
32
+ return value under `answer`.
33
+
34
+ | Case | Status | Body |
35
+ | --- | --- | --- |
36
+ | The method answers | 200 | `{"answer": ...}` |
37
+ | 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
+ | The hub refuses, or a required value is missing | 422 | `{"error": "<reason>"}` |
39
+ | A record the call names does not exist | 404 | `{"error": "No <record> has the id <id>"}` |
40
+
41
+ ## Installation
42
+
43
+ ```ruby
44
+ gem "hub_kernel-api"
45
+ ```
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ require "bundler/gem_tasks"
@@ -0,0 +1,35 @@
1
+ module HubKernel
2
+ module Api
3
+ class HubCallsController < HubKernel::Api.base_controller.constantize
4
+ rescue_from ActionController::RoutingError, HubKernel::NotAllowed, with: :not_found
5
+ rescue_from HubKernel::Refused, HubKernel::MissingArgumentError, with: :refused
6
+ rescue_from ActiveRecord::RecordNotFound, with: :missing_record
7
+
8
+ def answer
9
+ raise ActionController::RoutingError, "Not found" unless asked_with_the_right_verb?
10
+
11
+ render json: { answer: hub.call_exposed(params[:name], values: values, person: caller_person, account: caller_account) }
12
+ end
13
+
14
+ private
15
+
16
+ def hub = @hub ||= HubKernel::Api.find(params[:hub]) || raise(ActionController::RoutingError, "Not found")
17
+
18
+ def asked_with_the_right_verb? = hub.exposed(params[:name])&.writes == request.post?
19
+
20
+ def values = request.query_parameters.merge(request.request_parameters).deep_symbolize_keys
21
+
22
+ def caller_person = send(HubKernel::Api.person_method)
23
+
24
+ def caller_account = send(HubKernel::Api.account_method)
25
+
26
+ def not_found = render(json: { error: "Not found" }, status: :not_found)
27
+
28
+ def refused(refusal) = render(json: { error: refusal.message }, status: :unprocessable_content)
29
+
30
+ def missing_record(missing)
31
+ render json: { error: "No #{missing.model.demodulize.underscore.humanize(capitalize: false)} has the id #{missing.id}" }, status: :not_found
32
+ end
33
+ end
34
+ end
35
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ HubKernel::Api::Engine.routes.draw do
2
+ match ":hub/:name", to: "hub_calls#answer", via: [ :get, :post ]
3
+ end
@@ -0,0 +1,7 @@
1
+ module HubKernel
2
+ module Api
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace HubKernel::Api
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module HubKernel
2
+ module Api
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ require "hub_kernel"
2
+ require "hub_kernel/api/version"
3
+ require "hub_kernel/api/engine"
4
+
5
+ module HubKernel
6
+ module Api
7
+ mattr_accessor :hubs, default: []
8
+ mattr_accessor :base_controller, default: "ActionController::API"
9
+ mattr_accessor :person_method
10
+ mattr_accessor :account_method
11
+
12
+ def self.find(name) = hubs.find { |hub| hub.name.demodulize.underscore == name }
13
+ end
14
+ end
@@ -0,0 +1 @@
1
+ require "hub_kernel/api"
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hub_kernel-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tylercschneider
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rails
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 8.1.3
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 8.1.3
26
+ - !ruby/object:Gem::Dependency
27
+ name: hub_kernel
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '0.11'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.11'
40
+ description: 'hub_kernel-api gives any hub built on hub_kernel a JSON API: the host
41
+ lists the hubs it serves, and every call goes through the host''s sign-in, permission
42
+ check and account scope.'
43
+ email:
44
+ - tylercschneider@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - MIT-LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - app/controllers/hub_kernel/api/hub_calls_controller.rb
53
+ - config/routes.rb
54
+ - lib/hub_kernel-api.rb
55
+ - lib/hub_kernel/api.rb
56
+ - lib/hub_kernel/api/engine.rb
57
+ - lib/hub_kernel/api/version.rb
58
+ homepage: https://github.com/DYB-Development/hub_kernel-api
59
+ licenses:
60
+ - MIT
61
+ metadata:
62
+ allowed_push_host: https://rubygems.org
63
+ homepage_uri: https://github.com/DYB-Development/hub_kernel-api
64
+ source_code_uri: https://github.com/DYB-Development/hub_kernel-api
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 3.2.0
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 4.0.20
80
+ specification_version: 4
81
+ summary: Serves a hub_kernel hub's exposed methods as a JSON API.
82
+ test_files: []