Skip to content

Commit

Permalink
alpha version
Browse files Browse the repository at this point in the history
  • Loading branch information
andriytyurnikov committed May 3, 2024
0 parents commit 43636f8
Show file tree
Hide file tree
Showing 19 changed files with 635 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Ruby

on:
push:
branches:
- main

pull_request:

jobs:
build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- '3.3.0'

steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run the default task
run: bundle exec rake
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
18 changes: 18 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
AllCops:
NewCops: enable
TargetRubyVersion: 3.0

Metrics/MethodLength:
Enabled: false

Style/Documentation:
Enabled: false

Style/MultilineBlockChain:
Enabled: false

Style/StringLiterals:
EnforcedStyle: double_quotes

Style/StringLiteralsInInterpolation:
EnforcedStyle: double_quotes
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in clamo.gemspec
gemspec

gem "rake", "~> 13.0"

gem "minitest", "~> 5.16"

gem "rubocop", "~> 1.21"
50 changes: 50 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
PATH
remote: .
specs:
clamo (0.1.0)
parallel (~> 1.24.0)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
json (2.7.2)
language_server-protocol (3.17.0.3)
minitest (5.22.3)
parallel (1.24.0)
parser (3.3.1.0)
ast (~> 2.4.1)
racc
racc (1.7.3)
rainbow (3.1.1)
rake (13.2.1)
regexp_parser (2.9.0)
rexml (3.2.6)
rubocop (1.63.4)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
ruby-progressbar (1.13.0)
unicode-display_width (2.5.0)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
clamo!
minitest (~> 5.16)
rake (~> 13.0)
rubocop (~> 1.21)

BUNDLED WITH
2.5.9
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Clamo

JSON-RPC protocol toolkit for Ruby.

Consume, Serve or test JSON-RPC endpoints with Clamo.

## Installation

TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.

Install the gem and add to the application's Gemfile by executing:

$ bundle add clamo

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install clamo

## Usage

TODO: Write usage instructions here

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/clamo.
12 changes: 12 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "minitest/test_task"

Minitest::TestTask.create

require "rubocop/rake_task"

RuboCop::RakeTask.new

task default: %i[test rubocop]
11 changes: 11 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "clamo"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
43 changes: 43 additions & 0 deletions clamo.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require_relative "lib/clamo/version"

Gem::Specification.new do |spec|
spec.name = "clamo"
spec.version = Clamo::VERSION
spec.authors = ["Andriy Tyurnikov"]
spec.email = ["[email protected]"]

spec.summary = "JSON-RPC Client/Server tooling for Ruby"
spec.description = "JSON-RPC Client/Server tooling for Ruby"
spec.homepage = "https://github.com/andriytyurnikov/clamo"
spec.required_ruby_version = ">= 3.0.0"

# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = spec.homepage

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
gemspec = File.basename(__FILE__)
spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls|
ls.readlines("\x0", chomp: true).reject do |f|
(f == gemspec) ||
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
end
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "parallel", "~> 1.24.0"
# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"
#

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
spec.metadata["rubygems_mfa_required"] = "true"
end
9 changes: 9 additions & 0 deletions lib/clamo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require_relative "clamo/version"
require_relative "clamo/jsonrpc"
require_relative "clamo/server"

module Clamo
# class Error < StandardError; end
end
5 changes: 5 additions & 0 deletions lib/clamo/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class Client
ANSWER = 42
end
101 changes: 101 additions & 0 deletions lib/clamo/jsonrpc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# frozen_string_literal: true

module Clamo
module JSONRPC
PROTOCOL_VERSION_PRAGMA = { jsonrpc: "2.0" }.freeze

module ProtocolErrors
ErrorDescriptor = Data.define(:code, :message)
SERVER_ERROR_CODE_RANGE = ((-32_099)..(-32_000))

PARSE_ERROR = ErrorDescriptor.new(code: -32_700, message: "Parse error")
INVALID_REQUEST = ErrorDescriptor.new(code: -32_600, message: "Invalid request")
METHOD_NOT_FOUND = ErrorDescriptor.new(code: -32_601, message: "Method not found")
INVALID_PARAMS = ErrorDescriptor.new(code: -32_602, message: "Invalid params")
INTERNAL_ERROR = ErrorDescriptor.new(code: -32_603, message: "Internal error")
SERVER_ERROR = ErrorDescriptor.new(code: -32_000, message: "Server error")
end

class << self
def proper_pragma?(request)
request["jsonrpc"] == "2.0"
end

def proper_method?(request)
request["method"].is_a?(String)
end

def proper_id_if_any?(request)
if request.key?("id")
request["id"].is_a?(String) ||
request["id"].is_a?(Integer) ||
request["id"].is_a?(NilClass)
else
true
end
end

def proper_params_if_any?(request)
if request.key?("params")
request["params"].is_a?(Array)
else
true
end
end

def valid_request?(request)
request.is_a?(Hash) &&
proper_pragma?(request) &&
proper_method?(request) &&
proper_id_if_any?(request) &&
proper_params_if_any?(request)
end

def build_request **opts
# raise if no method present
# raise if params present, but not an array
{ jsonrpc: "2.0",
method: opts[:method] }
.merge({ params: opts[:params] })
.merge(opts.key?(:id) ? { id: opts[:id] } : {})
end

def build_result_response(id:, result:)
{}.merge(PROTOCOL_VERSION_PRAGMA)
.merge({ result: result })
.merge({ id: id })
end

def build_error_response **opts
# raise if no error code present
# raise if no error message present
{ jsonrpc: "2.0",
id: opts[:id],
error: {
code: opts.dig(:error, :code),
message: opts.dig(:error, :message),
data: opts.dig(:error, :data)
}.reject { |k, _| k == :data && !opts[:error].key?(:data) } }
end

def build_error_response_from **opts
# raise unless opts[:descriptor]
opts.merge(
{ error:
{ code: opts[:descriptor].code,
message: opts[:descriptor].message } }
)
.then { |hash| build_error_response(**hash) }
end

def build_error_response_parse_error **opts
opts.merge(
{ error:
{ code: ProtocolErrors::PARSE_ERROR.code,
message: ProtocolErrors::PARSE_ERROR.message } }
)
.then { |hash| build_error_response(**hash) }
end
end
end
end
Loading

0 comments on commit 43636f8

Please sign in to comment.