Should this method return nil?

Hey everyone,

I’m working on a wrapper for a REST API at the moment and curious to know what the best practices are for the following situation. I have several resource objects which contain methods for performing CRUD operations on that resource, however the API i’m wrapping returns empty response bodies for PUT requests.

What’s generally the best thing to be returning in this situation? It feels wrong to return nil or "" for a successful request. Would it make more sense to return the instance of that resource, i.e. self? I was also considering maybe returning true but it also felt weird since unsuccessful requests raise an exception, which is caught elsewhere. So my code would look like:

def update(attributes)
  http_client.put(url, attributes)
  true
end

I may be overthinking this and maybe they’re all valid options but i’m interested to hear what people think is the best approach. (And apologies for the awful topic name :slight_smile: )

Cheers

1 Like

@cakejelly I would personally expect this method to behave similarly to how an ActiveRecord object does. In which case, update returns true/false, but also mutates the object you’re working with.

ex:

person = Person.create(name: "Jake")
# => <Person name: "Jake">

person.update(name: "Cake")
# => true

person.name
# => "Cake"

Perhaps that’s just me tho! :slight_smile: