Skip to content

Repository files navigation

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.

Gem VersionMaintainabilityCircleCI

Installation

Add this line to your application's Gemfile:

gem'api_struct'

And then execute:

$ bundle

Or install it yourself as:

$ gem install api_struct

Usage

Initialize APIs routes:

ApiStruct::Settings.configuredo |config|
config.endpoints={first_api: {root: 'http://localhost:3000/api/v1',headers: {'content-type': 'application/json','Authorization': 'Bearer TOKEN'}},second_api: {root: 'http://localhost:3001/api/v1',params: {token: 'Default token'}}}end

Client

Endpoint wrapper

classPostsClient < ApiStruct::Clientfirst_api:postsdefshow(id)get(id)enddefindexgetenddefuser_posts(user_id,post_id=nil)get(post_id,prefix: [:users,user_id])# alias:# get(post_id, prefix: '/users/:id', id: user_id)enddefcustom_path(user_id)get(path: 'users_posts/:user_id',user_id: user_id)endend

Usage:

PostsClient.new.get(1)# -> /posts/1

Returns Resultmonad

# => Success({:id=>1, :title=>"Post"})

Other methods from sample:

post_client=PostsClient.newpost_client.index# -> /postspost_client.user_posts(1)# -> /users/1/postspost_client.user_posts(1,2)# -> /users/1/posts/2post_client.custom_path(1)# -> /users_posts/1/

Entity

Response serializer

classUser < ApiStruct::Entityclient_serviceUsersClientclient_serviceAuthorsClient,prefix: true,only: :index# alias:# client_service AuthorsClient, prefix: :prefix, except: :indexattr_entity:name,:idend
classUsersClient < ApiStruct::Clientfirst_api:usersdefshow(id)get(id)endend
classAuthorsClient < ApiStruct::Clientfirst_api:authorsdefindexgetendend

Usage:

user=User.show(1)# => {"id"=>1, "name"=>"John"}

Call methods from prefixed clients:

users=User.authors_client_index# or# users = User.prefix_index

Response serializers with related entities:

classNetwork < ApiStruct::Entityclient_serviceNetworkClientattr_entity:name,:idattr_entity:state, &:to_symhas_entity:super_admin,as: Userend
classNetworkClient < ApiStruct::Clientfirst_api:networksdefshow(id)get(id)endend

Usage:

network=Network.show(1)# => {"id"=>1, "name"=>"Main network", "state"=>"active", "super_admin"=>{"id"=>1, "name"=>"John"}}network.state# => :active

Dynamic headers:

classAuthdefself.call# Get a token..endend
classAuthHeaderValuedefself.call{"Authorization": "Bearer #{Auth.call}"}endend
classPostClient < ApiStruct::Clientfirst_api:postsdefupdate(id,post_data)put(id,json: post_data,headers: AuthHeaderValue.call)endend

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rubygarage/api_struct.

License

The gem is available as open source under the terms of the MIT License.


RubyGarage Logo

RubyGarage is a leading software development and consulting company in Eastern Europe. Our main expertise includes Ruby and Ruby on Rails, but we successfully employ other technologies to deliver the best results to our clients. Check out our portfolio for even more exciting works!

About

API wrapper builder with response serialization

Topics

Resources

Stars

235 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages