jsonrpc-middleware 0.1.0 → 0.2.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/.aiexclude +4 -0
- data/.claude/commands/document.md +105 -0
- data/.claude/docs/yard.md +602 -0
- data/.claude/settings.local.json +2 -1
- data/.env.example +5 -0
- data/CHANGELOG.md +22 -2
- data/CLAUDE.md +114 -0
- data/README.md +42 -102
- data/Rakefile +59 -1
- data/examples/README.md +37 -0
- data/examples/procedures.rb +6 -1
- data/examples/rack/README.md +26 -1
- data/examples/rack/app.rb +1 -1
- data/examples/rack-echo/README.md +23 -1
- data/examples/rack-single-file/README.md +37 -0
- data/examples/rack-single-file/config.ru +54 -0
- data/examples/rails/.gitignore +21 -0
- data/examples/rails/.ruby-version +1 -0
- data/examples/rails/Gemfile +15 -0
- data/examples/rails/Gemfile.lock +261 -0
- data/examples/rails/README.md +32 -0
- data/examples/rails/Rakefile +8 -0
- data/examples/rails/app/controllers/application_controller.rb +4 -0
- data/examples/rails/app/controllers/jsonrpc_controller.rb +44 -0
- data/examples/rails/bin/dev +4 -0
- data/examples/rails/bin/rails +6 -0
- data/examples/rails/bin/rake +6 -0
- data/examples/rails/bin/setup +28 -0
- data/examples/rails/config/application.rb +47 -0
- data/examples/rails/config/boot.rb +5 -0
- data/examples/rails/config/credentials.yml.enc +1 -0
- data/examples/rails/config/environment.rb +7 -0
- data/examples/rails/config/environments/development.rb +42 -0
- data/examples/rails/config/environments/production.rb +60 -0
- data/examples/rails/config/environments/test.rb +44 -0
- data/examples/rails/config/initializers/cors.rb +18 -0
- data/examples/rails/config/initializers/filter_parameter_logging.rb +10 -0
- data/examples/rails/config/initializers/inflections.rb +18 -0
- data/examples/rails/config/initializers/jsonrpc.rb +62 -0
- data/examples/rails/config/locales/en.yml +31 -0
- data/examples/rails/config/puma.rb +40 -0
- data/examples/rails/config/routes.rb +14 -0
- data/examples/rails/config.ru +8 -0
- data/examples/rails/public/robots.txt +1 -0
- data/examples/rails-single-file/config.ru +71 -0
- data/examples/sinatra-classic/Gemfile +9 -0
- data/examples/sinatra-classic/Gemfile.lock +95 -0
- data/examples/sinatra-classic/README.md +32 -0
- data/examples/sinatra-classic/app.rb +54 -0
- data/examples/sinatra-classic/config.ru +6 -0
- data/examples/sinatra-modular/Gemfile +9 -0
- data/examples/sinatra-modular/Gemfile.lock +95 -0
- data/examples/sinatra-modular/README.md +32 -0
- data/examples/sinatra-modular/app.rb +57 -0
- data/examples/sinatra-modular/config.ru +6 -0
- data/lib/jsonrpc/batch_request.rb +67 -2
- data/lib/jsonrpc/batch_response.rb +56 -0
- data/lib/jsonrpc/configuration.rb +156 -14
- data/lib/jsonrpc/error.rb +83 -2
- data/lib/jsonrpc/errors/internal_error.rb +14 -2
- data/lib/jsonrpc/errors/invalid_params_error.rb +13 -1
- data/lib/jsonrpc/errors/invalid_request_error.rb +8 -0
- data/lib/jsonrpc/errors/method_not_found_error.rb +8 -0
- data/lib/jsonrpc/errors/parse_error.rb +8 -0
- data/lib/jsonrpc/helpers.rb +212 -21
- data/lib/jsonrpc/middleware.rb +211 -5
- data/lib/jsonrpc/notification.rb +58 -0
- data/lib/jsonrpc/parser.rb +30 -0
- data/lib/jsonrpc/railtie.rb +57 -0
- data/lib/jsonrpc/request.rb +68 -1
- data/lib/jsonrpc/response.rb +76 -0
- data/lib/jsonrpc/validator.rb +67 -6
- data/lib/jsonrpc/version.rb +11 -1
- data/lib/jsonrpc.rb +53 -1
- metadata +49 -1
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# The test environment is used exclusively to run your application's
|
4
|
+
# test suite. You never need to work with it otherwise. Remember that
|
5
|
+
# your test database is "scratch space" for the test suite and is wiped
|
6
|
+
# and recreated between test runs. Don't rely on the data there!
|
7
|
+
|
8
|
+
Rails.application.configure do
|
9
|
+
# Settings specified here will take precedence over those in config/application.rb.
|
10
|
+
|
11
|
+
# While tests run files are not watched, reloading is not necessary.
|
12
|
+
config.enable_reloading = false
|
13
|
+
|
14
|
+
# Eager loading loads your entire application. When running a single test locally,
|
15
|
+
# this is usually not necessary, and can slow down your test suite. However, it's
|
16
|
+
# recommended that you enable it in continuous integration systems to ensure eager
|
17
|
+
# loading is working properly before deploying your code.
|
18
|
+
config.eager_load = ENV['CI'].present?
|
19
|
+
|
20
|
+
# Configure public file server for tests with cache-control for performance.
|
21
|
+
config.public_file_server.headers = { 'cache-control' => 'public, max-age=3600' }
|
22
|
+
|
23
|
+
# Show full error reports.
|
24
|
+
config.consider_all_requests_local = true
|
25
|
+
config.cache_store = :null_store
|
26
|
+
|
27
|
+
# Render exception templates for rescuable exceptions and raise for other exceptions.
|
28
|
+
config.action_dispatch.show_exceptions = :rescuable
|
29
|
+
|
30
|
+
# Disable request forgery protection in test environment.
|
31
|
+
config.action_controller.allow_forgery_protection = false
|
32
|
+
|
33
|
+
# Print deprecation notices to the stderr.
|
34
|
+
config.active_support.deprecation = :stderr
|
35
|
+
|
36
|
+
# Raises error for missing translations.
|
37
|
+
# config.i18n.raise_on_missing_translations = true
|
38
|
+
|
39
|
+
# Annotate rendered view with file names.
|
40
|
+
# config.action_view.annotate_rendered_view_with_filenames = true
|
41
|
+
|
42
|
+
# Raise error when a before_action's only/except options reference missing actions.
|
43
|
+
config.action_controller.raise_on_missing_callback_actions = true
|
44
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Be sure to restart your server when you modify this file.
|
4
|
+
|
5
|
+
# Avoid CORS issues when API is called from the frontend app.
|
6
|
+
# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin Ajax requests.
|
7
|
+
|
8
|
+
# Read more: https://github.com/cyu/rack-cors
|
9
|
+
|
10
|
+
# Rails.application.config.middleware.insert_before 0, Rack::Cors do
|
11
|
+
# allow do
|
12
|
+
# origins "example.com"
|
13
|
+
#
|
14
|
+
# resource "*",
|
15
|
+
# headers: :any,
|
16
|
+
# methods: [:get, :post, :put, :patch, :delete, :options, :head]
|
17
|
+
# end
|
18
|
+
# end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Be sure to restart your server when you modify this file.
|
4
|
+
|
5
|
+
# Configure parameters to be partially matched (e.g. passw matches password) and filtered from the log file.
|
6
|
+
# Use this to limit dissemination of sensitive information.
|
7
|
+
# See the ActiveSupport::ParameterFilter documentation for supported notations and behaviors.
|
8
|
+
Rails.application.config.filter_parameters += %i[
|
9
|
+
passw email secret token _key crypt salt certificate otp ssn cvv cvc
|
10
|
+
]
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Be sure to restart your server when you modify this file.
|
4
|
+
|
5
|
+
# Add new inflection rules using the following format. Inflections
|
6
|
+
# are locale specific, and you may define rules for as many different
|
7
|
+
# locales as you wish. All of these examples are active by default:
|
8
|
+
# ActiveSupport::Inflector.inflections(:en) do |inflect|
|
9
|
+
# inflect.plural /^(ox)$/i, "\\1en"
|
10
|
+
# inflect.singular /^(ox)en/i, "\\1"
|
11
|
+
# inflect.irregular "person", "people"
|
12
|
+
# inflect.uncountable %w( fish sheep )
|
13
|
+
# end
|
14
|
+
|
15
|
+
# These inflection rules are supported but not enabled by default:
|
16
|
+
# ActiveSupport::Inflector.inflections(:en) do |inflect|
|
17
|
+
# inflect.acronym "RESTful"
|
18
|
+
# end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../../../procedures'
|
4
|
+
|
5
|
+
# JSONRPC.configure do |config|
|
6
|
+
# config.log_internal_errors = true # Log internal error backtraces (default: true)
|
7
|
+
# config.log_request_validation_errors = true # Log JSON-RPC request validation errors (default: false)
|
8
|
+
# config.render_internal_errors = true # Render internal error information in responses (default: true)
|
9
|
+
# config.rescue_internal_errors = true # Handle and serialize internal errors (default: true)
|
10
|
+
#
|
11
|
+
# # Allow positional and named arguments
|
12
|
+
# procedure(:add, allow_positional_arguments: true) do
|
13
|
+
# params do
|
14
|
+
# required(:addends).filled(:array)
|
15
|
+
# required(:addends).value(:array).each(type?: Numeric)
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# rule(:addends) do
|
19
|
+
# key.failure('must contain at least one addend') if value.empty?
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# procedure(:subtract) do
|
24
|
+
# params do
|
25
|
+
# required(:minuend).filled(:integer)
|
26
|
+
# required(:subtrahend).filled(:integer)
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# procedure(:multiply) do
|
31
|
+
# params do
|
32
|
+
# required(:multiplicand).filled
|
33
|
+
# required(:multiplier).filled
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# rule(:multiplicand) do
|
37
|
+
# key.failure('must be a number') unless value.is_a?(Numeric)
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
# rule(:multiplier) do
|
41
|
+
# key.failure('must be a number') unless value.is_a?(Numeric)
|
42
|
+
# end
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# procedure(:divide) do
|
46
|
+
# params do
|
47
|
+
# required(:dividend).filled(:integer)
|
48
|
+
# required(:divisor).filled(:integer)
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# rule(:divisor) do
|
52
|
+
# key.failure('cannot be zero') if value.zero?
|
53
|
+
# end
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# # Used only to test internal server errors
|
57
|
+
# procedure(:explode) do
|
58
|
+
# params do
|
59
|
+
# # No params
|
60
|
+
# end
|
61
|
+
# end
|
62
|
+
# end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# Files in the config/locales directory are used for internationalization and
|
2
|
+
# are automatically loaded by Rails. If you want to use locales other than
|
3
|
+
# English, add the necessary files in this directory.
|
4
|
+
#
|
5
|
+
# To use the locales, use `I18n.t`:
|
6
|
+
#
|
7
|
+
# I18n.t "hello"
|
8
|
+
#
|
9
|
+
# In views, this is aliased to just `t`:
|
10
|
+
#
|
11
|
+
# <%= t("hello") %>
|
12
|
+
#
|
13
|
+
# To use a different locale, set it with `I18n.locale`:
|
14
|
+
#
|
15
|
+
# I18n.locale = :es
|
16
|
+
#
|
17
|
+
# This would use the information in config/locales/es.yml.
|
18
|
+
#
|
19
|
+
# To learn more about the API, please read the Rails Internationalization guide
|
20
|
+
# at https://guides.rubyonrails.org/i18n.html.
|
21
|
+
#
|
22
|
+
# Be aware that YAML interprets the following case-insensitive strings as
|
23
|
+
# booleans: `true`, `false`, `on`, `off`, `yes`, `no`. Therefore, these strings
|
24
|
+
# must be quoted to be interpreted as strings. For example:
|
25
|
+
#
|
26
|
+
# en:
|
27
|
+
# "yes": yup
|
28
|
+
# enabled: "ON"
|
29
|
+
|
30
|
+
en:
|
31
|
+
hello: "Hello world"
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This configuration file will be evaluated by Puma. The top-level methods that
|
4
|
+
# are invoked here are part of Puma's configuration DSL. For more information
|
5
|
+
# about methods provided by the DSL, see https://puma.io/puma/Puma/DSL.html.
|
6
|
+
#
|
7
|
+
# Puma starts a configurable number of processes (workers) and each process
|
8
|
+
# serves each request in a thread from an internal thread pool.
|
9
|
+
#
|
10
|
+
# You can control the number of workers using ENV["WEB_CONCURRENCY"]. You
|
11
|
+
# should only set this value when you want to run 2 or more workers. The
|
12
|
+
# default is already 1.
|
13
|
+
#
|
14
|
+
# The ideal number of threads per worker depends both on how much time the
|
15
|
+
# application spends waiting for IO operations and on how much you wish to
|
16
|
+
# prioritize throughput over latency.
|
17
|
+
#
|
18
|
+
# As a rule of thumb, increasing the number of threads will increase how much
|
19
|
+
# traffic a given process can handle (throughput), but due to CRuby's
|
20
|
+
# Global VM Lock (GVL) it has diminishing returns and will degrade the
|
21
|
+
# response time (latency) of the application.
|
22
|
+
#
|
23
|
+
# The default is set to 3 threads as it's deemed a decent compromise between
|
24
|
+
# throughput and latency for the average Rails application.
|
25
|
+
#
|
26
|
+
# Any libraries that use a connection pool or another resource pool should
|
27
|
+
# be configured to provide at least as many connections as the number of
|
28
|
+
# threads. This includes Active Record's `pool` parameter in `database.yml`.
|
29
|
+
threads_count = ENV.fetch('RAILS_MAX_THREADS', 3)
|
30
|
+
threads threads_count, threads_count
|
31
|
+
|
32
|
+
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
|
33
|
+
port ENV.fetch('PORT', 3000)
|
34
|
+
|
35
|
+
# Allow puma to be restarted by `bin/rails restart` command.
|
36
|
+
plugin :tmp_restart
|
37
|
+
|
38
|
+
# Specify the PID file. Defaults to tmp/pids/server.pid in development.
|
39
|
+
# In other environments, only set the PID file if requested.
|
40
|
+
pidfile ENV['PIDFILE'] if ENV['PIDFILE']
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Rails.application.routes.draw do
|
4
|
+
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
5
|
+
|
6
|
+
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
|
7
|
+
# Can be used by load balancers and uptime monitors to verify that the app is live.
|
8
|
+
get 'up' => 'rails/health#show', as: :rails_health_check
|
9
|
+
|
10
|
+
post '/', to: 'jsonrpc#handle'
|
11
|
+
|
12
|
+
# Defines the root path route ("/")
|
13
|
+
# root "posts#index"
|
14
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
# See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/inline'
|
4
|
+
|
5
|
+
gemfile(true) do
|
6
|
+
source 'https://rubygems.org'
|
7
|
+
|
8
|
+
gem 'rails', '~> 8.0.2'
|
9
|
+
gem 'puma', '~> 6.6.0'
|
10
|
+
gem 'jsonrpc-middleware', path: '../../', require: 'jsonrpc'
|
11
|
+
end
|
12
|
+
|
13
|
+
require 'rails'
|
14
|
+
require 'action_controller/railtie'
|
15
|
+
|
16
|
+
JSONRPC.configure do |config|
|
17
|
+
config.rescue_internal_errors = true # set to +false+ if you want to raise JSONRPC::InternalError manually
|
18
|
+
|
19
|
+
procedure(:echo) do
|
20
|
+
params do
|
21
|
+
required(:message).filled(:string)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Define the application
|
27
|
+
class App < Rails::Application
|
28
|
+
config.root = __dir__
|
29
|
+
config.cache_classes = true
|
30
|
+
config.eager_load = true
|
31
|
+
config.active_support.deprecation = :stderr
|
32
|
+
config.consider_all_requests_local = true
|
33
|
+
config.active_support.to_time_preserves_timezone = :zone
|
34
|
+
config.logger = nil
|
35
|
+
config.hosts.clear
|
36
|
+
|
37
|
+
routes.append do
|
38
|
+
post '/', to: 'jsonrpc#handle'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Define the JSONRPC controller
|
43
|
+
class JsonrpcController < ActionController::Base
|
44
|
+
def handle
|
45
|
+
if jsonrpc_request?
|
46
|
+
result = handle_single(jsonrpc_request)
|
47
|
+
render jsonrpc: result
|
48
|
+
elsif jsonrpc_notification?
|
49
|
+
handle_single(jsonrpc_notification)
|
50
|
+
render jsonrpc: nil
|
51
|
+
else
|
52
|
+
responses = handle_batch(jsonrpc_batch)
|
53
|
+
render jsonrpc: responses
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def handle_single(request_or_notification) = request_or_notification.params
|
60
|
+
|
61
|
+
def handle_batch(batch)
|
62
|
+
batch.flat_map do |request_or_notification|
|
63
|
+
result = handle_single(request_or_notification)
|
64
|
+
JSONRPC::Response.new(id: request_or_notification.id, result:) if request_or_notification.is_a?(JSONRPC::Request)
|
65
|
+
end.compact
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
App.initialize!
|
70
|
+
|
71
|
+
run App
|
@@ -0,0 +1,95 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../..
|
3
|
+
specs:
|
4
|
+
jsonrpc-middleware (0.1.0)
|
5
|
+
dry-validation (~> 1.11)
|
6
|
+
zeitwerk (~> 2.7)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
base64 (0.2.0)
|
12
|
+
bigdecimal (3.2.2)
|
13
|
+
concurrent-ruby (1.3.5)
|
14
|
+
dry-configurable (1.3.0)
|
15
|
+
dry-core (~> 1.1)
|
16
|
+
zeitwerk (~> 2.6)
|
17
|
+
dry-core (1.1.0)
|
18
|
+
concurrent-ruby (~> 1.0)
|
19
|
+
logger
|
20
|
+
zeitwerk (~> 2.6)
|
21
|
+
dry-inflector (1.2.0)
|
22
|
+
dry-initializer (3.2.0)
|
23
|
+
dry-logic (1.6.0)
|
24
|
+
bigdecimal
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
dry-core (~> 1.1)
|
27
|
+
zeitwerk (~> 2.6)
|
28
|
+
dry-schema (1.14.1)
|
29
|
+
concurrent-ruby (~> 1.0)
|
30
|
+
dry-configurable (~> 1.0, >= 1.0.1)
|
31
|
+
dry-core (~> 1.1)
|
32
|
+
dry-initializer (~> 3.2)
|
33
|
+
dry-logic (~> 1.5)
|
34
|
+
dry-types (~> 1.8)
|
35
|
+
zeitwerk (~> 2.6)
|
36
|
+
dry-types (1.8.3)
|
37
|
+
bigdecimal (~> 3.0)
|
38
|
+
concurrent-ruby (~> 1.0)
|
39
|
+
dry-core (~> 1.0)
|
40
|
+
dry-inflector (~> 1.0)
|
41
|
+
dry-logic (~> 1.4)
|
42
|
+
zeitwerk (~> 2.6)
|
43
|
+
dry-validation (1.11.1)
|
44
|
+
concurrent-ruby (~> 1.0)
|
45
|
+
dry-core (~> 1.1)
|
46
|
+
dry-initializer (~> 3.2)
|
47
|
+
dry-schema (~> 1.14)
|
48
|
+
zeitwerk (~> 2.6)
|
49
|
+
logger (1.7.0)
|
50
|
+
multi_json (1.15.0)
|
51
|
+
mustermann (3.0.3)
|
52
|
+
ruby2_keywords (~> 0.0.1)
|
53
|
+
nio4r (2.7.4)
|
54
|
+
puma (6.6.0)
|
55
|
+
nio4r (~> 2.0)
|
56
|
+
rack (3.1.13)
|
57
|
+
rack-protection (4.1.1)
|
58
|
+
base64 (>= 0.1.0)
|
59
|
+
logger (>= 1.6.0)
|
60
|
+
rack (>= 3.0.0, < 4)
|
61
|
+
rack-session (2.1.0)
|
62
|
+
base64 (>= 0.1.0)
|
63
|
+
rack (>= 3.0.0)
|
64
|
+
rackup (2.2.1)
|
65
|
+
rack (>= 3)
|
66
|
+
ruby2_keywords (0.0.5)
|
67
|
+
sinatra (4.1.1)
|
68
|
+
logger (>= 1.6.0)
|
69
|
+
mustermann (~> 3.0)
|
70
|
+
rack (>= 3.0.0, < 4)
|
71
|
+
rack-protection (= 4.1.1)
|
72
|
+
rack-session (>= 2.0.0, < 3)
|
73
|
+
tilt (~> 2.0)
|
74
|
+
sinatra-contrib (4.1.1)
|
75
|
+
multi_json (>= 0.0.2)
|
76
|
+
mustermann (~> 3.0)
|
77
|
+
rack-protection (= 4.1.1)
|
78
|
+
sinatra (= 4.1.1)
|
79
|
+
tilt (~> 2.0)
|
80
|
+
tilt (2.6.0)
|
81
|
+
zeitwerk (2.7.2)
|
82
|
+
|
83
|
+
PLATFORMS
|
84
|
+
arm64-darwin-24
|
85
|
+
ruby
|
86
|
+
|
87
|
+
DEPENDENCIES
|
88
|
+
jsonrpc-middleware!
|
89
|
+
puma
|
90
|
+
rackup
|
91
|
+
sinatra
|
92
|
+
sinatra-contrib
|
93
|
+
|
94
|
+
BUNDLED WITH
|
95
|
+
2.6.8
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# Sinatra Classic Calculator
|
2
|
+
|
3
|
+
A JSON-RPC calculator server using classic Sinatra style.
|
4
|
+
|
5
|
+
## Running
|
6
|
+
|
7
|
+
```sh
|
8
|
+
bundle install
|
9
|
+
bundle exec rackup
|
10
|
+
```
|
11
|
+
|
12
|
+
## API
|
13
|
+
|
14
|
+
The server implements a calculator with these procedures:
|
15
|
+
|
16
|
+
- `add` - Add numbers (supports both positional and named arguments)
|
17
|
+
- `subtract` - Subtract two numbers
|
18
|
+
- `multiply` - Multiply two numbers
|
19
|
+
- `divide` - Divide two numbers
|
20
|
+
- `explode` - Test procedure that throws an error
|
21
|
+
|
22
|
+
## Example Requests
|
23
|
+
|
24
|
+
```sh
|
25
|
+
curl -X POST http://localhost:9292 \
|
26
|
+
-H "Content-Type: application/json" \
|
27
|
+
-d '{"jsonrpc": "2.0", "method": "add", "params": {"addends": [1, 2, 3]}, "id": 1}'
|
28
|
+
|
29
|
+
curl -X POST http://localhost:9292 \
|
30
|
+
-H "Content-Type: application/json" \
|
31
|
+
-d '{"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 10, "subtrahend": 3}, "id": 2}'
|
32
|
+
```
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'sinatra'
|
4
|
+
require 'sinatra/json'
|
5
|
+
require 'jsonrpc'
|
6
|
+
|
7
|
+
set :host_authorization, permitted_hosts: []
|
8
|
+
set :raise_errors, true
|
9
|
+
set :show_exceptions, false
|
10
|
+
|
11
|
+
use JSONRPC::Middleware
|
12
|
+
helpers JSONRPC::Helpers
|
13
|
+
|
14
|
+
post '/' do
|
15
|
+
@env = env # Set the @env instance variable to use the JSONRPC helpers below
|
16
|
+
|
17
|
+
if jsonrpc_request?
|
18
|
+
result = handle_single(jsonrpc_request)
|
19
|
+
jsonrpc_response(result)
|
20
|
+
elsif jsonrpc_notification?
|
21
|
+
handle_single(jsonrpc_notification)
|
22
|
+
jsonrpc_notification_response
|
23
|
+
else
|
24
|
+
responses = handle_batch(jsonrpc_batch)
|
25
|
+
jsonrpc_batch_response(responses)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def handle_single(request_or_notification)
|
32
|
+
params = request_or_notification.params
|
33
|
+
|
34
|
+
case request_or_notification.method
|
35
|
+
when 'add'
|
36
|
+
addends = params.is_a?(Array) ? params : params['addends'] # Handle positional and named arguments
|
37
|
+
addends.sum
|
38
|
+
when 'subtract'
|
39
|
+
params['minuend'] - params['subtrahend']
|
40
|
+
when 'multiply'
|
41
|
+
params['multiplicand'] * params['multiplier']
|
42
|
+
when 'divide'
|
43
|
+
params['dividend'] / params['divisor']
|
44
|
+
when 'explode'
|
45
|
+
raise 'An internal error has occurred.'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def handle_batch(batch)
|
50
|
+
batch.flat_map do |request_or_notification|
|
51
|
+
result = handle_single(request_or_notification)
|
52
|
+
JSONRPC::Response.new(id: request_or_notification.id, result:) if request_or_notification.is_a?(JSONRPC::Request)
|
53
|
+
end.compact
|
54
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../..
|
3
|
+
specs:
|
4
|
+
jsonrpc-middleware (0.1.0)
|
5
|
+
dry-validation (~> 1.11)
|
6
|
+
zeitwerk (~> 2.7)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
base64 (0.2.0)
|
12
|
+
bigdecimal (3.2.2)
|
13
|
+
concurrent-ruby (1.3.5)
|
14
|
+
dry-configurable (1.3.0)
|
15
|
+
dry-core (~> 1.1)
|
16
|
+
zeitwerk (~> 2.6)
|
17
|
+
dry-core (1.1.0)
|
18
|
+
concurrent-ruby (~> 1.0)
|
19
|
+
logger
|
20
|
+
zeitwerk (~> 2.6)
|
21
|
+
dry-inflector (1.2.0)
|
22
|
+
dry-initializer (3.2.0)
|
23
|
+
dry-logic (1.6.0)
|
24
|
+
bigdecimal
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
dry-core (~> 1.1)
|
27
|
+
zeitwerk (~> 2.6)
|
28
|
+
dry-schema (1.14.1)
|
29
|
+
concurrent-ruby (~> 1.0)
|
30
|
+
dry-configurable (~> 1.0, >= 1.0.1)
|
31
|
+
dry-core (~> 1.1)
|
32
|
+
dry-initializer (~> 3.2)
|
33
|
+
dry-logic (~> 1.5)
|
34
|
+
dry-types (~> 1.8)
|
35
|
+
zeitwerk (~> 2.6)
|
36
|
+
dry-types (1.8.3)
|
37
|
+
bigdecimal (~> 3.0)
|
38
|
+
concurrent-ruby (~> 1.0)
|
39
|
+
dry-core (~> 1.0)
|
40
|
+
dry-inflector (~> 1.0)
|
41
|
+
dry-logic (~> 1.4)
|
42
|
+
zeitwerk (~> 2.6)
|
43
|
+
dry-validation (1.11.1)
|
44
|
+
concurrent-ruby (~> 1.0)
|
45
|
+
dry-core (~> 1.1)
|
46
|
+
dry-initializer (~> 3.2)
|
47
|
+
dry-schema (~> 1.14)
|
48
|
+
zeitwerk (~> 2.6)
|
49
|
+
logger (1.7.0)
|
50
|
+
multi_json (1.15.0)
|
51
|
+
mustermann (3.0.3)
|
52
|
+
ruby2_keywords (~> 0.0.1)
|
53
|
+
nio4r (2.7.4)
|
54
|
+
puma (6.6.0)
|
55
|
+
nio4r (~> 2.0)
|
56
|
+
rack (3.1.13)
|
57
|
+
rack-protection (4.1.1)
|
58
|
+
base64 (>= 0.1.0)
|
59
|
+
logger (>= 1.6.0)
|
60
|
+
rack (>= 3.0.0, < 4)
|
61
|
+
rack-session (2.1.0)
|
62
|
+
base64 (>= 0.1.0)
|
63
|
+
rack (>= 3.0.0)
|
64
|
+
rackup (2.2.1)
|
65
|
+
rack (>= 3)
|
66
|
+
ruby2_keywords (0.0.5)
|
67
|
+
sinatra (4.1.1)
|
68
|
+
logger (>= 1.6.0)
|
69
|
+
mustermann (~> 3.0)
|
70
|
+
rack (>= 3.0.0, < 4)
|
71
|
+
rack-protection (= 4.1.1)
|
72
|
+
rack-session (>= 2.0.0, < 3)
|
73
|
+
tilt (~> 2.0)
|
74
|
+
sinatra-contrib (4.1.1)
|
75
|
+
multi_json (>= 0.0.2)
|
76
|
+
mustermann (~> 3.0)
|
77
|
+
rack-protection (= 4.1.1)
|
78
|
+
sinatra (= 4.1.1)
|
79
|
+
tilt (~> 2.0)
|
80
|
+
tilt (2.6.0)
|
81
|
+
zeitwerk (2.7.2)
|
82
|
+
|
83
|
+
PLATFORMS
|
84
|
+
arm64-darwin-24
|
85
|
+
ruby
|
86
|
+
|
87
|
+
DEPENDENCIES
|
88
|
+
jsonrpc-middleware!
|
89
|
+
puma
|
90
|
+
rackup
|
91
|
+
sinatra
|
92
|
+
sinatra-contrib
|
93
|
+
|
94
|
+
BUNDLED WITH
|
95
|
+
2.6.8
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# Sinatra Modular Calculator
|
2
|
+
|
3
|
+
A JSON-RPC calculator server using modular Sinatra (Sinatra::Base).
|
4
|
+
|
5
|
+
## Running
|
6
|
+
|
7
|
+
```sh
|
8
|
+
bundle install
|
9
|
+
bundle exec rackup
|
10
|
+
```
|
11
|
+
|
12
|
+
## API
|
13
|
+
|
14
|
+
The server implements a calculator with these procedures:
|
15
|
+
|
16
|
+
- `add` - Add numbers (supports both positional and named arguments)
|
17
|
+
- `subtract` - Subtract two numbers
|
18
|
+
- `multiply` - Multiply two numbers
|
19
|
+
- `divide` - Divide two numbers
|
20
|
+
- `explode` - Test procedure that throws an error
|
21
|
+
|
22
|
+
## Example Requests
|
23
|
+
|
24
|
+
```sh
|
25
|
+
curl -X POST http://localhost:9292 \
|
26
|
+
-H "Content-Type: application/json" \
|
27
|
+
-d '{"jsonrpc": "2.0", "method": "add", "params": {"addends": [1, 2, 3]}, "id": 1}'
|
28
|
+
|
29
|
+
curl -X POST http://localhost:9292 \
|
30
|
+
-H "Content-Type: application/json" \
|
31
|
+
-d '{"jsonrpc": "2.0", "method": "multiply", "params": {"multiplicand": 6, "multiplier": 7}, "id": 3}'
|
32
|
+
```
|