'Gem to local libray in Rails
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
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Benjamin Udink ten Cate |