-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal : Use Class for writing api wrappers #1
Comments
I am not sure about that. Module with Classes, on the other hand, creates more objects, as they have a local memory. I agree in the case of usage of an API for per user or business specific cases. So I guess we need to use a factory pattern or create classes and objects as you said. But when working with
This is why I prefer to address them through the self method.
|
I think your proposal needs to be added as a scenario. As the existing module approach will fail for the example you mentioned. |
Ruby will copy the code when we are using it as mixins , Eg class Controller1
include FancyModule
end
class Controller2
include FancyModule
end Here the contents of fancy module will be copied to both controllers |
We will be creating objects only when it is needed , and it will get Garbage collected after it's use |
Do we really need that ? I think just using class object model is enough , check the snippet I posted above |
We can do the same with class , but the problem is everything will be in global scope . But when we have to change attribute of a particular object , For Eg : Getting leads data using Wufoo api of a particular business module Wufoo
class << self
attr_accessor :domain,api_key
end
end
Wufoo.api_key = ""
Wufoo.domain = "" Class based approach class Wufoo
attr_accessor :api_key,:domain
def initalize(api_key,domain)
@api_key,@doamin = api_key,domain
end
end
wufoo = Wufoo.new("","") Second approach is cleaner and difficult override and misuse |
You haven't read my doc this it is mentioned that
You will be breaking our policy if you include. |
Let's trust the garbage collector when its good enough. For now Ruby doesn't have the best Garbage collector (unless we use JRuby). We always need to reduce the number of objects being made. Unless we tune the GC and interpreter. |
Yes, I agree to use your snippet. Only in that case. when we need to change the API credentials or data per user. |
Compared to the objects are being made by active record , this is lightweight and will be created only when we are calling that method , and it is easily cachable . And GC in ruby isn't is bad as it sounds |
Thanks , but same thing is applicable for most of the apis we are using need these Class GoogleAnalytics
attr_accessor :start_date,:end_date
end
GoogleAnalytic.new(start_date: start_date, end_date: end_date).results is much better than the init hack currently we use |
Yes. But for a heavy volume application, this could lead to a spike in memory. Addressing to self-method in a module are a single object. No matter if its from just 10 requests or 10000 requests. I don't see the advantage of using class based design for use case which doesn't require one. We shouldn't use a persistent object unless we do need one.
The way it's used in clickx and way we defined in the document is different. |
Okay. here I am going to add all my comments for your problem in a single post. Cons of module based approach
Agreed.
Do not include the module, its mentioned there in our doc.
Agreed. But let's say that there is a global provider. Like Again. I agree when we get
Again don't include or exclude the module. Its meant to be used as Going through the advantage you mentioned:
Except one all others should not rise as a problem.
I am not sure what you meant by this one. First of all, we limit the modules to do only API fetch/update or format of data as per our requirement so we stub all the API request and process the subbed responses. So we are testing only if the output we expect is being received for the input we send. We won't be testing the changes it makes to our system here, that has to be done in the method where it is called.
Again not sure what you meant here, as we shouldn't be caching within the methods of the module. Like not calling
There is no problem with testing it from the console. You just need to write Personally, the only thing I can agree to is to use this approach when there are multiple credentials for single service. Where we can't avoid creating multiple objects as by definition they would be multiple objects. |
Context : We are currently recommending module for making making wrapper for external apis . It is making code difficult to maintain
Cons of module based approach
Alternative implementation using class
Advantages
Ref : https://github.com/redpanthers/red-book/blob/master/_entries/5015-api-wrappers.md
The text was updated successfully, but these errors were encountered: