My client has a nodejs SDK that fetches entries using a client that makes http requests. Their api looks like this:

    var Query = Client.ContentType('blog').Query();
      .where("title", "welcome")
      .find().then((response) => resolvePromise etc...))

I have been tasked with mirroring this api but in an idiomatic way in ruby.

My earlier attemps at doing entries = client.entries({content_type: 'blog'}) were rejected. They now want me to now write an api that reads like this:

query = client.content_type('blog').query;
entries = query
              .where("title", "welcome")

Somehow this doesn't make sense to me (perhaps due to the level of misdirections involved) and I don't exactly know why.

If I write a method like content_type('content_uid') on my client class, I am writing a parameterized setter that already breaks rules.

If you are a rubyist, does this look like good api design to you? How can I improve on this?


That looks like a transliteration, and Class modeling and practice in Ruby is much different and holistic than JavaScript. The JavaScript idiom is more of a data representation than Ruby's OO classes and objects.

The plus to transliteration is it is shorter time to deliver a working product, but it won't be idiomatic Ruby in anything other than method names.

This looks like interaction with a query builder. One way you could attack this with a meta approach. Write ruby that implements the interface they want but uses the bridge pattern to already existing ruby query builder (Rails or Active Record).

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.