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.
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 actionsendendNow 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)endThere 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_actionmoduleThirdPartyAppmoduleActionsmoduleMyCustomAction# 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]endendYou can even override the include REST actions using this method