diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 88f05f3..e9815a5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -26,6 +26,12 @@ jobs: with: python-version: '3.x' # Version range or exact version of a Python version to use, using SemVer's version range syntax + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: 2.7 + bundler-cache: true + - name: Install Thrift run: | sudo apt-get update @@ -54,3 +60,6 @@ jobs: - name: Run BEAR.Sunday client run: composer run:bear + + - name: Run Ruby client + run: composer run:rb \ No newline at end of file diff --git a/ResourceService.thrift b/ResourceService.thrift index ee0fd72..43d9e62 100644 --- a/ResourceService.thrift +++ b/ResourceService.thrift @@ -3,6 +3,7 @@ namespace php ResourceService namespace go ResourceService namespace py ResourceService +namespace rb ResourceService struct ResourceRequest { 1: string method, diff --git a/client/rb_client/Gemfile b/client/rb_client/Gemfile new file mode 100644 index 0000000..4c2d825 --- /dev/null +++ b/client/rb_client/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gem 'thrift', '~> 0.19.0' diff --git a/client/rb_client/Gemfile.lock b/client/rb_client/Gemfile.lock new file mode 100644 index 0000000..e7769f3 --- /dev/null +++ b/client/rb_client/Gemfile.lock @@ -0,0 +1,13 @@ +GEM + remote: https://rubygems.org/ + specs: + thrift (0.19.0) + +PLATFORMS + arm64-darwin-22 + +DEPENDENCIES + thrift (~> 0.19.0) + +BUNDLED WITH + 2.4.22 diff --git a/client/rb_client/main.rb b/client/rb_client/main.rb new file mode 100644 index 0000000..2c96352 --- /dev/null +++ b/client/rb_client/main.rb @@ -0,0 +1,42 @@ +# main_client.rb + +$LOAD_PATH.unshift(File.expand_path('./gen_rb/')) + +require 'json' +require_relative './gen_rb/resource_service' +require_relative 'resource_invoke' + +def main + if ARGV.length < 4 + puts "Usage: ruby main_client.rb hostname port method uri" + return + end + + hostname = ARGV[0] + port = ARGV[1].to_i + method = ARGV[2] + uri = ARGV[3] + + resource_invoke = ResourceInvoke.new(hostname, port) + response = resource_invoke.call(method, uri) + + if response.is_a?(String) && response.start_with?("Error:") + puts response + return + end + + puts "Response Code: #{response.code}" + puts "Response Headers: #{response.headers}" + puts "Raw Response JsonValue: #{response.json_value}" + + begin + value = JSON.parse(response.json_value) + puts "Response Value: #{value}" + rescue JSON::ParserError => e + puts "Error Unmarshaling JSON: #{e.message}" + end + + puts "Response View: #{response.view}" +end + +main \ No newline at end of file diff --git a/client/rb_client/resource_invoke.rb b/client/rb_client/resource_invoke.rb new file mode 100644 index 0000000..56d31bd --- /dev/null +++ b/client/rb_client/resource_invoke.rb @@ -0,0 +1,29 @@ +require 'thrift' +require './gen_rb/resource_service' +require './gen_rb/resource_service_types' + +class ResourceInvoke + def initialize(host, port) + @socket = Thrift::Socket.new(host, port) + @transport = Thrift::BufferedTransport.new(@socket) + @protocol = Thrift::BinaryProtocol.new(@transport) + @client = ResourceService::ResourceService::Client.new(@protocol) + end + + def call(method, uri) + @socket.open unless @socket.open? + request = ResourceService::ResourceRequest.new(method: method, uri: uri) + + begin + response = @client.invokeRequest(request) + rescue => e + puts "Error: #{e.message}" + puts "Backtrace: #{e.backtrace.join("\n")}" + end + rescue Thrift::Exception => e + "Error: #{e.message}" + ensure + @socket.closethr if @socket.open? + response + end +end diff --git a/composer.json b/composer.json index 14d6ae2..21906c8 100644 --- a/composer.json +++ b/composer.json @@ -36,23 +36,27 @@ "process-timeout":0 }, "scripts": { - "build:all": ["@build:thrift", "@build:go", "@build:py", "@build:php", "@build:bear"], + "build:all": ["@build:thrift", "@build:go", "@build:py", "@build:php", "@build:bear", "@build:rb"], "build:thrift": [ "rm -rf client/go_client/gen*", "rm -rf client/py_client/gen*", + "rm -rf client/rb_client/gen*", "thrift -r --gen php:server ResourceService.thrift", "thrift -r --gen go -o client/go_client ResourceService.thrift && mv client/go_client/gen-go client/go_client/gen_go", - "thrift -r --gen py -o client/py_client ResourceService.thrift && mv client/py_client/gen-py client/py_client/gen_py" + "thrift -r --gen py -o client/py_client ResourceService.thrift && mv client/py_client/gen-py client/py_client/gen_py", + "thrift -r --gen rb -o client/rb_client ResourceService.thrift && mv client/rb_client/gen-rb client/rb_client/gen_rb" ], "build:go": "cd client/go_client && go build", "build:py": "cd client/py_client && python3 -m venv ./ && . ./bin/activate && ./bin/pip install thrift", "build:php": "composer update && cd tests/Fake/app && composer update", "build:bear": "cd client/bear_client && composer dump", + "build:rb": "cd client/rb_client && bundle install", "serve": "php bin/serve.php", "run:go": "./client/go_client/go_client 127.0.0.1 9090 get \"page://self/?name=World!\"", "run:py": "./client/py_client/bin/python3 ./client/py_client/main.py 127.0.0.1 9090 get \"page://self/?name=World!\"", "run:php": "php ./client/php_client/main.php get \"page://self/?name=World!\"", "run:bear": "php ./client/bear_client/main.php", + "run:rb": "cd ./client/rb_client && bundle exec ruby main.rb 127.0.0.1 9090 get \"page://self/?name=World!\"", "cs": ["phpcs --standard=./phpcs.xml src"], "cs-fix": ["phpcbf src bin --standard=./phpcs.xml"], "test": ["php bin/serve.php &", "sleep 1", "@run:go", "@run:py", "@run:php", "@run:bear"]