Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: let entities use multiple clients #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

# <img src='https://github.com/rubygarage/api_struct/blob/master/api_struct.svg' height='60' alt='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.
Expand Down
7 changes: 6 additions & 1 deletion lib/api_struct/extensions/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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
24 changes: 21 additions & 3 deletions spec/api_struct/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 } }

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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