We’re working on a new app, as some of you may know, and it’s required we find some sort of database to get food data from. Calories, protein, serving sizes, etc. We started with the USDA food database. This thing is a piece of garbage. Naming conventions are inconsistent and absurdly long, sometimes in ALL CAPS even. It was also missing several fairly common foods (coconut oil, anyone?). Not to mention it’s distributed as either a Microsoft Access database, an Excel sheet, or a custom file format. Really? Access or Excel? Those were immediately unusable to us, so we were left with the custom file format. This thing is a real piece of work, let me give you a taste:

~06075~^~SOUP,BF BROTH OR BOUILLON,PDR,DRY~^3.27^213 ^15.97^8.89^54.50^17.40^0.0^16.71^60^1.00^51^320^446^26000 ^0.00^0.000^0.459^27.6^0.0^0.070^0.243^4.467^0.300^0.200^32 ^0^32^32^113.4^1.00^0^0^0^0^0^0^0^0^2.17^0.0^0^3.2^4.320 ^3.616^0.332^10^3.6^~1 cube~^6.0^~1 packet~^0 ~06076~^~SOUP,BEEF BROTH,CUBED,DRY~^3.30^170 ^17.30^4.00^59.30^16.10^0.0^14.51^60^2.23^50^225^403^24000 ^0.21^0.000^0.386^27.6^0.0^0.200^0.240^3.300^0.300^0.200^32 ^0^32^32^113.4^1.00^1^0^0^0^0^0^0^0^0.00^0.0^0^0.0^1.990 ^1.670^0.160^4^3.6^~1 cube~^^~~^0

I spent a good many hours writing a custom parser for that beauty. Why in the world can’t they just do JSON, or I’d even get my big-boy pants on and suffer through some XML. We no longer live in an age when your best option is custom file formats! At the VERY least provide some sort of parser I can use. I… just… how…

So we decided to pivot. We did some searching and found FatSecret. It’s paid for the Premier tier (which we’ll probably end up having to get) but it also has a free plan. Hallelujah.

After determining that it was our best option, I unknowingly dove into the pits of OAuth hell. It authenticates with OAuth, which is fine, except there’s not fantastic iOS library support for OAuth. I did some searching and the best I could find was OAuthCore, which generates a signed header, but I needed it as a query string in a GET request. Wasn’t too tough, just switched a few lines of code, debugged it a good while, scratched my head, and suddenly it worked. I was moments away from giving up when light shown through the heavens and I was Enlightened. I got back some JSON of food. And it was very desirable. Consistent naming, hundreds of results, it was beautiful. I cried. Not really, I drank some Pomegranate 7-up and kissed my wife.

All there was left to do was abstract my code into an open source library. I’d give you a song-and-dance about MT’s commitment (read: burning passion) for open source (which is true) but it really, in this case, came down to how cool CocoaPods is that there’s really no reason not to open source something like this. Here it is, if you ever need it. I pray even my worst enemy won’t find that USDA database.

And that’s the Tale of FatSecretKit.