Project Ramon

A learning journey from a Ruby noob perspective

Lunch Panoply: Testing a Request & Other Improvements

Digital StillCamera

Hello, today I didn’t quite get to building queries in my pair with Adam Cuppy today. Adam did however share some valuable insights into the area of how to provide coverage of an external web service. I’ll briefly go over what I learned so far about that, and then show in my code some of the changes I have started making. I’m very glad that we went over some specs, it gave me a sort of clarity, even with just the little we covered, of what’s actually going on between my adapter class and the external api I’m sending requests to.

Faraday

I ended up using the Faraday HTTP client library to make my requests with. I found quite a few blog post tutorials about how to use it, and being able to easily pass in my header info to the request made utilizing Faraday a simple decision for me.

First here is a look at some of the posts on Faraday that I’ve found valuable:

And now I’ll share how I’ve re-arranged some of my code based upon what I’d like to think is a slightly deeper understanding of Lunch Panoply than I had even a couple of days ago.

Here’s the outline of what I’ve changed:

  1. Changed MenuItemsRetriever from a module into a class.
  2. Created instance methods inside of MenuItemsRetriever that will eventually implement the different tabs I’ll have set up in the index view.
  3. Removed the api_clients folder that I spoke on yesterday, and placed the MenuItemsRetriever class in the models folder. I kept having to restart my server every time I changed code when I was using the module in its own folder.
  4. Adam very graciously walked me through three specs covering two instance methods.

MenuItemsRetriever

In yesterday’s post I showed an illustration of my MenuItemsRetriever module. Inside it, I showed only one class method self.all_meals.

From a user’s perspective, I want to have an index view that not only displays all menu items, but provides tabs that allows a user to switch views between certain types of menu items.

Lets take a look:

The retrieve method is the interface we’re defining for our web service. This just means that all communication with the api is going to be made through the retrieve method. And all we have to do is pass in one of the four methods below it.

Sending a Faraday Request

This is mostly a repeat from yesterday, I’ve figured out how to call only one menu_item from the list of menu_items in the JSON response. But I’m still working out how to access particular attributes of the response hash.

I was very pleased with how intuitive sending a GET request in Faraday was. The header hang-ups I’ve been having will no longer be an issue thanks to the simplicity of being able to include the headers with the url argument that Faraday’s GET method is looking for.

Here’s one example:

api8_img1

Notice at line: 22 in the image, I am successfully grabbing the eleventh menu item from this restaurant.

Here is the output from the browser:

api8_img2

Far be it from me to break out the fireworks and sauerkraut from seeing yet another hash render in the browser, but I am pleased about the small but definitive progress gained over the past couple of days. I now can pull one menu item out from amongst it’s peers, baby steps are still steps in the right direction!

Test Coverage

I learned a bunch today about testing! And to be honest, I want to re-watch my expert session with Adam before talking in depth about this. I have never used Rspec stubs before, and found great value in listening and discussing with Adam as he patiently brought me up to speed.

I’m just going to show the three specs, and will probably share more thoughts on this when I have wrapped my head around whats happening a little better.

Here it is:

api8_img3

The parts that I do understand is the describe '#retreive' block as the expectation is somewhat similar to how I write my index controller specs. Making sure that my controllers instance variable inside of the index method is assigned and returns a collection of objects.

Well, thats pretty much all for today. I’m going to be working on letting all the instruction and insights I’ve been given this week sink in.

Stay tuned…

Advertisements

Categories: 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