“They were careless people, Tom and Daisy- they smashed up things and creatures and then retreated back into their money or their vast carelessness or whatever it was that kept them together, and let other people clean up the mess they had made.” –F. Scott Fitgerald
If there was a metaphorical representation of a third party API, Leo would be it.
The Great Gatsby. Eternally eloquent, dazzling, sparkling, but somehow you can’t help but feel that you were handed a big broken, mess, you need to JSON your way out of!
APIs aren’t exactly “messy” so to speak, in fact, they are quite the opposite, a careful, calculated way to access an Application Programming Interface’s data–yet to someone interacting with an API for the first time, you can’t help but be a bit messy, “careless” so to speak–in fact, it’s completely necessary to understand how your API is working.
This blog will attempt to give you the tools to wrecklessly and glamorously parse APIs. Before you know it, you’ll be flying over the Queensboro bridge, all of New York City in your view as you glide slowly but stylishly (cause you’re in a hot car with a 12 cylinder engine 😎) headfirst into the city that never sleeps.
First, it sounds obvious, but at times, we all need the obvious pointed out to us with exclamation marks!!!!!!! and –>arrows<–. LOUD AND CLEAR. Making an API call is simply accessing the correct URL for your desired data (well… you are also scraping the JSON located at a SPECIFIC website, but this is a good starting understanding.)
Now, look up at the top of the screen. RIGHT NOW! The url you are currently at is “http://beccaades.github.io/what-we-talk-about-when-we-talk-about-third-party-apis”. This is where you come on the internet to read my blog about API’s!
Now for THE RULES:
1st RULE OF API’s: READ THE DOCUMENTATION.
The latest API I used, instagram, includes a developer page. It tells you all the different endpoints you will need to access their data, and you can get to playing immediately, by placing the different queries in your browser.
This is what they give you:
This is what you do:
And this is what you get:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
(SIDENOTE: Please don’t be a dummy and know that you will have to actually replace the access token with your own.)
To access this information you can treat the JSON like a hash. Let’s say we set this equal to the variable data_hash. If I wanted to access “Swatch,” I could enter data_hash[:data][:name]and it would return “Swatch” back to me.
2nd RULE OF API’s: PLAY!!
Now. Listen close. I have a little gem to share with you. The httpary gem to be exact.
This gem lets you get down to business quickly by simply requiring it and then gaining direct access to JSON in the API to your app, to gain both familiarity and explore potential methods for your classes.
To use the httparty gem you simply require it in your gem file. Then in your model, your require the gem like so:
1 2 3 4 5 6 7
You can then begin by binding into the respond and deciding what information you need and begin to structure your methods.
3rd RULE OF API’s: ASK THE API’s WHAT METHODS THEY HAVE
Once you are able to access the JSON, don’t be afraid to ask the API exactly what methods they have! They may just tickle your fancy with some extra methods that they don’t discuss in the documentation. For example, the only way, I was able to learn about instagram’s nifty tag_count feature was through directly asking instagram what methods they have (rails console, baby!) and I got this quite lovely response (amongy MANY many others).
1 2 3
I simply did this by entering the following into my console:
How wonderfully convenient.
4th RULE OF API’s: WRAP THE API’S METHODS IN YOUR OWN INSTANCE METHODS
Once you have found the desired method, it is time to wrap it in an instance method. For example the instagram tag count method could be wrapped as such:
1 2 3 4
Results for the tag “HTTparty” gives you back :
The people of instagram are definitely missing out.
And there you have it. A step by step guide to deconstruct the many faces third party API’s may bring us. Cheers to that!