Skip to content

Repository files navigation

EZApi

EZApi makes interfacing with third party APIs easy! EZApi handles network requests, responses, and serializes the object into an easy to use ruby object. Standard RESTful actions are included out of the box and you can easily add custom actions.

Usage

Install the gem

gem'ezapi'

Define your API client & models

moduleThirdPartyAppextendEZApi::Clientapi_url'http://www.example.com/api/'# => / at the end is requiredapi_key'XXXXXX'# => requiredclassUser < EZApi::ObjectBasepath'foo'# => to create custom path. Defaults to 'users' in this caseactions[:show,:create,:delete,:update,:index]# => Included RESTful actionsendend

Now we can call http://www.example.com/api/users endpoints

beginuser=ThirdPartyApp::User.show('123')user.first_name# => returns first_name json fielduser.first_name='new name'user.saveThirdPartyApp::User.delete('123')ThirdPartyApp::User.update({first_name: 'Name'})ThirdPartyApp::User.index({page: 1})rescueEZApiError=>eprint(e.message)end

Custom actions

There are two ways of adding custom actions that are not in the included rest

moduleThirdPartyAppclassUser < EZApi::ObjectBase# For class methodsdefself.custom_actionresponse=client.get("#{api_path}/custom_action")# Do whatever you want with the responseend# for instance methodsdefcustom_instance_actionresponse=self.class.client.get("#{self.class.api_path}/custom_action")# Do whatever you want with the responseendendendThirdPartyApp::User.custom_actionThirdPartyApp::User.new.custom_instance_action

Add custom actions to actions array

moduleThirdPartyAppmoduleActionsmoduleMyCustomAction# For instance methodsdefmy_custom_action# Do Somethingend# For class methodsmoduleClassMethodsdefmy_custom_class_action# do somethingendenddefself.included(base)base.extend(ClassMethods)endendendendmoduleThirdPartyAppclassUser < EZApi::ObjectBaseactions[:my_custom_action]endend

You can even override the include REST actions using this method

About

A ruby gem that makes interacting with 3rd party APIs easy!

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages