< Return to Video

RailsConf 2014 - Build The API First by Rosie Hoyem and Sonja Hall

  • 0:17 - 0:20
    ROSIE HOYEM: All right. OK. I think we are
    ready
  • 0:20 - 0:22
    to get going. Thanks so much to everyone
  • 0:22 - 0:25
    for being here. This is both of our first
  • 0:25 - 0:27
    times at RailsConf and we're super excited
    to be
  • 0:27 - 0:30
    here as well. It's been a fantastic week.
  • 0:30 - 0:32
    So fantastic that I'm losing my voice and
  • 0:32 - 0:34
    I've gotten a little cold, so please forgive
    if
  • 0:34 - 0:37
    I have a crackly voice. I'm all hopped up
    on
  • 0:37 - 0:39
    cold meds, so this should be an entertaining
  • 0:39 - 0:41
    presentation, anyway.
  • 0:41 - 0:43
    So my name is Rosie. And I am joining
  • 0:43 - 0:46
    you here from Minneapolis, and this lovely
    lady here
  • 0:46 - 0:50
    is Sonja, all the way from Washington D.C.
    And
  • 0:50 - 0:52
    today, Sonja and I are going to talk about
  • 0:52 - 0:55
    APIs. But we're going to take a slightly different
  • 0:55 - 0:58
    approach to this talk. We both have been through
  • 0:58 - 1:01
    fairly major career pivots, in the past few
    years,
  • 1:01 - 1:02
    that have lead us to be in front of
  • 1:02 - 1:06
    you here today as Rails developers. So we
    wanted
  • 1:06 - 1:08
    to take all of our past experiences and use
  • 1:08 - 1:10
    this to kind of frame this conversation in
    a
  • 1:10 - 1:13
    way that we thought might be relevant and
    interesting.
  • 1:13 - 1:15
    So, my background before I made the transition
    to
  • 1:15 - 1:19
    Rails development was in architecture, as
    in bricks and
  • 1:19 - 1:24
    seal architecture. Not software architecture.
    And city planning. Sonja
  • 1:24 - 1:27
    also worked as a writer and graphic designer
    in
  • 1:27 - 1:30
    a former life. We both spent a lot of
  • 1:30 - 1:35
    time thinking about the building process and
    design elements.
  • 1:35 - 1:38
    So to start off this presentation, I'm gonna
    talk
  • 1:38 - 1:41
    a little bit about the importance of patterns
    in
  • 1:41 - 1:44
    both the build environment and software that
    support good
  • 1:44 - 1:47
    design. Then I'll get into the meat of the
  • 1:47 - 1:50
    talk and describe why we think you should
    consider
  • 1:50 - 1:54
    building the API first. And Sonja will go
    over
  • 1:54 - 1:57
    some best practices, and I'll bring it home
    with
  • 1:57 - 2:00
    a bit of conversation about some tools and
    best
  • 2:00 - 2:04
    practices that you can use to build your API.
  • 2:04 - 2:11
    So, as I mentioned, my background, before
    I got
  • 2:11 - 2:14
    the wild hair to become a Rails developer,
    was
  • 2:14 - 2:17
    in architecture and planning. Along the way,
    learning to
  • 2:17 - 2:20
    program, and learning Rails, has been a fascinating
    journey,
  • 2:20 - 2:24
    partially because everywhere I look, I find
    these parallels
  • 2:24 - 2:27
    between the build environment and the Rails
    software stack.
  • 2:27 - 2:30
    One of these parallels is the role that patterns
  • 2:30 - 2:33
    play in both of these worlds. Before we dive
  • 2:33 - 2:34
    into the meat of this talk, I want to
  • 2:34 - 2:37
    tell a quick story about how I came around
  • 2:37 - 2:40
    to this topic and why we thought it might
  • 2:40 - 2:42
    be relevant to approach this topic from the
    perspective
  • 2:42 - 2:45
    of design patterns.
  • 2:45 - 2:47
    One of the most influential books in the last
  • 2:47 - 2:50
    half a century in architecture is this book
    by
  • 2:50 - 2:54
    Christopher Alexander, A Pattern Language.
    If you're unfamiliar, a
  • 2:54 - 2:57
    pattern language, in a nutshell, is a method
    of
  • 2:57 - 3:02
    describing good design practices. A pattern
    language should facilitate
  • 3:02 - 3:07
    successfully solving very large, complex design
    problems. These patterns
  • 3:07 - 3:10
    are tools to help abstract away some of the
  • 3:10 - 3:13
    complexity in our world so our little brains
    can
  • 3:13 - 3:17
    wrap around how to approach solutions.
  • 3:17 - 3:19
    Design patterns in the build environment go
    back hundreds
  • 3:19 - 3:22
    and hundreds of years, and are very much so
  • 3:22 - 3:24
    rooted in how we, as humans, experience our
    surroundings
  • 3:24 - 3:27
    and relate to each other. And, for me, as
  • 3:27 - 3:30
    an architecture student, understanding patterns
    was a big part
  • 3:30 - 3:32
    of my training.
  • 3:32 - 3:35
    I'm sure you all guess where this is going.
  • 3:35 - 3:39
    So this book came out in 1977, and responds
  • 3:39 - 3:42
    to a few decades of really, really bad architecture
  • 3:42 - 3:46
    that just went in the face of good design
  • 3:46 - 3:49
    patterns that had developed over the ages.
    They ignored
  • 3:49 - 3:52
    these patterns that support a tried and true
    way
  • 3:52 - 3:55
    humans want to inhabit cities and essentially
    ended up
  • 3:55 - 3:57
    creating a lot of buildings that were really
    awful
  • 3:57 - 3:59
    spaces for humans.
  • 3:59 - 4:04
    And so now, we fast-forward twenty years,
    and these
  • 4:04 - 4:07
    ideas re-emerge in the software world, and,
    Martin Fowler,
  • 4:07 - 4:11
    for example, is writing about patterns. Not
    only is
  • 4:11 - 4:13
    he writing about patterns, all of the language
    that
  • 4:13 - 4:16
    he uses to talk about object-oriented software
    design, comes
  • 4:16 - 4:19
    straight out of the architecture world.
  • 4:19 - 4:22
    On this book's Wikipedia page, they point
    directly to
  • 4:22 - 4:25
    Christopher Alexander's book as an inspiration
    for Fowler's work
  • 4:25 - 4:29
    in software. Most people probably take it
    for granted
  • 4:29 - 4:31
    at this point, but all of the language that
  • 4:31 - 4:33
    we use to talk about software was lifted straight
  • 4:33 - 4:37
    out of the architecture world. And, as an
    extension,
  • 4:37 - 4:39
    I think the way the profession thinks about
    the
  • 4:39 - 4:43
    organization of things. We like to think that
    what
  • 4:43 - 4:46
    we're doing is new. But take a couple steps
  • 4:46 - 4:50
    back, and the concepts are really the same.
  • 4:50 - 4:53
    I don't know if Fowler and Alexander knew
    each
  • 4:53 - 4:54
    other. But I think they would have gotten
    along.
  • 4:54 - 4:55
    And one of the things I like to imagine
  • 4:55 - 4:59
    is them going out on Tuesdays to drink cappuccinos
  • 4:59 - 5:02
    and have these really intense philosophical
    conversations about the
  • 5:02 - 5:07
    order of things. Even though, at a glance,
    it
  • 5:07 - 5:08
    seems they come from very different worlds,
    at a
  • 5:08 - 5:10
    conceptual level, I think they would be having
    the
  • 5:10 - 5:13
    same conversation.
  • 5:13 - 5:16
    So, coming back to Rails, when I started to
  • 5:16 - 5:19
    think about software, I inevitably brought
    this lens of
  • 5:19 - 5:23
    patterns with me, which meant I was super
    primed
  • 5:23 - 5:27
    to drink the Rails Kool-Aid. Convention over
    configuration? Obviously
  • 5:27 - 5:31
    this was just really smart design. These conventions
    mapped
  • 5:31 - 5:34
    very cleanly in my brain to patterns, and
    I
  • 5:34 - 5:37
    just got it right away.
  • 5:37 - 5:39
    At first, the process of learning Rails was
    just
  • 5:39 - 5:43
    a smooth progression of these elegantly designed
    pieces sort
  • 5:43 - 5:48
    of clicking into place. Then, inevitably,
    I stumbled into
  • 5:48 - 5:51
    area after area where Rails does not provide
    conventions
  • 5:51 - 5:54
    or strong opinions on what to do. And I
  • 5:54 - 5:56
    would look to my right, look to my left,
  • 5:56 - 6:00
    talk to developers around me, and realized
    everybody had
  • 6:00 - 6:02
    a different opinion. And honestly this is
    when learning
  • 6:02 - 6:05
    Rails got kind of hard.
  • 6:05 - 6:08
    And so this brings us here, today. One of
  • 6:08 - 6:11
    these areas that I, that we've found without
    a
  • 6:11 - 6:13
    clear, sort of mainstream convention that
    everyone seems to
  • 6:13 - 6:17
    agree on was APIs. Beyond using JBuilder and
    the
  • 6:17 - 6:21
    basic happy path Rails application, there
    wasn't clear guidance
  • 6:21 - 6:24
    how to deal with APIs. And honestly this really
  • 6:24 - 6:27
    surprised me.
  • 6:27 - 6:30
    Seems to me, and maybe many of you, most
  • 6:30 - 6:32
    of the hard problems I deal with today are
  • 6:32 - 6:35
    related to data transfer. At my current job,
    this
  • 6:35 - 6:38
    is what we do. We build applications that
    manage
  • 6:38 - 6:40
    large amounts of data. This is a really important
  • 6:40 - 6:45
    piece of our applications. And so, thinking
    about this
  • 6:45 - 6:48
    more as developers, as a new developer I should
  • 6:48 - 6:50
    say. When we have a question, I, of course,
  • 6:50 - 6:53
    ask the internet. This sent me down a rabbit
  • 6:53 - 6:56
    hole where everyone was suggesting something
    slightly different, and
  • 6:56 - 6:59
    nobody seemed quite satisfied with how they
    were dealing
  • 6:59 - 7:00
    with this problem.
  • 7:00 - 7:02
    I read about lots of solutions, all having
    varying
  • 7:02 - 7:06
    degrees of success. It was all interesting.
    And somewhat
  • 7:06 - 7:08
    helpful. But it wasn't the solid answer I
    was
  • 7:08 - 7:13
    looking for. And honestly it was really frustrating.
    I
  • 7:13 - 7:15
    knew this wasn't rocket surgery, and it left
    me
  • 7:15 - 7:19
    feeling like, OK Rails, if you're so opinionated,
    why
  • 7:19 - 7:22
    don't you have an opinion on this? What should
  • 7:22 - 7:24
    I do?
  • 7:24 - 7:29
    And so, here we are. And we believe building
  • 7:29 - 7:32
    APIs is one of the critical areas where we
  • 7:32 - 7:36
    should have a shared solution because, obviously,
    this is
  • 7:36 - 7:39
    how our applications talk to each other. This
    is
  • 7:39 - 7:43
    where my application and your applications
    cease to be
  • 7:43 - 7:46
    my problem and your problems, and they're
    now everybody's
  • 7:46 - 7:50
    problem. Especially if we have a public API.
  • 7:50 - 7:55
    So, for one moment, bring this back to architecture.
  • 7:55 - 7:57
    The evolution of our great cities is really
    the
  • 7:57 - 8:02
    evolution of shared problems becoming shared
    solutions in elegant
  • 8:02 - 8:04
    and innovative ways. We tend to think of the
  • 8:04 - 8:07
    development of cities in terms of buildings,
    but buildings
  • 8:07 - 8:10
    come and go, in reality. The interesting story,
    and
  • 8:10 - 8:12
    here I take off the architecture hat and put
  • 8:12 - 8:14
    on the city planner hat, is, is really the
  • 8:14 - 8:18
    story of infrastructure. How we came together
    and agreed
  • 8:18 - 8:22
    how electricity should be delivered. How are
    sanitation systems
  • 8:22 - 8:24
    should be constructed. And how we wanted to
    get
  • 8:24 - 8:29
    from point A to point B.
  • 8:29 - 8:31
    So the starting point of this talk is that
  • 8:31 - 8:36
    APIs should act as the infrastructure for
    our applications.
  • 8:36 - 8:38
    If passing around data is the most important
    thing
  • 8:38 - 8:40
    your app does, then your APIs should be elevated
  • 8:40 - 8:43
    from something hobble together to something
    that we have
  • 8:43 - 8:47
    a solid solution for. And starting with the
    API
  • 8:47 - 8:51
    and building your client applications around
    it opens up
  • 8:51 - 8:54
    all sorts of interesting possibilities.
  • 8:54 - 8:56
    Carried out to the full realization of this
    concept,
  • 8:56 - 8:59
    your APIs could be the foundation that supports
    a
  • 8:59 - 9:04
    variety of clients, such as mobile applications
    or rich
  • 9:04 - 9:08
    JavaScript framework clients. By decoupling
    your REST API from
  • 9:08 - 9:12
    your client, you can more easily support multiple
    clients
  • 9:12 - 9:14
    and have a system that is much more flexible,
  • 9:14 - 9:16
    but also organized.
  • 9:16 - 9:22
    If a mobile interface is really important
    to your
  • 9:22 - 9:25
    application, your REST API can support multiple
    native mobile
  • 9:25 - 9:28
    apps with minimal duplication across platforms.
    This is a
  • 9:28 - 9:30
    good thing. You can also use the same API
  • 9:30 - 9:34
    to serve data to a JavaScript framework. Again,
    with
  • 9:34 - 9:37
    very little duplication.
  • 9:37 - 9:40
    Your API should be the common denominator.
    Without a
  • 9:40 - 9:43
    good standardize server solution, you can't
    have a standardized
  • 9:43 - 9:47
    way for different clients to talk to the servers.
  • 9:47 - 9:50
    All right. So we've discussed some reasons
    why, for
  • 9:50 - 9:53
    this new generation of Rails applications
    that must embrace
  • 9:53 - 9:56
    a variety of clients, the API should be the
  • 9:56 - 10:01
    foundation, and where we think you should
    consider starting.
  • 10:01 - 10:05
    So, one, your application will be designed
    to pass
  • 10:05 - 10:09
    around data from the get-go. And reason two,
    if
  • 10:09 - 10:11
    you need to support multiple clients, you
    can easily
  • 10:11 - 10:16
    do this with minimal duplication across platforms.
    And three,
  • 10:16 - 10:19
    if you create the standardized server code
    first, you'll
  • 10:19 - 10:22
    easily support the creation of standardized
    client code.
  • 10:22 - 10:27
    OK. Now I'm gonna pass it off to Sonja.
  • 10:27 - 10:32
    SONJA HALL: All right. So, let's discuss some
    of
  • 10:32 - 10:35
    the API best practices. There are a few that
  • 10:35 - 10:38
    are very important to begin with, when you're
    constructing
  • 10:38 - 10:40
    your API. But before we jump into them, I
  • 10:40 - 10:44
    think it's important to consider this question.
    Why is
  • 10:44 - 10:47
    a well-designed API so important?
  • 10:47 - 10:53
    Well, in this image we see falling water,
    going
  • 10:53 - 10:55
    back to architecture just for a moment. This
    is
  • 10:55 - 10:58
    a catilever home in Pennsylvania, built in
    1935, that
  • 10:58 - 11:00
    is among one of the greatest assets to the
  • 11:00 - 11:03
    architect and design thinker Frank Lloyd Wright.
  • 11:03 - 11:05
    It is built partly over a waterfall, deriving
    its
  • 11:05 - 11:09
    name from its situation. And will, and has
    been
  • 11:09 - 11:11
    conserved as a museum now since 1963. So,
    in
  • 11:11 - 11:15
    other words, this is a huge asset to the
  • 11:15 - 11:19
    legacy of Frank Lloyd Wright. A well-designed
    API can
  • 11:19 - 11:23
    be that of a company's greatest assets.
  • 11:23 - 11:27
    Why? Because users invest their time in obvious
    and
  • 11:27 - 11:32
    not-so-obvious ways. In the obvious ways,
    you're writing to
  • 11:32 - 11:35
    it, buying products that use it. You tell
    your
  • 11:35 - 11:37
    friends and communities about how great this
    API is.
  • 11:37 - 11:39
    You start writing blog posts about it. You're
    creating
  • 11:39 - 11:42
    hype, basically. The less obvious ways is
    that the
  • 11:42 - 11:45
    users are digging into it and spending a significant
  • 11:45 - 11:47
    amount of time learning how it works and playing
  • 11:47 - 11:50
    around in the code.
  • 11:50 - 11:53
    A poorly designed API can be a huge liability.
  • 11:53 - 11:55
    The same users who would have been promoting
    it
  • 11:55 - 11:57
    and using it as inspiration for new products
    are
  • 11:57 - 12:00
    now calling it, calling in for support and
    asking
  • 12:00 - 12:03
    you what to do about the problems that you've
  • 12:03 - 12:05
    created with the API, and taking away from
    your
  • 12:05 - 12:08
    productivity, or worse case scenario, they're
    just taking one
  • 12:08 - 12:11
    look at your API and then running away from
  • 12:11 - 12:11
    it.
  • 12:11 - 12:14
    So the first guideline to follow, of the best
  • 12:14 - 12:17
    practices, is to design your APIs for experience
    rather
  • 12:17 - 12:21
    than, and readability, not for data alone.
    In other
  • 12:21 - 12:24
    words, an API should reflect how something
    is used,
  • 12:24 - 12:27
    not just how it is stored. In fact, I
  • 12:27 - 12:29
    hate to say it, but nobody really cares what
  • 12:29 - 12:31
    your database looks like.
  • 12:31 - 12:33
    People want to know how it operates, how they
  • 12:33 - 12:35
    can use it. The experience should not fill
    your
  • 12:35 - 12:38
    fellow developer with misery or regret, it
    should be
  • 12:38 - 12:42
    methodical and consistent. Which leads me
    to the next
  • 12:42 - 12:45
    best practice of consistency.
  • 12:45 - 12:47
    By providing a single API that can serve multiple
  • 12:47 - 12:52
    clients, you get consistent client solutions
    across platforms. This
  • 12:52 - 12:55
    includes having single end points and clean
    routes throughout
  • 12:55 - 12:58
    your API. It's kind of like a telephone pole
  • 12:58 - 13:00
    with all the cables and wires running through
    and
  • 13:00 - 13:04
    around it. The pole is built with specific
    plugins
  • 13:04 - 13:08
    for specific wires and purposes, allowing
    electricity to flow
  • 13:08 - 13:10
    from the power plant to your computer screen,
    or
  • 13:10 - 13:13
    your light bulb or what have you. And similarly,
  • 13:13 - 13:16
    an API call retrieves a particular dataset
    from your
  • 13:16 - 13:20
    database, and expects a consistent return
    every time.
  • 13:20 - 13:25
    Without this, we get this. The alternative.
    Something hideous
  • 13:25 - 13:31
    and terrifying. Unfortunately, this slum's
    infrastructure does not support
  • 13:31 - 13:35
    consistency or sustained development of regulated
    wiring. It is
  • 13:35 - 13:38
    inefficiency at its finest, where everyone
    seems to have
  • 13:38 - 13:41
    adopted a 'to each their own' mentality.
  • 13:41 - 13:44
    You should never have your API making someone
    feel
  • 13:44 - 13:48
    like this. So avoid that. Coming from the
    writing
  • 13:48 - 13:52
    and editing world, documentation is so important.
    Especially having
  • 13:52 - 13:55
    learned how to code later in life. I love
  • 13:55 - 14:00
    a well-documented gem or, you know, anything
    I'm trying
  • 14:00 - 14:01
    to use, I love when I say a lot
  • 14:01 - 14:05
    of documentation, and written the write way.
  • 14:05 - 14:08
    So, documentation is very important to the
    wide-spread use
  • 14:08 - 14:12
    of your API. Public or not. It should act
  • 14:12 - 14:14
    as a bonus layer of information, though. Like
    a
  • 14:14 - 14:17
    glimpse into a developer's mind that assumes
    nothing and
  • 14:17 - 14:20
    notes everything. It should sit on top of
    an
  • 14:20 - 14:24
    already easy-to-understand API, and so, in
    other words, don't
  • 14:24 - 14:27
    make the documentation do the dirty work.
    Your API
  • 14:27 - 14:29
    should already be understandable, and you're
    just providing this
  • 14:29 - 14:33
    layer of documentation on top of it.
  • 14:33 - 14:35
    So if you're the type who just grumbled at
  • 14:35 - 14:38
    my last slide, I know you're out there, let's
  • 14:38 - 14:40
    chat about a service that makes documentation
    a sync.
  • 14:40 - 14:44
    One that we found was Apiary. It's a structured,
  • 14:44 - 14:49
    interactive tool that follows the necessities
    documentation, using mark
  • 14:49 - 14:51
    down and a template for easy usage. The result
  • 14:51 - 14:56
    is an intuitive document incorporating the
    basic necessities that
  • 14:56 - 14:58
    all documentation could contain. Should contain.
  • 14:58 - 15:02
    So here's an example of Apiary and what it
  • 15:02 - 15:04
    might look like when you're doing it. But,
    generally
  • 15:04 - 15:08
    speaking, Apiary or not, all documentation
    should include the
  • 15:08 - 15:09
    following:
  • 15:09 - 15:12
    Examples of the full request and expected
    response, a
  • 15:12 - 15:16
    list of error codes, their significance - like,
    what
  • 15:16 - 15:22
    is causing them. A searchable html interface,
    and communication
  • 15:22 - 15:25
    of versioning and deprecation schedules so
    you don't catch
  • 15:25 - 15:27
    anyone off guard who happens to be using an
  • 15:27 - 15:29
    API, a version of your API that you're going
  • 15:29 - 15:32
    to be moving forward from.
  • 15:32 - 15:35
    So, the life span of an API is important
  • 15:35 - 15:38
    to consider. It can be difficult to choose
    how
  • 15:38 - 15:41
    you're going to version, or what your strategy
    will
  • 15:41 - 15:43
    be, but you should approach it as if you
  • 15:43 - 15:46
    have to get the first one, the first version
  • 15:46 - 15:50
    of your API, absolutely right. It makes it
    easier
  • 15:50 - 15:52
    down the road.
  • 15:52 - 15:54
    The most important thing, though, to remember,
    is that
  • 15:54 - 15:59
    versioning is inevitable and plan for deprecation
    from version
  • 15:59 - 16:01
    one.
  • 16:01 - 16:05
    I also like to think of APIs as if
  • 16:05 - 16:09
    they were a rescue dog. If anyone knows me
  • 16:09 - 16:09
    from Flat Iron School - I see a few
  • 16:09 - 16:12
    of you out there - I'm obsessed with rescue
  • 16:12 - 16:14
    animals. So I had to tie this back in
  • 16:14 - 16:16
    for a moment. So, this is not a West
  • 16:16 - 16:20
    Minister Kennel Club winning dog, but he's
    more like
  • 16:20 - 16:23
    a little rescue pup. He has limitless potential
    to
  • 16:23 - 16:25
    be an incredible pet if you let him get
  • 16:25 - 16:28
    there. Just like you want to set your future
  • 16:28 - 16:30
    dogs up for success, you'll want to do the
  • 16:30 - 16:32
    same for your API.
  • 16:32 - 16:35
    For starters, neither this dog nor your API
    require
  • 16:35 - 16:39
    any fancy things. They simply crave repetition
    and unconditional
  • 16:39 - 16:42
    support in order to be successful. Give them
    what
  • 16:42 - 16:45
    they need to evolve into their new versions,
    but
  • 16:45 - 16:49
    don't ever forget where they came from.
  • 16:49 - 16:52
    The JSON APIs is one standard for building
    APIs
  • 16:52 - 16:55
    in JSON that is supported by the Rails community.
  • 16:55 - 16:59
    So, this is Steve Klabnik's quote. By following
    shared
  • 16:59 - 17:02
    conventions you can increase productivity,
    take advantage of generalized
  • 17:02 - 17:07
    tooling, and focus on what matters: your application.
    And
  • 17:07 - 17:10
    we liked the JSON API standards, so I put
  • 17:10 - 17:13
    an example of it in here.
  • 17:13 - 17:16
    And like so many of the tools that we've
  • 17:16 - 17:18
    discussed already today, there are more out
    there, and
  • 17:18 - 17:20
    there's a lot of great ones being developed
    right
  • 17:20 - 17:22
    now. So Rosie's gonna share a few more of
  • 17:22 - 17:26
    those with you.
  • 17:26 - 17:31
    R.H.: So, building APIs is a shared problem
    for
  • 17:31 - 17:34
    most developers. It seems there is now a general
  • 17:34 - 17:38
    shared understanding of what a good API should
    consist
  • 17:38 - 17:40
    of. A shared solution is the next step. So
  • 17:40 - 17:44
    we can turn these best practices into conventions.
  • 17:44 - 17:47
    For the last couple of months, we've been
    asking
  • 17:47 - 17:49
    every developer we encounter what they use
    to build
  • 17:49 - 17:54
    APIs. The answers were usually Rails or Sinatra,
    with
  • 17:54 - 17:58
    some combination of the tools available on
    Ruby Toolbox,
  • 17:58 - 18:02
    and usually a partially hand-ruled solution.
    We also heard,
  • 18:02 - 18:04
    more and more, how Rails is being used to
  • 18:04 - 18:07
    create a REST API that talks to a mobile
  • 18:07 - 18:09
    client, and there seemed to be growing interest
    in
  • 18:09 - 18:12
    using Rails for JavaSrcipt frameworks.
  • 18:12 - 18:15
    These may not be traditional Rails applications,
    but people
  • 18:15 - 18:17
    still recognize the value of Rails for creating
    a
  • 18:17 - 18:20
    REST API.
  • 18:20 - 18:24
    After talking to lots of people, it was evident,
  • 18:24 - 18:28
    though, that an agreed upon solution still
    wasn't clear,
  • 18:28 - 18:29
    and the whole thing just required way too
    much
  • 18:29 - 18:34
    thinking. There's so many ways to hobble something
    together,
  • 18:34 - 18:36
    but we wanted to know the Rails way. What
  • 18:36 - 18:38
    will be the Rails way to build a solid
  • 18:38 - 18:38
    API?
  • 18:38 - 18:41
    Again, damn it Rails, if you're so opinionated
    and
  • 18:41 - 18:44
    committed to this idea of conventions, tell
    me what
  • 18:44 - 18:46
    to do. We no longer have to be pioneers
  • 18:46 - 18:49
    venturing out into the unknown.
  • 18:49 - 18:52
    So, in the middle of all of these conversations,
  • 18:52 - 18:55
    we did see a Rails way emerging, though it's
  • 18:55 - 18:57
    not baked in yet. Lo and behold, there are
  • 18:57 - 19:00
    solutions emerging from the core Rails team
    that provide
  • 19:00 - 19:03
    an opinion on how to create REST APIs. We've
  • 19:03 - 19:08
    been exploring Rails API in combination with
    ActiveModel serializer
  • 19:08 - 19:12
    for building an API where you only need the
  • 19:12 - 19:13
    JSON end points.
  • 19:13 - 19:16
    Rails API is Rails. It utilizes most of the
  • 19:16 - 19:19
    same generators and conventions and will look
    very, very
  • 19:19 - 19:24
    familiar. For this new generation of applications,
    where the
  • 19:24 - 19:27
    API is your infrastructure that supports a
    family of
  • 19:27 - 19:30
    client applications, we think this is what
    the future
  • 19:30 - 19:31
    might look like.
  • 19:31 - 19:35
    So Rails API is really just a miniature Rails
  • 19:35 - 19:37
    stack, where they skipped all of the stuff
    that
  • 19:37 - 19:40
    you need if you were creating views, producing
    a
  • 19:40 - 19:42
    solution that is lighter and faster than the
    full
  • 19:42 - 19:44
    Rails app. But it does still come with a
  • 19:44 - 19:48
    full suite of baked in solutions for many
    poorly
  • 19:48 - 19:51
    understood problems.
  • 19:51 - 19:53
    And the documentation, there's a laundry list
    of things
  • 19:53 - 19:55
    that are handled in the middleware layer and
    the
  • 19:55 - 19:59
    ActionPack layer. This is definitely a win.
    For example,
  • 19:59 - 20:02
    you might understand perfectly how to handroll
    a solution
  • 20:02 - 20:06
    for IP spoofing attacks, or maybe not. But
    even
  • 20:06 - 20:08
    if you do, why spend time doing this if
  • 20:08 - 20:12
    Rails is gonna do it for you? Thanks Rails.
  • 20:12 - 20:14
    And again, if there's a solution for something
    already
  • 20:14 - 20:20
    out there, I'd rather use that than reinvent
    the
  • 20:20 - 20:20
    wheel.
  • 20:20 - 20:23
    The other tool we're exploring is ActiveModel
    serializers, and
  • 20:23 - 20:28
    sure, serializers replace hash-driven development
    with object-oriented development. For
  • 20:28 - 20:32
    example, with AcitveModel serializers, when
    you're using render JSON
  • 20:32 - 20:35
    in your controllers, Rails will search for
    a serializer
  • 20:35 - 20:38
    before, for the object and use it if available.
  • 20:38 - 20:42
    It's an elegant solution to creating server-side
    APIs that
  • 20:42 - 20:46
    doesn't require a view layer.
  • 20:46 - 20:49
    They act a lot, serializers act a lot like
  • 20:49 - 20:53
    models and can be easily customized. ActiveModel
    serializers also
  • 20:53 - 20:57
    follows the JSON API standard, so you know
    you're
  • 20:57 - 21:00
    creating a standardized API that will easily
    talk to
  • 21:00 - 21:03
    a variety of clients.
  • 21:03 - 21:05
    It really helps to take the guessing out of
  • 21:05 - 21:08
    how your JSON API should be structured.
  • 21:08 - 21:13
    OK. So, it's time to start wrapping things
    up.
  • 21:13 - 21:15
    A few points to leave you, leave everyone
    with.
  • 21:15 - 21:19
    We find it's increasingly important to design
    APIs following
  • 21:19 - 21:23
    patterns and conventions that fit into the
    system as
  • 21:23 - 21:25
    a whole. To do this, we can leverage existing
  • 21:25 - 21:29
    best practices and tools, and by doing so
    we'll
  • 21:29 - 21:32
    create APIs that support a more organized
    structure for
  • 21:32 - 21:34
    our applications.
  • 21:34 - 21:38
    I was inspired by Yehuda Katz's keynote yesterday,
    and
  • 21:38 - 21:40
    I think it's time for us as a community
  • 21:40 - 21:43
    to move API design out of the area of
  • 21:43 - 21:48
    experimentation and into the area of collective
    shared solution.
  • 21:48 - 21:51
    We'll all benefit.
  • 21:51 - 21:55
    And again, trivial choices are the enemy.
    Drink your
  • 21:55 - 21:59
    convention over configuration Kool-Aid. But
    brush your teeth afterwards.
  • 21:59 - 22:04
    Kool-Aid's not good for you. Anyway. We're
    often trying
  • 22:04 - 22:06
    to solve the same problem. And when this is
  • 22:06 - 22:09
    the case, we should agree on a strong convention
  • 22:09 - 22:11
    and all follow it.
  • 22:11 - 22:14
    We'll all benefit in the end. And third, don't
  • 22:14 - 22:16
    forget about who or what else is going to
  • 22:16 - 22:19
    want to use this API in the future, and
  • 22:19 - 22:23
    plan accordingly. This may include native
    mobile applications or
  • 22:23 - 22:27
    a JavaScript client-side framework or something
    else that we
  • 22:27 - 22:30
    haven't even imagined yet. By designing our
    APIs today
  • 22:30 - 22:33
    to be consistent across platforms, we can
    make changes
  • 22:33 - 22:38
    to our client-side applications in the future
    much easier.
  • 22:38 - 22:41
    So tomorrow, someone's not trying to use your
    hand-ruled
  • 22:41 - 22:44
    API that didn't really follow any established
    design patterns
  • 22:44 - 22:47
    or conventions, and you have to explain that,
    well,
  • 22:47 - 22:49
    it seemed like a good idea at the time.
  • 22:49 - 22:52
    For those of you who aren't from Minneapolis,
    this
  • 22:52 - 22:55
    is the ugliest building in the Minneapolis
    skyline. So.
  • 22:55 - 22:59
    A little context for you. We all drive by
  • 22:59 - 23:02
    and shake our heads. Like oh, why?
  • 23:02 - 23:06
    Or worse. Your application gets scrapped because
    it was
  • 23:06 - 23:10
    poorly organized from the get-go and too rigid
    to
  • 23:10 - 23:11
    update.
  • 23:11 - 23:16
    And last, but not least, document all the
    things.
  • 23:16 - 23:19
    Documentation is the cool to API being used
    successfully.
  • 23:19 - 23:24
    All right. And we've come to the end. I
  • 23:24 - 23:27
    hope, just want to leave you with one thought.
  • 23:27 - 23:29
    I hope when you approach your next project,
    you'll
  • 23:29 - 23:30
    consider building your API first.
  • 23:30 - 23:33
    S.H.: All right, thanks everybody.
Title:
RailsConf 2014 - Build The API First by Rosie Hoyem and Sonja Hall
Description:

more » « less
Duration:
23:59

English subtitles

Revisions