Home > other >  Gem to local libray in Rails
Gem to local libray in Rails

Time:05-14

My boss asked me to write a gem to wrap an API (canvas) consumer for our app, I did it and it works, but in the last minute he asked me not to use the gem approach but to create a Model of it... I have a vague idea of creating the model for the endpoints and put the code in the controller, but the gem is highly "modularized" so to speak, modules requiring modules and classes inheriting from other classes... Is there some way to include everything just as it is and use like a locally installed library? What is the best approach? My gem depends on Faraday for net_http adapter

CodePudding user response:

We often use the Service object approach for this, a good writeup can be found here https://blog.appsignal.com/2020/06/17/using-service-objects-in-ruby-on-rails.html

Basically you put your code in app/services and abstract all config and usage away so you can do:

result = AppServices::SubscriptionService.new({
           subscription_params: {
             subscription: @subscription,
             coupon: params[:coupon],
             token: params[:stripeToken]
           } 
         }).call

So your code could live in app/services/canvas/lib for example.

  • Related