Skip to content

Commit 8d4c54c

Browse files
committed
Add client for stencil to get file set descriptor
1 parent 384da70 commit 8d4c54c

9 files changed

Lines changed: 145 additions & 24 deletions

File tree

clients/ruby/Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ gemspec
77

88
gem "rake", "~> 13.0"
99
gem "http", "4.4.1"
10+
gem 'concurrent-ruby', require: 'concurrent'
11+
gem 'protobuf'
1012

1113
group :test do
1214
gem "simplecov", ">= 0.9"
1315
gem "rspec", "~> 3.0"
1416
gem "rubocop", "~> 1.7"
1517
gem "pry"
18+
gem "google-protobuf"
1619
end

clients/ruby/lib/stencil.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
require "stencil/version"
2-
require "stencil/configuration"
3-
require "stencil/constants"
1+
require_relative "stencil/version"
2+
require_relative "stencil/configuration"
3+
require_relative "stencil/constants"
4+
require_relative "stencil/client"
5+
6+
require "http"
7+
require "concurrent"
8+
require "protobuf"
49

510
module Stencil
611
class Error < StandardError; end
12+
class InvalidConfiguration < Error; end
13+
class InvalidProtoClass < Error; end
14+
class HTTPClientError < Error; end
15+
class HTTPServerError < Error; end
716
end

clients/ruby/lib/stencil/client.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module Stencil
2+
class Client
3+
attr_reader :root
4+
def initialize
5+
begin
6+
@config = Stencil.configuration
7+
validate_configuration(@config)
8+
@root = nil
9+
setup_http_client
10+
load_descriptors
11+
end
12+
end
13+
14+
def get_type(proto_name)
15+
@root.file.each do |file_desc|
16+
file_desc.message_type.each do |message|
17+
if proto_name == "#{file_desc.options.java_package}.#{message.name}"
18+
return message
19+
end
20+
end
21+
end
22+
raise InvalidProtoClass.new
23+
end
24+
25+
private
26+
27+
def validate_configuration(configuration)
28+
raise Stencil::InvalidConfiguration.new() if configuration.registry_url.nil? || configuration.bearer_token.nil? || configuration.bearer_token == "Bearer "
29+
end
30+
31+
def setup_http_client
32+
@http_client = HTTP.auth(@config.bearer_token).timeout(@config.http_timeout)
33+
end
34+
35+
def load_descriptors
36+
begin
37+
response = @http_client.get(@config.registry_url)
38+
if response.code != 200
39+
raise HTTPServerError.new("Error while fetching descriptor file: Got #{response.code} from stencil server")
40+
end
41+
rescue StandardError => e
42+
raise HTTPClientError.new(e.message)
43+
end
44+
45+
@root = Google::Protobuf::FileDescriptorSet.decode(response.body)
46+
end
47+
end
48+
end

clients/ruby/lib/stencil/configuration.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ def initialize
44
@config = ::OpenStruct.new
55
end
66

7-
def registry_urls
8-
@config.registry_urls || []
7+
def registry_url
8+
@config.registry_url
99
end
1010

11-
def registry_urls=(registry_urls)
12-
@config.registry_urls = registry_urls
11+
def registry_url=(registry_url)
12+
@config.registry_url = registry_url
1313
end
1414

1515
def http_timeout
3.63 KB
Binary file not shown.

clients/ruby/spec/spec_helper.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
require 'pry'
1818
require 'stencil'
19+
require 'webmock/rspec'
1920

2021
RSpec.configure do |config|
2122
config.filter_run_when_matching focus: true
@@ -24,11 +25,4 @@
2425
config.order = :random
2526
end
2627

27-
module JSON
28-
module_function
29-
30-
def reader(file_name)
31-
path = File.join(File.dirname(__FILE__), "test_data/#{file_name}")
32-
File.read(path)
33-
end
34-
end
28+
WebMock.disable_net_connect!(allow_localhost: true)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
module Stencil
2+
RSpec.describe Client do
3+
let(:registry_url) { 'http://stencil.test' }
4+
let(:bearer_token) { 'sample-token-123' }
5+
let(:service_type_message) do
6+
service_type = nil
7+
Google::Protobuf::FileDescriptorSet.decode(File.read('spec/data/desc-proto-bin')).file.each do |file|
8+
file.message_type.each do |msg|
9+
if msg.name == "ServiceType"
10+
service_type = msg
11+
end
12+
end
13+
end
14+
service_type
15+
end
16+
17+
context '#get_type' do
18+
subject { Stencil::Client.new }
19+
20+
before(:each) do
21+
Stencil.configure do |config|
22+
config.registry_url = registry_url
23+
config.bearer_token = bearer_token
24+
end
25+
26+
@stencil_get_stub = stub_request(:get, registry_url).
27+
with(
28+
headers: {
29+
'Authorization' => 'Bearer ' + bearer_token,
30+
'Connection' => 'close',
31+
'Host' => 'stencil.test',
32+
'User-Agent' => 'http.rb/4.4.1'
33+
})
34+
end
35+
36+
it 'should raise error if configs are invalid' do
37+
config = Stencil.configuration
38+
config.bearer_token = ""
39+
expect { subject }.to raise_error(Stencil::InvalidConfiguration)
40+
end
41+
42+
it 'should raise error if http client returns error on stencil get api' do
43+
@stencil_get_stub.to_raise(StandardError.new('some error'))
44+
expect { subject.get_type }.to raise_error(Stencil::HTTPClientError)
45+
end
46+
47+
it 'should raise error if http client returns 500' do
48+
@stencil_get_stub.to_return(status: 500, body: 'Internal server error', headers: {})
49+
expect { subject.get_type }.to raise_error(Stencil::HTTPClientError)
50+
end
51+
52+
it 'should raise error for invalid proto type' do
53+
@stencil_get_stub.to_return(status: 200, body: File.new('spec/data/desc-proto-bin'), headers: {})
54+
55+
proto_name = "incorrect"
56+
expect { subject.get_type(proto_name) }.to raise_error(Stencil::InvalidProtoClass)
57+
end
58+
59+
it 'should successfully return proto type' do
60+
@stencil_get_stub.to_return(status: 200, body: File.new('spec/data/desc-proto-bin'), headers: {})
61+
62+
proto_name = "com.gojek.esb.types.ServiceType"
63+
actual_type = subject.get_type(proto_name)
64+
expect(actual_type).to eq(service_type_message)
65+
end
66+
end
67+
end
68+
end

clients/ruby/spec/stencil/configuration_spec.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ module Stencil
33
let(:configuration) { Stencil.configuration }
44

55
it 'should set registry urls correctly' do
6-
expect(configuration.registry_urls).to eq([])
6+
expect(configuration.registry_url).to eq(nil)
77

8-
expected_value = ['http://localhost:3000']
9-
configuration.registry_urls = expected_value
10-
expect(configuration.registry_urls).to eq(expected_value)
8+
expected_value = 'http://localhost:3000'
9+
configuration.registry_url = expected_value
10+
expect(configuration.registry_url).to eq(expected_value)
1111
end
1212

1313

@@ -42,19 +42,17 @@ module Stencil
4242
expect(configuration.bearer_token).to eq(expected_bearer_token)
4343
end
4444

45-
46-
4745
describe '#configure' do
4846
let(:refresh_enabled) {true}
4947
let(:refresh_ttl_in_secs) {60000}
50-
let(:registry_urls) {["abc.com/latest"]}
48+
let(:registry_url) {"abc.com/latest"}
5149
let(:token) {"ABCD1234"}
5250
let(:bearer_token) {"Bearer " + token}
5351
let(:http_timeout) {6000}
5452

5553
before(:each) do
5654
Stencil.configure do |config|
57-
config.registry_urls = registry_urls
55+
config.registry_url = registry_url
5856
config.bearer_token = token
5957
config.refresh_enabled = refresh_enabled
6058
config.refresh_ttl_in_secs = refresh_ttl_in_secs
@@ -65,7 +63,7 @@ module Stencil
6563
subject { Stencil.configuration }
6664

6765
it 'should set configuration correctly' do
68-
expect(subject.registry_urls).to eq(registry_urls)
66+
expect(subject.registry_url).to eq(registry_url)
6967
expect(subject.bearer_token).to eq(bearer_token)
7068
expect(subject.refresh_enabled).to eq(refresh_enabled)
7169
expect(subject.refresh_ttl_in_secs).to eq(refresh_ttl_in_secs)

clients/ruby/stencil.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
2828
spec.require_paths = ["lib"]
2929

3030
spec.add_development_dependency "bundler", "~> 1.17.3"
31+
spec.add_development_dependency "webmock", "~> 3.14.0"
3132
end

0 commit comments

Comments
 (0)