DEV Community

Cover image for Ruby classes seem pretty cool
danielarmbruster0314
danielarmbruster0314

Posted on

Ruby classes seem pretty cool

I just started to get a handle on conceptualizing Ruby classes and am feeling like there is a lot of potential here. I have yet to see how to apply them when it comes to creating a restful API though. What is the application of classes in terms of a REST Api?

Latest comments (8)

Collapse
 
kadeesterline profile image
Kade Esterline

A class is a template for objects, if you were building an API that queried a database of movies you would probably have a classes for things like Movies, Characters, and Actors. You would have tables in your database that coincide with each class you define.

Collapse
 
danielarmbruster0314 profile image
danielarmbruster0314

ohhhhhh that makes sense, thank you this adds a lot of clarity, thank you.

Collapse
 
jeremyf profile image
Jeremy Friesen

In Ruby, I like to think of one class responsible for the content of the response document sent back to the requester:

class Api::Posts
  def to_json
  end
end
Enter fullscreen mode Exit fullscreen mode

And at the GET /api/posts endpoint, your response document is the results of calling Api::Posts#to_json. This is a simplification.

Collapse
 
danielarmbruster0314 profile image
danielarmbruster0314

Its all coming together now this is so sick! can't wait to try this out. thank you.

Collapse
 
gumatias profile image
Gustavo Matias

(non-tricky questions) (no need to google the answer)

Could you elaborate your current understanding of what a class is?

Would you mind sharing an example you see without REST involved?

What is REST in your view? How do you make sense of it?

Collapse
 
danielarmbruster0314 profile image
danielarmbruster0314

Sorry, guess that description can be misleading because of how I am referring to rest Api and what Rest Api is defined as. I mean in terms of a making Api's and or making Api calls. To re-phrase my question, What role does Ruby classes play in handling Api creation and Api requests?

Collapse
 
gumatias profile image
Gustavo Matias • Edited

All good :)

What role does Ruby classes play in handling Api creation?

In our context, a Ruby class is one of the prerequisites to create an API

What role does Ruby classes play in handling Api requests?

The request comes as plain text. But then it gets transformed into an object that is made of a Ruby class. Like a Hash for example.

You mentioned classes and handling requests. So here's what I think this means in practice in Ruby on Rails: ActionDispatch::Http::Request.

The mechanics of it uses Controllers in Rails case. Assuming that's what you're learning. I recommend a read at Rails guides for further clarification.

Thread Thread
 
danielarmbruster0314 profile image
danielarmbruster0314

This is exactly what I was looking for thank you.