Last we left, I initialized a simple Rails API that can create and dish out positive quotations. Next, I defined the resources that my application would deal with, and threw together the data models that tied them together. Let’s keep this simplicity train rolling with the next logical step: the user API.

Image for post
Image for post
Something like this, but without all of the buttons. Also, nothing like this.

to_json

Before we get to the business, I’m going to make my life easier by adding a custom to_json method for the Quote class.

# quote.rbdef to_json(opts = {})
{
id: id,
content: content,
attribution: attribution,
category: category.title,
topic: topic.title
}
end

Under the hood, this is called when we render a quote as JSON in the controllers. The render method will pass an options hash to the function, so we stub out the parameter to avoid an argument error. …


in order from most to least sexy.

Image for post
Image for post
^^This^^ or “An eye for eye makes the whole world blind.” You be the judge.

Let’s leave the modeling to the data.

Last we left, the quotes API was able to both be told to create a quote and call for a single quotation. As great as that is, I would like to be able to add more information to the quote in a maintainable way.

The book that I will be using to source quotes also provides a category and sub-category (I’ll call them “topics”) for each quote.

For any one category, there are many topics. For any one topic, there are many quotes. For any one quote, there is only one topic and category.

In object relational terms, category:topics are one:many, topics:quotes are one:many. A quote will be able to tell its category based on its topic. …


I’ve been programming in Rails for years now, but have yet to make a publicly consumable API. Especially not one using the built in Rails API functionality. Today that ends. Read ahead as I document building a pretty simple API to be consumed by some projects I’ve had in mind.

Twitter has its place in the world, but truth be told I’m not a big Tweeter.

A few weeks ago, I looked into making a Twitter bot to disclose my thoughts on a regular basis. The bot would posts a tweet daily at 5pm if I hadn’t already. …

About

Alexa Anderson

Fullstack Developer and Code Yogi

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store