ActiveRecord to JSON API Part 3: Handling Filtering

Did you know that Puma has a 10,240 character query string limit? We do now.

In the process of building our internal JSON API, we found two issues with handling filtering:

  1. Query string limitations
  2. Querying for an array of IDs when some values were nil

Background

We were building an internal JSON API using the following libraries:

For more information on our goals and set up for the internal API, see part 1 of this series.

Query Strings

Given that the JsonApiClient filtering methods use an HTTP GET with query string parameters, character limits quickly became a concern for us. How could we ensure that some of our lengthier queries weren’t exceeding the limit and failing?

The simplest solution to us was to make the filters a POST request, even if that was a divergence

Continue reading “ActiveRecord to JSON API Part 3: Handling Filtering”

Write A Reduce Function From Scratch

JavaScript's Array object has a built-in reduce method. This is a handy,
yet underutilized method. Let's take a closer look at what reduce does and
see if we can implement it from scratch.

Reduce?

Here is what
MDN
has to say about reduce:

The reduce() method executes a reducer function (that you provide) on each
member of the array resulting in a single output value.

So, we can take an array of things and turn it into something else using a
function that we get to define.

const sum = list => list.reduce((total, val) => total + val, 0);

sum([1,2,3,4]) // => 10

The reduce method can seem pretty limiting at first when we read that it
produces "a single output value." That makes it sound like all it is good
for

Continue reading “Write A Reduce Function From Scratch”

Books that Made me Happy 2018

Books that made me happy 2018

Well, I failed in my plan to get this out by the end of January, but here are the books I liked in 2018. Unlike past years, here they all are in one post, I think it’s about 25. I tried, with mixed success to not write six gazillion words about each book.

Enjoy!

My favorite book of the year

The Calculating Stars / The Fated Sky by Mary Robinette Kowal— if you have ever liked anything I’ve recommended ever, there’s a good chance you’ll like this. After a meteor wipes out the Eastern seaboard in 1952, the space race becomes a race against climate change, and humanity goes to the moon, and then Mars. These are books with lots of smart people solving hard problems under pressure. It’s like The Martian but with fewer potatoes, and more punch cards and representation. It’s amazing.

Continue reading “Books that Made me Happy 2018”

ActiveRecord to JSON API Part 2: Solving Pagination

While building an internal JSON API for a Rails application, we came across a few issues with pagination.

  • Making Kaminari work
  • Resolving problems with pagination link parameters
  • Enabling pagination while still allowing users to get everything

Background

We were building an internal JSON API using the following libraries:

For more information on our goals and set up for the internal API, see part 1 of this series.

Kaminari

The application we were working on was using Kaminari for pagination, and one of the first things we noticed during our switch from ActiveRecord to JsonApiClient::Resources was that pagination no longer worked, but only when the page number param was not present.

Upon investigating the source code for JsonApiClient, we noticed that when the page number param was blank during pagination, the page was resolved to 0,

Continue reading “ActiveRecord to JSON API Part 2: Solving Pagination”

Use Yarn Like a Pro

Yarn is a fast, reliable, and secure alternative to NPM. In this post, we'll look at techniques to use Yarn to its fullest.

The expectations of life depend upon diligence; the mechanic that would
perfect his work must first sharpen his tools. —Confucius

Yarn is a great tool. I'm won't wade into the Yarn vs. NPM debate,
because the tradeoffs are always in flux. I just say that I've been
using it for two years on a variety of projects, and have nothing but praise.

Part of that journey has been learning Yarn's nuances, and today I'm happy to be
sharing them with you. Some of these epiphanies came to me through my own work,
and some via our company knowledge base, Today I
Learned
.

I'm going to skip the setup and some common Yarn commands; head over to Yarn's
official site
to boot up. All of these commands were

Continue reading “Use Yarn Like a Pro”

ActiveRecord to JSON API Part 1: Our Approach

In a recent Rails project, we were tasked with the migration of a group of ActiveRecord models to a microservice that had not yet been built.

This raised an interesting question for us: how do we migrate data out of an application when we don’t have anything to replace it with?

Reasoning

The originally proposed solution was to build a layer between the controllers and models that would delegate out to the ActiveRecord models for now, and then when the new microservice was done, would delegate out to an ActiveResource object. This solution raised some concerns for us.

  • How would we update the models and jobs that interacted with the models being extracted, when the layer was only present in the controllers?
  • How do we know the performance impact of moving from a database record to fetching data from an API?
  • How do we know we’ve properly converted everything when

    Continue reading “ActiveRecord to JSON API Part 1: Our Approach”

How To Quit Vim

A lot of people would have you believe that Vim is near impossible to quit.
I'm here to dispel the rumors and show that there are quite a few ways to
quit Vim.

The Basic Quit

First thing if you start up a new Vim session with just vim, you'll see a
sort of splash screen. Below the title and copyright information is this
line:

type  :q<Enter>               to exit

This is a good place to start because it is perhaps the most straightforward
way to quit a Vim session — the :q or :quit command. It does come with
some minor caveats.

Here is the first caveat. If the current file (or some other file open in
another buffer) has been edited, but not yet saved, then Vim will stop you
from quitting. It wants to make sure you don't accidentally lose your
changes.

If you're sure you want

Continue reading “How To Quit Vim”

Integrating with Google Calendar as a Service App

Recently, I was working on a project where the goal was to have a shared calendar for the application that all the users would have access to, but no individual user would have ownership of. This seemed like the perfect use case to have a service app owned calendar that gave access to users based on email addresses.

I started by going to the Google developers console, creating a new project, creating service app account credentials, and enabling the Google Calendar API.

I used two libraries for setting up the integration:

Basic App Set Up

I was working with users that had access to many calendars, and those calendars had many events.

class User < ApplicationRecord
  has_many :calendar_users
  has_many :calendars, through: :calendar_users
end

class Calendar < ApplicationRecord
  has_many :calendar_users
  has_many :users, through: :calendar_users
  has_many 
Continue reading "Integrating with Google Calendar as a Service App"

Generate Images for Instagram

Followers of Hashrocket on social media will note our renewed presence
on Instagram, punctuated by a collection of custom images. In this post, I
will try to reverse-engineer these images using ImageMagick.

Hashrocket project manager Suzanne Erin recently started creating a series of
engaging images for our Instagram account to pair with blog posts. An example:

handmade image

Soon, I started to wonder if I could reverse-engineer these
images with code. Here’s what I came up with:

generated image

In this post, I'll explain how I created this image.

Concept

I wanted a CLI accepting two arguments: a background image and a blog post
title. With no other interaction, it should return a finished image.

Part of what powers this idea are the folks at Unsplash, who provide wonderful free photographs. I'm a huge
fan of their service, and always try to give the appropriate credit. These images would work with stock photography,

Continue reading “Generate Images for Instagram”

Session Zero for Software Teams

I recently spoke at our Hashrocket Miniconf about a concept I learned from the tabletop role-playing game community: Session Zero.

Before I get into Session Zero, let’s set the stage. What is tabletop role-playing? Think Dungeons and Dragons. I know we’re all some flavor of nerd here on the internet, but if you’re not familiar with these types of games, here’s a simplified description: Generally a tabletop role-playing game (RPG) consists of participants who play characters with different attributes and abilities. Players describe their characters actions and roll dice to see whether they succeed. A campaign in an RPG is a series of challenges or situations that are presented to the players as they progress through a fictional setting. A Game Master (GM) sets the scenes, presents the challenges, and adjudicates the rules for the players. A campaign is a longer term game played out over a series of sessions.

Continue reading “Session Zero for Software Teams”

SQL: Inner Join vs. Outer Join

A standard join in SQL is implicitly an inner join. This means that only
records that can be matched on both sides of the join will be included in
the result set.

Here is an example of that:

> select * from table_a join table_b on table_a.letter = table_b.letter;
 name  | letter |  name  | letter
-------+--------+--------+--------
 Brian | A      | Dunn   | A
 Derek | C      | Parker | C

That same query could be written with inner join.

We can alternatively write an outer join which will ensure that the
subject of the join has all of it's rows included even if there isn't a
matching row from the other table.

So, which table is the subject of the join? If it is a left outer join,
then it is the table on the left of the join. If it is a right outer

Continue reading "SQL: Inner Join vs. Outer Join"

Friday Lunch at Hashrocket Chicago

Friday is a special day at Hashrocket.

Friday is our open source day. After spending the first half of the day working on regular client projects, Rocketeers spend the afternoon honing their skills by contributing to open source, working on side projects, brushing up on new languages, or working on company initiatives. The other very special part about Friday is lunch. We kick-off the open source afternoon by dining out together as a team. This is an important tradition, because we are divided across many separate project teams and there aren’t always as many opportunities for everyone to have a conversation together as we would like. This helps us stay connected to each other.

In Chicago our office is nearby the West Loop, known for its restaurants and cafes. This means that we have many delicious dining options. How to choose? If you’ve ever tried to get 9 people to

Continue reading “Friday Lunch at Hashrocket Chicago”

5 JavaScript Object Destructuring Tricks

The ES6 destructuring syntax is quite flexible. It allows us to do a lot of
nice things with objects and arrays that in the past would have taken many
distracting lines of code.

Let's look at five handy destructuring tricks for objects.

Trick #1

Remove an item from an object

// const data = { a: 1, b: 2, thingToRemove: "bye" };

const { thingToRemove, ...rest } = data;

console.log(rest); // { a: 1, b: 2 }

This is a concise way of getting a new copy of an object with a specific
key/value pair removed. Note that it is immutable and you are free to
specify as many keys as you'd like separated from the ...rest object.

Trick #2

Rename things in a destructuring

// const data = { coordinates: { x: 5, y: 6 }};

const { x: xCoord, y
Continue reading "5 JavaScript Object Destructuring Tricks"