HAVE… YOU MET JSON AND RUBY
Thinking about possibly naming my future kids these names. LOL!
We’ve all met Ruby and we all miss her. We were glad to have her back for now.
We’ve all met Json and we don’t really quite get him, but we want to.
But having these two to work together, it’s quite a pain, let me tell you.
First, we wanted to work with yelp’s api. We got a token, a client id and client secret code. But somehow it was still not working how we want it to be. It kept giving us this error:
At some point we got it working but it kept asking for a lot of the parameters to be included, and we wanted it to work with just the zipcode paramater. We were almost about to give up and use google’s api (which we should have, probably easier) but then my man Gianpaul was clever enough to figure out to use post man even though we both really don’t know how that thing works. (To get screenshots for this blog, I had to learn how to do it too.)
So that was day 1’s problem and we left that day victorious and hopeful that it’s going to be easy peasy after this. The following day, we got another problem that took us the whole day to figure out. With that JSON data we got to work, we thought that copy pasting it to our seed file is all we need to do to make it all work.
The moment it was time to render it on localhost site we were getting this:
We tried manipulating the JSON file we copy pasted on our seed file and some other things to make it work. Thankfully, Andrew came to the rescue and pointed out the error we were getting on our terminal:
We forgot to set up our Serializer!! If only we paid attention to what Lindsey said and slacked us about this very matter, we probably would have known what was wrong. Lesson learned: Listen to Lindsey!
After following the steps on the documentation to set it up:
We were still getting the same null value on our localhost site. This time, Tim came to the rescue. He went over the steps with us and made sure that we got our syntaxes correct and namespaces and all that right. But somehow we were still getting the same error. Then he told us to put the attributes one by one, like this:
AND GUESS WHAT?!
IT FREAKING WORKED! Slowly added back each attribute, and it still worked! I don’t know what Tim did, but he’s a Serializer whisperer!
Then the test was making BE and FE talk.
But it’s true what they say, promises are made to be broken.
We then changed our getSearch code to make sure that our fetching was correct. But it was still giving us the same undefined promise value. We tried object.keys, object.assign — no luck!
Then Tim (the Serializer Whisperer), pointed out that we needed to turn our Json to a Ruby object. And we know there are a few ways to do it, like the common way which is to parse it. But we really wanted to challenge ourselves, so we ended up setting our json file into a variable and writing a ruby method for it:
So a few takeaways:
- Get a better api to work with
- If you end up copy pasting json data to your seed file, do not forget to change it to a Ruby object — more documentation here: https://www.twilio.com/blog/2015/10/4-ways-to-parse-a-json-api-with-ruby.html
- Don’t forget to set your serializers
- Check your naming spaces/conventions
The Ruby Toolbox lists no less than Let's look at how to retrieve and parse JSON results from a RESTful API using the…www.twilio.com