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. Everybody would win. …


with no space to spare

Problem Statement: Given a list of integers with the following characteristics, find at least one integer that appears more than once in the list.

  1. The integers are in the range 1..n
  2. The list has a length of n + 1

Constraint: Space complexity of O(1)

Analysis: The most obvious solution is to iterate through the list and check each element against every other element until there’s a match.

That’s not very efficient at all. O(n²) time, but space complexity is maintained.

But we can do better.

Solution: In order to lower time complexity, we can’t think in terms of elements. There is one source of sorted information with this list: the indexes. And because we know that our list necessarily contains integers only up to the length of the list, we can mentally map the idea of ‘integers’ to the list’s indexes. …


with the same set of digits

An Algorithm

Problem Statement: Given a number, find the next higher number which has the exact same set of digits as the original number. For example: Given 545543, return 553445.

Analysis: The most brute force way to solve this is to calculate all permutations of the digits present, sort them, and then compare the original number against each until the first one that is larger is reached. The time complexity here is insane. O(n *n!). And that’s not counting the sort.

Solution: There is actually an O(n) solution to this, just takes a little human ingenuity. The idea here is that given 545543, no matter how much jumbling we do to the ending -5543, we will not produce a larger number. …


Welcome back to this quick demo of making breakfast with SOLID design principles. If you haven’t read Part 1, check it out. It’s pretty swell. Here’s a quick recap:

Single Responsibility Principle: An object should only encapsulate one functionality. In this example, the Egg class’ one functionality is being an egg. If I were to ask the Egg about itself, it won’t tell me a list of egg recipes or over what temperature to cook it. Writing a function to do that would be simple, but that would betray the SRP.

Liskov Substitution Principle: An object should not alter the interface of any parent class from which it inherits. For variance in examples, let’s consider a class MusicalInstrument. This class would have a play function, def play, as part of its public interface. For every type of musical instrument that inherits from this class, they must also have play as part of their public interface. Obviously, a trombone and a drum set would have different implementation details in their respective play functions; the point of this principle is that any object expecting to receive a MusicalInstrument to interact with shouldn’t have to care about what type it receives. …


S.O.L.I.D is an acronym for the first five object-oriented design principles by Robert Martin. These principles make it easy for developers write clean, modular code that is easy to read and refactor. The acronym stands for Single-responsiblity principle…Open-closed principle…Liskov substitution principle…Interface segregation principle…Dependency Inversion Principle…And together they make your software SOLID.

For my first years of teaching myself development, I never grasped these principles in a replicable way. Intellectually sure, but in the text editor trenches, programs were 100s+ lines of code masterscripts, classes initialized their own dependents, and inheritance was a idealized dream of the future. …


An algorithm

Problem Statement: Given an increasingly sorted array and a number s, please find two numbers whose sum is s. If there are multiple pairs with sum s, just output any one of them.
For example, if the inputs are an array [1, 2, 4, 7,11, 15] and a number 15, output two numbers 4 and 11 since 4+11=15.

Assume the requested value will always be possible to attain.

Analysis: The most brute force way to solve this is to compare each element one at a time against every other element until we reach the value we requested. …


I admit it — sometimes, I use my file explorer to dig through directories. While I have Sublime Text symlink’d as subl for easy access through my terminal, whenever I open a [‘txt’, ‘rb’, ‘js’] file from explorer, the default text editor is gedit. Gedit, a plain text editor, isn’t terrible. But it also isn’t my full featured, highly configurable, go-to Sublime Text 3.

Image for post
Image for post
Unfortunately, ST3 cannot be configured to come with self-realization or Channing Tatum. We take what we can get.

Setting Sublime Text as Default

This is specific to Ubuntu 14.04 and Sublime Text 3, but it will only take minor edits to configure for Ubuntu >14.04 and/or ST2.

One:

Set up a sublime.desktop file in ~/.local/share/applications Sublime comes with this file in the same directory as the sublime_text executable (the sublime_text application directory), so you may need to change the first argument to match your file structure. …


After spending the last year and a half working with ERB, React, handlebars, and various other ways to avoid the raw HTML grind, I’ve gone back to basics to sharpen my tool set.

To start, I worked through Shay Howe’s HTML & CSS Course 101. It took a while (hence the delay in posting), but while working through it with a fine tooth comb, I came across some information that I had forgotten about as well as picked up some new stuff. I’ll go over some of the highlights.

Image for post
Image for post
  • To open a link in a new window, set target attribute to “_blank”.

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