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.
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:
- Faraday: Advanced HTTP requests made easy. by: Mislav Marohnić
- Faraday: One HTTP Client To Rule Them All. by: Jerry Cheung
- Writing modular HTTP client code with Faraday. by: Kevin Faustino
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:
MenuItemsRetrieverfrom a module into a class.
Created instance methods inside of
MenuItemsRetrieverthat will eventually implement the different tabs I’ll have set up in the index view.
Removed the api_clients folder that I spoke on yesterday, and placed the
MenuItemsRetrieverclass 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.
- Adam very graciously walked me through three specs covering two instance methods.
In yesterday’s post I showed an illustration of my
MenuItemsRetriever module. Inside it, I showed only one class method
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:
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:
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!
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:
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.
Categories: Ruby on rails