From 1807d591761808173cb290911a7b3c783e5b26c4 Mon Sep 17 00:00:00 2001 From: Stepan Redka Date: Tue, 4 Feb 2020 13:36:09 +0300 Subject: [PATCH 1/2] fix: let entities use multiple clients --- lib/api_struct/extensions/api_client.rb | 7 ++++++- spec/api_struct/entity_spec.rb | 24 +++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/api_struct/extensions/api_client.rb b/lib/api_struct/extensions/api_client.rb index 86f4532..85cad47 100644 --- a/lib/api_struct/extensions/api_client.rb +++ b/lib/api_struct/extensions/api_client.rb @@ -16,7 +16,7 @@ def client_service(*services, **options) def register_service(service, options) options[:prefix] = prefix_from_class(service) if options[:prefix] == true - options[:client_key] = options[:prefix] || :base + options[:client_key] = options[:prefix] || class_name_to_sym(service) @clients[options[:client_key]] = service allowed_methods(service, options).each { |method| define_client_method(method, options) } @@ -38,6 +38,11 @@ def allowed_methods(service, options) rejected = REJECTED_METHODS.concat(Array(options[:except])) service.instance_methods(false).reject { |method| rejected.include?(method) } end + + def class_name_to_sym(klass) + name = klass.to_s + name.gsub(/(.)([A-Z])/,'\1_\2').downcase.to_sym + end end end end diff --git a/spec/api_struct/entity_spec.rb b/spec/api_struct/entity_spec.rb index ec99acc..e03c857 100644 --- a/spec/api_struct/entity_spec.rb +++ b/spec/api_struct/entity_spec.rb @@ -15,6 +15,14 @@ def index(params = {}) end end + class AnotherStubClient < ApiStruct::Client + stub_api 'posts' + + def pull(id) + get(id) + end + end + class StubNestedEntity < ApiStruct::Entity attr_entity :name end @@ -25,13 +33,14 @@ class StubEntity < ApiStruct::Entity client_service StubClient, prefix: :custom client_service StubClient, prefix: :only, only: :index client_service StubClient, prefix: :except, except: :show + client_service AnotherStubClient attr_entity :id, :title, :camel_case has_entity :nested_entity, as: StubNestedEntity has_entities :another_nested_entities, as: StubNestedEntity end - + let(:response) { { title: FFaker::Name.name, 'id' => rand(1..100), another_attributes: FFaker::Name.name } } let(:nested_response) { { name: FFaker::Name.name } } @@ -136,8 +145,9 @@ class StubEntity < ApiStruct::Entity describe '.client_service' do context 'prefix' do context 'empty' do - it 'should register client as base' do - expect(StubEntity.clients[:base]).to eq StubClient + it 'should register client as its class name' do + expect(StubEntity.clients[:stub_client]).to eq StubClient + expect(StubEntity.clients[:another_stub_client]).to eq AnotherStubClient end it 'should define client methods without prefix' do @@ -182,5 +192,13 @@ class StubEntity < ApiStruct::Entity expect(StubEntity).not_to be_respond_to(:except_show) end end + + context 'two clients' do + it 'should define methods from multiple clients' do + expect(StubEntity).to be_respond_to(:index) + expect(StubEntity).to be_respond_to(:show) + expect(StubEntity).to be_respond_to(:pull) + end + end end end From 5a315f8d00f150eaaac44ca47f1341d0a81d9efb Mon Sep 17 00:00:00 2001 From: Stepan Redka Date: Tue, 4 Feb 2020 14:03:47 +0300 Subject: [PATCH 2/2] docs: document changes --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 5d8dd53..df2e770 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +# FORK DESCRIPTION + +This fork fixes a bug that prevents entities from having multiple clients. See https://github.com/rubygarage/api_struct/issues/14 + # ApiStruct **ApiStruct** consists of two main interfaces: `ApiStruct::Client` and `ApiStruct::Entity`. The `ApiStruct::Client` class is aimed at using the same interface for describing requests to different APIs. The `ApiStruct::Entity` enables you to use *ApiStruct* clients in ORM-like style.