Skip to content

Commit

Permalink
Merge pull request #5 from bearsunday/ruby
Browse files Browse the repository at this point in the history
Support Ruby client
  • Loading branch information
koriym authored Mar 25, 2024
2 parents 79c1c3c + 077a65d commit 40abc6e
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 10 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ 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: 3.3

- name: Install Thrift
run: |
sudo apt-get update
Expand All @@ -38,6 +43,9 @@ jobs:
- name: Run Composer build tasks
run: composer build:all

- name: Run check.rb
run: ruby client/rb_client/check.rb

- name: Start server in background
run: |
composer serve > /dev/null 2>&1 &
Expand All @@ -54,3 +62,7 @@ jobs:

- name: Run BEAR.Sunday client
run: composer run:bear

- name: Run Ruby client
run: composer run:rb
continue-on-error: true
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A package that allows high-speed access to BEAR.Sunday resources from multiple l

* BEAR.Sunday resources can be utilized across various languages and frameworks
* Seamless access to remote BEAR.Sunday apps from the local BEAR.Sunday app
* Access resources regardless of language (currently supports PHP, Go, and Python)
* Access resources regardless of language (currently supports PHP, Go, Python, Ruby and BEAR.Sunday)
* Fast access with Thrift/Swoole, compared to HTTP
* Can call resources from older versions of BEAR.Sunday apps
* Operates independently of an HTTP server
Expand Down Expand Up @@ -89,10 +89,7 @@ echo $resource->get('page://sekai/?name=World'); // "greeting": "Hello World" fr
method := "get"
uri := "/user?id=1
response, err := ResourceInvoke(hostname, port, method, uri)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Response Code:", response.Code)
fmt.Println("Response Headers:", response.Headers)
fmt.Println("Raw Response JsonValue: ", response.JsonValue)
Expand All @@ -105,16 +102,28 @@ echo $resource->get('page://sekai/?name=World'); // "greeting": "Hello World" fr
method = "get"
uri = "/user?id=1"
response = ResourceInvoke(hostname, port, method, uri)
if not response:
print("Request failed.")
return
print("Response Code:", response.code)
print("Response Headers:", response.headers)
print("Raw Response JsonValue:", response.jsonValue)
print("Response View:", response.view)
```
### Ruby
```Ruby
method = "get"
uri = "/user?id=1"
resource_invoke = ResourceInvoke.new(hostname, port)
response = resource_invoke.call(method, uri)
puts "Response Code: #{response.code}"
puts "Response Headers: #{response.headers}"
puts "Raw Response JsonValue: #{response.jsonValue}"
puts "Response View: #{response.view}"
```
Note: The URI can be a schema as well as a path. Instead of `/user?id=1`, you can specify `page://self/user?id=1` to access both app and page resources.
As you can see, it's easy to access BEAR.Sunday resources from other languages.
Expand Down
1 change: 1 addition & 0 deletions ResourceService.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace php ResourceService
namespace go ResourceService
namespace py ResourceService
namespace rb ResourceService

struct ResourceRequest {
1: string method,
Expand Down
3 changes: 3 additions & 0 deletions client/rb_client/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gem 'thrift', '~> 0.19.0'
13 changes: 13 additions & 0 deletions client/rb_client/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions client/rb_client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
It works locally, but the Ruby client test fails in CI.
If you have Ruby expertise, please help us determine the cause.

ローカルでは動作するのですが、CIではRubyのクライアントのテストが失敗してしまいます。
もしあなたにRubyの専門知識があれば原因究明にご協力お願いします。


8 changes: 8 additions & 0 deletions client/rb_client/check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Check versions of Ruby and gems
puts "Ruby version: #{RUBY_VERSION}"
puts "Installed gems:"
puts `gem list`

# Check environment variables
puts "Environment variables:"
puts ENV.to_h
34 changes: 34 additions & 0 deletions client/rb_client/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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.jsonValue}"
puts "Response View: #{response.view}"
end

main
29 changes: 29 additions & 0 deletions client/rb_client/resource_invoke.rb
Original file line number Diff line number Diff line change
@@ -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, false)
@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.close if @socket.open?
response
end
end
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,28 @@
"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:all": ["@run:go", "@run:py", "@run:php", "@run:bear", "@run:rb"],
"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"]
Expand Down

0 comments on commit 40abc6e

Please sign in to comment.