Project Ramon

A learning journey from a Ruby noob perspective

Lunch Panoply: Rendering API responses and other Sunday goodies

api10_header_image

Hello and welcome.

If you remember from Friday’s post I have been attempting to get my specs from red to green based on an AirPair where I received mentorship from Adam Cuppy. During our hour together he mentioned using the VCR gem, by: Myron Marston.

I spent some time last night and today working through a few tutorials. And although I haven’t figured out how to get a cassette to play yet, I have stumbled upon a solution for a different requirement I need to satisfy in my project. But before I go into what that is and how I arrived at the solution, I wanted to list my current list of resources for understanding VCR. If you know of any additional resources that you have found helpful, feel free to leave the link(s) in a comment below.

Here’s my list so far:

  1. Testing with VCR Railscast (pro)
  2. VCR’s Relish documentation
  3. How to use VCR Gem with Rails and Rspec by: Natasha the Robot

I think the key for me is to give this a few more attempts, walking through all the docs and tutorials to ensure that I’m following and understanding the steps necessary. I hope to have an opportunity to write about API testing soon.

Sunday Access

Today while searching for ways to more fluently understand and use the Faraday gem, I managed to run across these two StackOverflows:

  1. Convert a JSON formatted key value pair into a Ruby hash
  2. Assign value from an API response to a specific model object in Rails

The first in the list is the one that I followed, wich changed my browser output from a page full of this:

api10_img1

Into a rendering where I had control over which key/value pairs are being displayed, and now looks like so:

api10_img2

This isn’t a huge step forward programmatically, and although it does feel swell to be able to satisfy a requirement I was stumped on only a week or so ago, this is only half of a solution. The second StackOverflow listed above looks to be the second half of the solution to my requirement.

Adam mentioned that there were two main ways to get external web service data to our end user.
One, was to build up specific queries and as an end user makes a selection my application would make the query request and have to wait for a response to return, in real time.

The second way he mentioned was for my application to get all the data needed (updating the web service data at intervals), store this data into a Rails model by mapping the web service’s key names with attribute names I would build to allow us to use more Rails convention.

I plan on working on this as soon as I can regain my balance on the current API testing challenges.

Code Walk Thru

So to finish off today, I’ll just show you how I rewrote my faraday code in the MenuItemsRetriever adapter class and how I accessed these keys in the view.

menu_items_retriver.rb

api10_img3

There were only two things that kept me stumbling over generating a response from a Faraday.get request. On lines 23 – 27 I had to ensure that I passed an empty hash before adding the additional header hash on line 26. The documentation I used as my guide can be found here.

And the second hurdle was calling payload[:dishes] to provide the entire menu (since it’s the index page) instead of narrowing down the results here (i.e. payload[:dishes][:name][:etc]). Now I can use the instance variable to list the attributes I want in the index view through some iteration.

api10_img4

Neither the model nor controller had to change, thanks to the adapter pattern!

And here are a few additional resources I’m planning on reading:

Losing Time to Faraday by: J. B. Rainsberger

Oh My Faraday by: Björn Rochel

Mocking Web Services with VCR by: Mark Maglana

Stay tuned…

Advertisements

Categories: Newbie, Ruby on rails

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s