I’m working with a Thrift based ruby API that is essentially compiled Ruby code. I don’t think compilers adhere to single responsibility principle or developer happiness and as a result the core set of useful operations I need to work with are housed in a beastly 6k line class that basically serves as the public facing API. It’s rather cumbersome to work with to say the least, so I’m building a set of wrapper classes to logically seperate the various data types and take care of a lot of the heavy lifting I would have to do and that looks something like this:
# the third party API
class Client
...
def say_foo; end
...
def say_bar; end
end
# my singleton class which serves as the main wrapper for the third party API
class ClientWrapper
def client
@client ||= build_client
end
def method_missing(method_name, *args, &block)
@client.send(method_name, *args, &block)
end
end
# houses all the foo related logic
class Foo
def say_foo
# boiler plate
ClientWrapper.say_foo
end
end
# house all the bar related logic
class Bar
def say_bar
# boiler plate
ClientWrapper.say_bar
end
end
Needless to say this example has been simplified, but that’s more or less the basic idea. Now anytime I wan’t to do some Foo operations it’s as easy as Foo.say_foo
, as opposed to the crazy amounts of boiler plate and repeated code (which I omit above where the comments are) that I would have to deal with if I was working directly with the third party API.
I’m a bit conflicted however, as I have calls to ClientWrapper littered throughout my Foo
and Bar
classes and the only purpose these classes really serve is to dry up my code a bit and keep me sane. Would anybody care to comment on this solution? Is it legitimate use of ruby classes or just pure laziness? I feel as if I should be entitled to some free passes on things like LoD and feature envy when designing a wrapper interface like this simply for the sake of convenience, but perhaps I’m mistaken?