Latest / CyberCode Academy / Course 37 - Building Web Apps with Ruby On Rails | Episode 15: Multi-format Controllers and Custom JSON Serialization
Transcript
- 0:00Speaker 1: Imagine opening a really simple weather app on your phone.
- 0:04Speaker 1: You know, you're just trying to figure out if you
- 0:06Speaker 1: need an umbrella today.
- 0:07Speaker 2: Right, You just want to see a little cloud icon.
- 0:09Speaker 1: Exactly, just a cloud, maybe the temperature, But instead your
- 0:12Speaker 1: phone just completely freezes up. I mean, the app becomes
- 0:15Speaker 1: totally in response of your battery sort of draining insanely fast,
- 0:18Speaker 1: and you're just standing there staring at a blank screen.
- 0:20Speaker 2: It's the worst, and it happens all the time.
- 0:24Speaker 1: Yeah, and why did that happen? Well, because, under the hood,
- 0:27Speaker 1: the application didn't just send you the day's forecast. It
- 0:30Speaker 1: actually downloaded the entire one hundred year meteorological history of
- 0:34Speaker 1: your hemisphere, just to extract that single umbrella icon.
- 0:38Speaker 2: Which is just a phenomenal waste of resources.
- 0:40Speaker 1: It really is. And that is the frustrating, incredibly common
- 0:45Speaker 1: reality of a poorly designed API.
- 0:47Speaker 2: Yeah, it happens because developers, you know, they often treat
- 0:50Speaker 2: their database like a blunt instrument. You ask for a user,
- 0:54Speaker 2: so the system grabs the entire user record, every preference,
- 0:57Speaker 2: every log in timestamp, every internal ID, and just throws
- 1:00Speaker 2: that whole massive chunk of data over the wall to
- 1:02Speaker 2: the client device, which.
- 1:04Speaker 1: Brings us to today. So welcome to the deep Dive,
- 1:07Speaker 1: fellow developers, tech students, and curious learners. Glad to be
- 1:10Speaker 1: here today. Our mission is to look under the hood
- 1:13Speaker 1: of building smart hyper performance APIs. We're going to step
- 1:17Speaker 1: through exactly how an application serves up entirely different data
- 1:20Speaker 1: formats from the exact same endpoint, and how.
- 1:24Speaker 2: We can elegantly transform raw database models into really clean,
- 1:29Speaker 2: beautiful data.
- 1:31Speaker 1: Right and to make this tangible for you listening, we're
- 1:33Speaker 1: going to use a mental sandbox today. So picture a
- 1:36Speaker 1: project tracker application. We have projects, we have start dates,
- 1:39Speaker 1: and we have nested milestones.
- 1:41Speaker 2: It's honestly the ideal architecture to study. It has just
- 1:44Speaker 2: enough relational complexity that well, if we don't manage the
- 1:47Speaker 2: data flow carefully, the whole system will just bottleneck.
- 1:50Speaker 1: Okay, let's unpack this. If we have a single endpoint
- 1:53Speaker 1: in our project tracker, let's call it the project list,
- 1:56Speaker 1: how does our application actually know what kind of data
- 1:58Speaker 1: the client wants in the first place. That's a great
- 2:00Speaker 1: question because I mean, one user might be accessing the
- 2:04Speaker 1: platform via a standard web browser, right they expect a
- 2:06Speaker 1: full visual web page, but a mobile app hitting the
- 2:09Speaker 1: exact same URL just wants the raw data. So how
- 2:13Speaker 1: does the controller sort that out?
- 2:15Speaker 2: Well, that brings us to the underlying philosophy of an
- 2:18Speaker 2: API controller. A robust controller doesn't just do one thing.
- 2:22Speaker 2: It's inherently flexible.
- 2:24Speaker 1: Okay.
- 2:24Speaker 2: It has the capacity to serve multiple response formats like
- 2:28Speaker 2: JSON for that mobile app, XML for older legacy systems,
- 2:32Speaker 2: or standard HTML for a browser.
- 2:34Speaker 1: Oh I see, it reminds me of a multi lingual
- 2:36Speaker 1: restaurant host.
- 2:37Speaker 2: Yeah exactly.
- 2:38Speaker 1: The walk through the front door and they hand you
- 2:40Speaker 1: a menu translated perfectly into your preferred language. But wait,
- 2:43Speaker 1: those can't just magically guess your language. The client making
- 2:47Speaker 1: the request has to actually ask for that specific format first. Yeah,
- 2:51Speaker 1: they do, so how are they communicating that preference?
- 2:53Speaker 2: So the communication happens at the network layer. The primary
- 2:56Speaker 2: method is under the hood, utilizing what's called the HTTP
- 3:00Speaker 2: the accept header.
- 3:00Speaker 1: The accept header.
- 3:01Speaker 2: Yeah, when the client's browser or their mobile application initiates
- 3:06Speaker 2: the request to your server, it includes this hidden note.
- 3:08Speaker 2: It passes along a specific mimy type in that header.
- 3:12Speaker 1: Okay, So the request literally states text slash HTML or
- 3:17Speaker 1: application slash Jason spot on.
- 3:19Speaker 2: So the mobile app is basically slipping the server a
- 3:22Speaker 2: note that says, hey, I only speak Jason, please format accordingly.
- 3:26Speaker 1: That makes so much sense. Are there other ways to
- 3:27Speaker 1: do it? Yeah?
- 3:28Speaker 2: There is a secondary, much more visible way to achieve
- 3:31Speaker 2: the exact same result. The client can simply append the
- 3:35Speaker 2: desired file extension to the very end of the UURL
- 3:37Speaker 2: path itself.
- 3:38Speaker 1: Oh, like putting dot Jason at the.
- 3:39Speaker 2: End exactly if they hit slash projects dot json. The
- 3:43Speaker 2: intent is undeniable, and naturally, if the client sends a
- 3:47Speaker 2: request with no preference whatsoever, the application relies on a
- 3:51Speaker 2: configuration setting to just fall back to a default format.
- 3:54Speaker 1: Okay, so let's trace the path of that request into
- 3:57Speaker 1: the code. We're visualizing a framework like Ruby on Rails
- 3:59Speaker 1: here as our mental model, though these concepts really apply
- 4:03Speaker 1: across all modern development. Inside that controller picture a logic block,
- 4:08Speaker 1: it's the respond underscore.
- 4:09Speaker 2: To method, right, the respond to method. Think of respond
- 4:13Speaker 2: to as a highly sophisticated switchboard.
- 4:15Speaker 1: A switchboard.
- 4:16Speaker 2: Yeah, it intercepts the requested format from that HTTP header
- 4:19Speaker 2: we just talked about, and it routes the applications execution
- 4:22Speaker 2: down entirely different paths.
- 4:24Speaker 1: So if the accept header dictates HTML, the switchboard routes
- 4:28Speaker 1: the data to a standard view file.
- 4:30Speaker 2: Exactly, it triggers the rendering of the full visual web page.
- 4:35Speaker 2: It stitches together the layout, the CSS, the dom elements,
- 4:38Speaker 2: and sends back a complete visual experience.
- 4:41Speaker 1: But if the header indicates applications slash JSON.
- 4:44Speaker 2: Then it ignores the visual u files entirely. The switchboard
- 4:47Speaker 2: routes execution to a block that totally bypasses the HTML
- 4:50Speaker 2: rendering engine. It just sends back a raw data payload
- 4:54Speaker 2: representing those projects.
- 4:55Speaker 1: Okay, so sending standard JSON back is a solid foundation.
- 4:58Speaker 1: But in our sandbox, a project isn't just a flat
- 5:02Speaker 1: record with a title.
- 5:03Speaker 2: Right, It's more complex.
- 5:04Speaker 1: Yeah, it contains milestones, and these are subtasks that live
- 5:07Speaker 1: in a completely different database table, tied together by foreign keys.
- 5:11Speaker 1: So if the mobile app needs those milestones included, how
- 5:14Speaker 1: are we converting these complex, interconnected database models into neat
- 5:18Speaker 1: text strings without completely cluttering up our controller code.
- 5:21Speaker 2: Well, what's fascinating here is that the framework relies on
- 5:24Speaker 2: a distinct process called serialization to prevent that exact clutter. Serialization, right,
- 5:30Speaker 2: and serialization is deliberately engineered not as a single action,
- 5:33Speaker 2: but as a multi step process. Think of it as
- 5:36Speaker 2: a two step dance.
- 5:37Speaker 1: Wait, hold on. If I have a database object in
- 5:39Speaker 1: the system and the ultimate end goal is to send
- 5:42Speaker 1: a Jason Tech string over the network. Yeah, well, pausing
- 5:46Speaker 1: in the middle of that process, ye, just sounds like
- 5:48Speaker 1: adding an unnecessary middleman. Why not just you know, blast
- 5:53Speaker 1: the database objects straight into a text string in one
- 5:55Speaker 1: swift motion.
- 5:56Speaker 2: I get why you'd think that, but it comes down
- 5:58Speaker 2: to flexibility and archetl extual control. If you aggressively blast
- 6:02Speaker 2: an object straight into a string, you lose the ability
- 6:05Speaker 2: to easily manipulate the structure. Imagine trying to inject a
- 6:09Speaker 2: nested array of milestones into a raw text string.
- 6:13Speaker 1: Oh man, that sounds awful.
- 6:15Speaker 2: It is. You'd have to use messy string concatenation or
- 6:18Speaker 2: like complex regular expressions to find the exact right bracket
- 6:22Speaker 2: and insert the new text. It's incredibly fragile.
- 6:24Speaker 1: Ah. I see. So by pausing the process, you create
- 6:27Speaker 1: an intermediate state. You aren't dealing with text yet. You
- 6:31Speaker 1: are mapping the database attributes into a standard dictionary like
- 6:35Speaker 1: a hash map of keys and values exactly.
- 6:37Speaker 2: That's step one data preparation. This is the stage where
- 6:40Speaker 2: all the actual business logic lives. Okay, the framework takes
- 6:44Speaker 2: your complex object and maps it into a simple hash map.
- 6:47Speaker 2: Because it's still a structured data object, you can easily
- 6:49Speaker 2: add keys, remove keys, or merge in those nested milestones
- 6:53Speaker 2: without breaking any formatting.
- 6:54Speaker 1: Which means if step one handles the dictionary preparation, there
- 6:58Speaker 1: must be a completely separate execute U engine that handles
- 7:01Speaker 1: step two.
- 7:01Speaker 2: You got it, that's data transformation. Once the hash map
- 7:05Speaker 2: is perfectly prepared, the framework takes that pristine dictionary and
- 7:09Speaker 2: runs the mechanical process of converting it into the final
- 7:13Speaker 2: gs onstring.
- 7:14Speaker 1: So there's no business logic there, absolutely none.
- 7:17Speaker 2: There is no business logic at this stage. It's just
- 7:19Speaker 2: a pure algorithmic translation from a hash map to network
- 7:23Speaker 2: ready text.
- 7:23Speaker 1: This makes complete simps. We separate the concerns. The controller
- 7:28Speaker 1: isn't bogged down trying to format quotation marks and brackets exactly.
- 7:32Speaker 1: The model prepares its own dictionary and the framework translates it.
- 7:36Speaker 1: This protects the API's.
- 7:37Speaker 2: Performance, which brings us to perhaps the most critical aspect
- 7:41Speaker 2: of modern architecture, deciding what actually goes into that dictionary.
- 7:45Speaker 1: Oh yes, we need to talk about API dieting because
- 7:49Speaker 1: this ties directly back to that freezing weather app we
- 7:51Speaker 1: talked about earlier.
- 7:52Speaker 2: Yes, the golden rule of API design is simple, Never
- 7:56Speaker 2: send everything. Never. If your user model has fifty columns
- 8:00Speaker 2: the database including internal metrics, password hashes, and timestamps, but
- 8:05Speaker 2: the mobile app only needs the user's first name and
- 8:07Speaker 2: avatar URL to render a comments section, Sending all fifty
- 8:11Speaker 2: fields is architectural negligence.
- 8:13Speaker 1: It's like shipping a customer an entire cow. Because they
- 8:16Speaker 1: order the stake, that.
- 8:17Speaker 2: Is a perfect way to look at it.
- 8:18Speaker 1: The customer now has to figure out what to do
- 8:20Speaker 1: with all the excess, and the shipping costs are astronomical.
- 8:23Speaker 2: Exactly Bloated APIs waste resources on multiple fronts. Your server
- 8:28Speaker 2: burns CPU cycles fetching unnecessary data from the database, you
- 8:33Speaker 2: consume massive amounts of network bandwidth transmitting it, and most importantly,
- 8:37Speaker 2: the client device someone's actual smartphone has to utilize its
- 8:41Speaker 2: own processor and battery life to parse through thousands of
- 8:44Speaker 2: lines of irrelevant JSON just to find the one name
- 8:47Speaker 2: it needs.
- 8:48Speaker 1: YEA explains the battery drain, So we put the API
- 8:51Speaker 1: on a strict diet. What else goes into this preparation phase.
- 8:54Speaker 2: Well, you ensure your keys are universally readable. The front
- 8:58Speaker 2: end developer consuming your data shouldn't have to guess what
- 9:02Speaker 2: an attribute named flag underscore status underscore four represents.
- 9:06Speaker 1: Oh, that's the worst, right.
- 9:08Speaker 2: The key should explicitly define its purpose. Furthermore, you only
- 9:13Speaker 2: include relationships that are immediately necessary.
- 9:16Speaker 1: Right. If a project has ten thousand historical log entries,
- 9:19Speaker 1: we certainly don't want to bundle all of those into
- 9:21Speaker 1: the initial project.
- 9:22Speaker 2: Fetch exactly, You paginate them. Instead, you send the core
- 9:26Speaker 2: project data maybe the five most recent logs, and provide
- 9:30Speaker 2: a separate URL endpoint for the client to fetch more
- 9:33Speaker 2: logs if the user actually clicks view history.
- 9:35Speaker 1: Okay, that makes sense.
- 9:36Speaker 2: Additionally, you truncate exceptionally long tech strings. If a project
- 9:41Speaker 2: description is twelve pages long, you send a one hundred
- 9:44Speaker 2: character preview. It makes the initial load time feel instantaneous.
- 9:48Speaker 1: Okay. So let's bring this back to the code mechanics
- 9:50Speaker 1: for you listening, how do we actually trigger these two steps?
- 9:53Speaker 1: Becauseince we look at framework documentation, there are specific methods
- 9:57Speaker 1: attached to these active record models.
- 9:59Speaker 2: Right. For the data preparation step creating that flexible hash map,
- 10:04Speaker 2: the framework uses a method called as underscore Jason.
- 10:07Speaker 1: As underscore Jason, yeah, out.
- 10:09Speaker 2: Of the box. As underscore Jason simply dumps every single
- 10:13Speaker 2: attribute of the model into a dictionary, but it is
- 10:16Speaker 2: explicitly designed as a hook for you to override and customize, and.
- 10:20Speaker 1: Once as underscore json builds the dictionary. The method that
- 10:23Speaker 1: handles the actual string transformation is called the underscore Jason
- 10:27Speaker 1: right correct.
- 10:28Speaker 2: When you execute to underscore Jason, it reaches out calls
- 10:32Speaker 2: as underscore Jason to get the dictionary, and then converts
- 10:35Speaker 2: the whole thing into texts.
- 10:36Speaker 1: As the sequence. But this sequence introduces a very dangerous pitfall.
- 10:41Speaker 2: It really does.
- 10:42Speaker 1: I can see a catastrophic trap here based on how
- 10:44Speaker 1: those methods interact. If to underscore json transforms a dictionary
- 10:49Speaker 1: into a string, what happens if a developer accidentally calls
- 10:52Speaker 1: to underscore Jathon on a nested milestone and then the
- 10:55Speaker 1: parent project calls to underscore Jason on the whole package.
- 10:59Speaker 1: You're running the string cation process on something that is
- 11:01Speaker 1: already a string.
- 11:02Speaker 2: Exactly. If we connect this to the bigger picture, you've
- 11:04Speaker 2: just described a double encoded payload.
- 11:07Speaker 1: Double encoded.
- 11:07Speaker 2: Yes, the system takes your already formatted Jason string sees
- 11:12Speaker 2: all the quotation marks and assumes there are literal characters
- 11:15Speaker 2: that need to be escaped.
- 11:16Speaker 1: Oh no, so it adds backslashes everywhere, everywhere, like title
- 11:20Speaker 1: colon project suddenly turns into backslash quote title backslash quote.
- 11:25Speaker 1: It becomes a massive, unreadable wall of escaped characters.
- 11:28Speaker 2: It's awful And when the mobile app tries to parse it,
- 11:32Speaker 2: the Jason parser throws a fatal error and the application
- 11:35Speaker 2: just crashes.
- 11:36Speaker 1: That sounds like a nightmare to debug.
- 11:38Speaker 2: It's a notoriously difficult bug to track down for beginners.
- 11:42Speaker 2: So to avoid this entirely, you want to keep the
- 11:44Speaker 2: controller out of the serialization business entirely. Okay, you don't
- 11:48Speaker 2: manually call to underscore Jason. Instead, controllers typically rely on
- 11:52Speaker 2: a method called render. You simply tell the controller render
- 11:55Speaker 2: Jason Colon.
- 11:56Speaker 1: Project, and render acts as a secure delegator.
- 11:59Speaker 2: It does when you call render, it invokes to underscore
- 12:02Speaker 2: Jason exactly once, at the very top level of the payload. Okay,
- 12:06Speaker 2: just once, right, That single call securely trickles down, asking
- 12:11Speaker 2: every nested object the project, the milestones, the authors to
- 12:15Speaker 2: simply provide their expandal as underscore Jason dictionary. It compiles
- 12:20Speaker 2: one giant pristine Master dictionary and then converts the entire
- 12:25Speaker 2: tree into a string simultaneously. Wow, no double encoding is
- 12:29Speaker 2: mathematically possible.
- 12:30Speaker 1: So what does this all mean for us? It means
- 12:33Speaker 1: our controller code is beautifully minimal. It's essentially just one
- 12:36Speaker 1: line render this object. Yep. But if the controller's totally
- 12:39Speaker 1: hands off, the responsibility shifts. How do we actually get
- 12:43Speaker 1: inside that model and sculpt that as underscore Jason dictionary
- 12:47Speaker 1: to implement the API diet and best practices we just discussed.
- 12:50Speaker 2: Let's do a virtual walkthrough. Imagine refactoring the as underscore
- 12:54Speaker 2: Jason method in our project tracker. We open up the project.
- 12:57Speaker 1: Model file, okay, picturing it.
- 12:58Speaker 2: We are defining a custom underscore Jason method inside it.
- 13:02Speaker 2: We are going to call super super calling super triggers.
- 13:04Speaker 2: The framework's a default behavior of grabbing everything. But we
- 13:07Speaker 2: are going to pass a very strict set of instructions
- 13:10Speaker 2: and options hashed into that super call to manipulate the output.
- 13:13Speaker 1: Okay, let's start by filtering the junk out. We have
- 13:15Speaker 1: massive amounts of internal database tracking that the client doesn't
- 13:19Speaker 1: care about at all.
- 13:20Speaker 2: We have two primary tools for filtering only and accept
- 13:23Speaker 2: only and accept right. You can pass the only option
- 13:27Speaker 2: an array of the exact attributes you want to keep,
- 13:30Speaker 2: discarding everything else, or you can use except to single
- 13:35Speaker 2: out specific fields for removal. That makes sense, for instance,
- 13:39Speaker 2: when we eventually pull in our milestones, we absolutely want
- 13:42Speaker 2: to use except to strip out the project underscore ID
- 13:46Speaker 2: foreign key if the milestone is already nested securely inside
- 13:49Speaker 2: the project payload. Transmitting the parent IDA again is just
- 13:52Speaker 2: redundant data.
- 13:53Speaker 1: Everybody counts. Okay, so we've filtered the core project. But
- 13:57Speaker 1: as we just noted, a project is practically useless without
- 14:00Speaker 1: its tasks. How do we pull those relationships in without
- 14:03Speaker 1: breaking the structure we just built?
- 14:05Speaker 2: We passed the include option yes. By telling the super
- 14:09Speaker 2: method to include the milestones, the framework automatically queries the
- 14:13Speaker 2: related database table, runs the preparation step on each individual milestone,
- 14:18Speaker 2: and nests them as an array directly under a milestone's
- 14:21Speaker 2: key in our project dictionary.
- 14:24Speaker 1: That handles the structure perfectly. But what about the clarity
- 14:28Speaker 1: of the data.
- 14:29Speaker 2: What do you mean?
- 14:30Speaker 1: Well, sometimes legacy database columns have terrible naming conventions. Let's
- 14:34Speaker 1: say ten years ago, someone named the core descriptive field
- 14:37Speaker 1: text Underscore Desk Underscore v two. If we send that
- 14:41Speaker 1: to a modern mobile developer, they are going to be
- 14:44Speaker 1: incredibly confused.
- 14:45Speaker 2: Oh absolutely, that requires renaming attributes. And because we are
- 14:48Speaker 2: intercepting the process at the hash map stage step one,
- 14:51Speaker 2: we have total freedom.
- 14:52Speaker 1: Right, It's just a dictionary.
- 14:53Speaker 2: Exactly inside our custom as Underscore Jason method. We simply
- 14:56Speaker 2: write a line of code that reads the value of
- 14:58Speaker 2: text Underscore Desk Underscore v two, assigns that value to
- 15:02Speaker 2: a brand new, highly readable key called project Underscore description,
- 15:06Speaker 2: and then we just delete the old confusing key from
- 15:08Speaker 2: the dictionary entirely.
- 15:10Speaker 1: That is so clean the client gets a crystal clear,
- 15:13Speaker 1: self documenting API response. Now here's where it gets really
- 15:17Speaker 1: interesting to me. What if we want to send data
- 15:19Speaker 1: that doesn't actually exist in the database at all?
- 15:22Speaker 2: Ah? Yes, derived data types. Yes, derived data types are
- 15:25Speaker 2: the hallmark of a truly intelligent API. These are custom
- 15:30Speaker 2: fields generated dynamically at runtime during the serialization process.
- 15:35Speaker 1: Let's look at our sandbox. Every project has a start
- 15:38Speaker 1: date and an end date in the database. These are
- 15:40Speaker 1: stored as standard daytime strings. Right, But anyone who has
- 15:44Speaker 1: built a mobile app knows that Parsing string based dates
- 15:47Speaker 1: and calculating time zone offsets on a mobile CPU is
- 15:50Speaker 1: an absolute nightmare.
- 15:52Speaker 2: It is notoriously error prone, so we solve it on
- 15:54Speaker 2: the server inside are as underscore Jason preparation. We intercept
- 15:58Speaker 2: those date time strings. We run a quick conversion script
- 16:01Speaker 2: to translate them into Unix timestamp ah, which.
- 16:03Speaker 1: Are just clean universal integers representing seconds since nineteen seventy exactly.
- 16:08Speaker 2: We attach those integers to the dictionary instead of the strings.
- 16:11Speaker 1: I love this because we could take it a step
- 16:12Speaker 1: further with business logic. Let's see, the mobile app needs
- 16:16Speaker 1: to display a special badge if a project starts and
- 16:19Speaker 1: ends on the exact same day. Okay. Instead of forcing
- 16:22Speaker 1: the mobile app to write logic to compare those two dates,
- 16:24Speaker 1: we create a brand new field on the server called
- 16:27Speaker 1: is underscore single Underscore day Underscore project. During serialization, our
- 16:34Speaker 1: model simply checks if the start date matches the end date.
- 16:37Speaker 1: If they match, it injects a single day project true
- 16:40Speaker 1: into the Jason payload.
- 16:42Speaker 2: The broader implication here is immense. By running that simple
- 16:45Speaker 2: boolean calculation once on your powerful server, you are sitting
- 16:49Speaker 2: every single client device in the world from having to
- 16:52Speaker 2: write that logic, process those dates, and compute the result locally. Wow,
- 16:56Speaker 2: you are actively offloading compute costs from your user's battery
- 17:00Speaker 2: to your server.
- 17:00Speaker 1: Picture the final result. The user taps their screen, the
- 17:03Speaker 1: request flies to the controller. The controller simply routes the
- 17:06Speaker 1: format and says render Jason. But because of the work
- 17:09Speaker 1: we did inside the model, the response that comes back
- 17:12Speaker 1: is breathtakingly efficient.
- 17:13Speaker 2: It really is.
- 17:14Speaker 1: The bloated database fields are gone, the nested milestones are
- 17:18Speaker 1: perfectly structured, the keys are instantly readable, the dates are
- 17:22Speaker 1: universally parsible. Integers, and custom business logic is already computed
- 17:26Speaker 1: and ready to be displayed. It is secure, lightweight, and
- 17:30Speaker 1: incredibly fast.
- 17:31Speaker 2: And crucially, all of that complex preparation logic is neatly
- 17:35Speaker 2: encapsulated inside the project model.
- 17:37Speaker 1: Right.
- 17:38Speaker 2: If you ever build a completely different controller, maybe an
- 17:40Speaker 2: admin dashboard, that also needs to render a project as Jason,
- 17:45Speaker 2: it automatically inherits all of these perfect rules.
- 17:48Speaker 1: It's brilliant. We've taken that chaotic, overflowing database suitcase and
- 17:52Speaker 1: reorganized it into a sleek, impossibly efficient carry on back.
- 17:56Speaker 2: That's a great way to put it.
- 17:57Speaker 1: Let's do a quick recap of the architectural journey we
- 17:59Speaker 1: just took for a listening. We started by exploring how
- 18:02Speaker 1: API controllers act as intelligent switchboards, utilizing ar respro respond
- 18:06Speaker 1: to and HTP accept headers to route different data formats
- 18:09Speaker 1: from a single endpoint. Right, we broke down the two
- 18:12Speaker 1: step dance of serialization, leveraging as underscore json to prepare
- 18:16Speaker 1: a manipulatable dictionary and to underscore json to handle the
- 18:20Speaker 1: final text transformation.
- 18:21Speaker 2: And we solve the catastrophic double encoding bug yes by.
- 18:25Speaker 1: Letting the render method handle strangifications safely at the top level.
- 18:29Speaker 1: And finally, we learned how to aggressively sculpt the data
- 18:32Speaker 1: payload by filtering out junk, including relationships, renaming, confusing keys,
- 18:37Speaker 1: and generating derived data to save client side processing power.
- 18:42Speaker 2: It's a really comprehensive approach to data delivery, but theoretical
- 18:46Speaker 2: knowledge only matters when you can apply it to real
- 18:49Speaker 2: world security constraints.
- 18:50Speaker 1: That is very true. So to really cement this logic,
- 18:54Speaker 1: I want you listening to think about your own applications
- 18:56Speaker 1: for a second. Imagine you were building out a user model.
- 19:00Speaker 1: This model has twenty different fields. Hidden among those fields
- 19:03Speaker 1: is a highly sensitive password hash and a massive paragraph
- 19:07Speaker 1: long user biography.
- 19:08Speaker 2: Okay, I'm with you.
- 19:09Speaker 1: If you are writing the as underscore Jason method for
- 19:12Speaker 1: this user model, you have to make a choice which
- 19:15Speaker 1: filtering option, using only to create a white list of
- 19:17Speaker 1: allowed fields or using accept to create a blacklist of
- 19:20Speaker 1: hidden fields would be fundamentally safer to ensure that the
- 19:23Speaker 1: password hash never accidentally leaks into a public API response.
- 19:27Speaker 2: That's a really crucial question. Consider the long term life
- 19:31Speaker 2: cycle of an application. If you hire a junior developer
- 19:34Speaker 2: six months from now and they add a new, highly
- 19:38Speaker 2: confidential security token to the database table, how does your
- 19:41Speaker 2: chosen filter react to the unknown.
- 19:44Speaker 1: If you used accept the blacklist approach, you'd have to
- 19:47Speaker 1: actively remember to go back into the serialization logic and
- 19:50Speaker 1: explicitly add that new security token to the blacklist. If
- 19:54Speaker 1: you forget, the system defaults to sending everything and the
- 19:57Speaker 1: token leaks immediately exactly. But if you use only the
- 20:00Speaker 1: whitelist approach, you're establishing a strict closed door. You define
- 20:04Speaker 1: exactly what is permitted to leave the server. When that
- 20:07Speaker 1: junior developer adds a new column to the database, it
- 20:10Speaker 1: is automatically hidden from the API by default until someone
- 20:13Speaker 1: explicitly goes in and authorizes it. When it comes to
- 20:17Speaker 1: sensitive data, a white list is almost always the superior
- 20:20Speaker 1: architectural choice.
- 20:21Speaker 2: It's the principle of secure by default. You build systems
- 20:24Speaker 2: that fail safely. But looking at all of this custom logic,
- 20:28Speaker 2: of the whitelists, the Unix conversions, the boolean calculations, it
- 20:33Speaker 2: raises a very important bigger picture question for anyone designing
- 20:37Speaker 2: a growing system.
- 20:38Speaker 1: Oh what's that?
- 20:40Speaker 2: If our database models are constantly burdened with transforming their
- 20:43Speaker 2: own data, calculating external logic, and managing presentation rules for
- 20:48Speaker 2: the API, at what point does a simple database representation
- 20:52Speaker 2: become too bloated with API specific logic? Oh?
- 20:55Speaker 1: Wow, that's a phenomenal point, because the model's primary job
- 20:58Speaker 1: is to interact with the data based table, not to
- 21:01Speaker 1: act as a full time presentation layer for the front end. Right.
- 21:04Speaker 2: So, as your application scals from a sandbox into an
- 21:06Speaker 2: enterprise system, is there a threshold where you might need
- 21:09Speaker 2: to introduce a dedicated architectural tool like what perhaps an
- 21:12Speaker 2: isolated serializer object or a dedicated Jason presentation builder whose
- 21:17Speaker 2: only job is to handle highly complex responses, allowing your
- 21:20Speaker 2: active record models to go back to simply being data models.
- 21:23Speaker 1: That is a fantastic architectural thought to chew on as
- 21:26Speaker 1: you design your own platforms. Just like our multilingual restaurant host,
- 21:31Speaker 1: there comes a point where the host is juggling too
- 21:34Speaker 1: many menus and translating to many languages, and management simply
- 21:38Speaker 1: needs to hire a dedicated translator.
- 21:40Speaker 2: Exactly.
- 21:41Speaker 1: Well, thank you so much for joining us on this
- 21:42Speaker 1: deep dive. Keep building resilient systems, keep experimenting with your architecture,
- 21:47Speaker 1: and most importantly, keep your API payloads lean. We'll catch
- 21:50Speaker 1: you next time.