Ben Lovell

Fast, Testable and SANE APIs

By now, we've all written JSON APIs in Rails. But how do you write fast, testable and sane APIs? I'll guide you through the trials of designing and building awesome, scalable APIs. We'll cover rails-api, activemodelserializers, and all the associated goodness that our ecosystem has to offer.

I'll speak on the approaches to authentication, how to ensure we remain good REST/HTTP citizens and maybe if I have time I'll share some of my top secret beard grooming tips!

Ancient City Ruby 2014

00:00:00.120 Hello, everybody! How are we doing? I'm Ben Lovell.
00:00:11.580 I own a piece of every country in the world except for America.
00:00:17.460 So, yeah! You can tweet me if you like or find me on GitHub.
00:00:23.220 You can even Google Plus me if you want.
00:00:30.420 Leon was talking about this whole social networking theme and the dystopia surrounding it.
00:00:37.140 What Google has done is make it nearly impossible for people to follow you, so everything you say is effectively to yourself.
00:00:44.460 Right, Leon?
00:00:49.559 I’m going to get so drunk tonight! But that's not my GitHub timeline.
00:00:58.079 So yeah, look at me! Wow! You can see in the middle picture that I actually grew into these teeth and these ears.
00:01:06.000 The one on the left, though, hell!
00:01:17.640 Bring it! I have killed and I will kill again!
00:01:25.860 I like to tell a few jokes, I like beards, and I like swearing.
00:01:31.619 I love my mum. She said something really funny to me the other day.
00:01:38.759 She said, "Of all of your siblings, I have five brothers and one sister, so there's a ton of us.
00:01:44.640 You're not the smartest one," but you can make people laugh.
00:01:53.159 She said I needed to play to my skills.
00:01:58.200 So, I'm probably not going to teach you anything today.
00:02:03.420 Well, I might teach you one thing: Don't invite me to your conference to speak!
00:02:10.440 Some of my interests include race driving; here I am driving my car, which is great fun.
00:02:18.300 I'm not as good as DHH. You can see him there crashing his car.
00:02:25.020 I was actually at this race, and what you can't hear is that when he crashed, he lifted his visor and exclaimed, "Oh my gosh!".
00:02:31.080 I'm quite adept at various programming languages—Visual Basic is amazing.
00:02:40.379 You've got the Professional Edition for the professionals and the Enterprise Edition for people who own suitcases and briefcases.
00:02:46.200 It's the best. I work for One Minus One.
00:03:00.420 You're probably going to ask, "Why don't you call yourself Zero?" Well, I've heard that one a million times!
00:03:06.120 I work in Farnham, which is a place that none of you have probably ever heard of.
00:03:11.459 You're probably not going to travel there, and I'd advise against it.
00:03:17.700 That’s me! I like Star Wars and pair programming—sometimes after taking important business calls.
00:03:24.659 But, you know, we deal with it!
00:03:30.480 I've been traveling a lot lately and was recently in the Philippines.
00:03:36.360 It was really nice and hot, but I kind of miss home.
00:03:41.760 While browsing the internet for whimsical things that reminded me of home, I found some interesting news.
00:03:47.159 For example, we've got some interesting headlines, like the Supreme resilience of windshields vs. the resilience of ornamental snails.
00:03:52.379 Who writes this stuff? We've also got ducks taking refuge from the rain and fury over giant hedges. And there's a headline about seagulls turning off TVs in Exeter.
00:04:04.560 It all started with this first student attacked by a seagull, which prompted an angry seagull strike back.
00:04:10.560 And that brings us to building awesome JSON APIs!
00:04:16.320 I got your segues; I got your segues! I'm going to talk about a bunch of topics, interspersed with the jokes I tell.
00:04:21.600 We’ll cover Rails API, which is pretty awesome, and JSON APIs as well.
00:04:28.199 You're familiar with bike settings and ActiveModelSerializers because JSON can be a bit tedious.
00:04:34.860 We will also touch upon HTTP and REST.
00:04:40.020 I'm going to need a rest in a minute!
00:04:46.280 Moreover, we have Etsy and some stuff that just cannot be categorized.
00:04:52.860 And there are unrelated pictures that will remind me to take a break—how are we doing?
00:04:58.560 Great! Have you learned anything about seagulls?
00:05:04.199 Yeah, bastards!
00:05:10.440 In my limited worldview as a Visual Basic programmer, there are some good components of API design.
00:05:16.199 APIs must be fast, standardized, and intuitive, meaning they should be easy to discover and use.
00:05:22.979 This brings us to JSON API, an emerging standard.
00:05:28.320 Its aim is to cut out the need for bike settings; it's maintained by Haley Dunn.
00:05:34.199 This is particularly interesting because you can contribute to it.
00:05:40.500 If you've written a JSON API, you might recognize this format.
00:05:46.199 The header section with links is similar to hypermedia, which I’ll elaborate on shortly.
00:05:52.199 There are some dictates by the JSON API standard that are mostly common sense, aimed at enhancing API usability.
00:05:59.940 You should treat singular resources like collections, with proper pluralization and nesting.
00:06:06.900 The JSON API reserves only a few keywords, such as 'links' and 'ID'.
00:06:12.600 This structure allows you to link related resources by their IDs.
00:06:18.960 You can use a URI expansion as per RFC guidelines.
00:06:25.199 Let me tell you a quick story about the Queen of England.
00:06:33.300 Back when I was a junior VB programmer, I worked on a project to move the royal.gov.uk website from a Unix environment to Windows.
00:06:39.860 This was while I worked for a consultancy known for ASP.
00:06:46.199 Here’s the fun part: the Royal Family always has their obituaries in draft form.
00:06:52.560 After I shipped my feature, I went home for the weekend, only to get paged with a problem.
00:07:02.640 Apparently, all draft obituaries were accidentally deployed, and I killed them all!
00:07:09.180 But back to JSON API—the format allows you to express compound and side-loaded documents.
00:07:14.640 This is useful since the fastest form of HTTP is no HTTP at all, which benefits API consumers.
00:07:22.500 The JSON API specification outlines how to include associated documents.
00:07:28.460 There are primarily three levels of associations: first-level, association of associations, and unrelated documents.
00:07:34.560 The side-loaded documents appear under a named, pluralized section in the JSON response.
00:07:40.020 It's essential to include a links section when you're side-loading resources to maintain clarity.
00:07:46.320 When you read the JSON API standard, it uses clear RFC language.
00:07:49.500 You can substitute words like 'should' and 'must' for more playful alternatives if you wish.
00:07:55.920 Apart from JSON API, active model serializers help make API responses more efficient.
00:08:02.699 You can use rack-test without bringing in the whole view layer.
00:08:09.060 So when testing your APIs, you can verify response codes based on the data and URI.
00:08:16.380 It's crucial to maintain compatibility for versioning as well.
00:08:22.739 As a piece of advice concerning versioning, once you've built your API, you shouldn't change it.
00:08:31.080 If there are necessary changes, make them additive to maintain backwards compatibility.
00:08:38.340 Your tests are vital for proving this compatibility.
00:08:43.260 That wraps up my brief overview of creating fast, testable, and sane APIs.
00:08:49.440 I appreciate you all for being here and hope to chat more during our break.
00:08:55.320 Thanks!