Code Monkey home page Code Monkey logo

house-salad's Introduction

House Salad API Consumption Refactor

The purpose of the House Salad API Consumption Refactor exercise is to refactor the House Salad app with a particular focus on abstraction and encapsulation. This workshop was very helpful with solidifying the concepts of abstraction and encapsulation, and how they can be implemented during refactoring using declarative programming and the Red, Green, Refactor technique.

Refactored Items

I refactored out the calls to the API from the SearchController into a PropublicaService class, whose sole responsibility is calling out to the API and obtaining information. This code was then further refactored to split the connection to the API from the specific GET request. This was done so that the PropublicaService can be used to request other information from the Propublica API, without having to duplicate the code for the connection. This is a huge benefit for the adaptability of the app. This change also makes the overall app more flexible because it allows for the app to easily change the API without having to update a bunch of methods throughout the app. The only changes that would be required would be a new service with a new API URI, and perhaps updating the key names in the Member object instantiation to match the key names from the new API.

The logic of converting the results from the API into Member objects was also refactored out of the SearchController and into a new SearchResults poro. This new SearchResults poro follows encapsulation and abstraction because it encapsulates the creation of the Member objects into a single-responsibility class. It also follows abstraction because the actual call for information is abstracted out into the PropublicaService because the creation of Member objects does not need to know how the information needed to create those objects is obtained, it just needs the information.

Finally, a new Member class was created to instantiate Member objects with specific attributes that can be called in the view, instead of sifting through the hash in the view. There are many benefits of instantiating Member objects instead of searching through the Hash for the desired information. By instantiating objects, the code is much more versatile and can be easily updated to accommodate changes to the API. For example, if the key names change in the API, you only need to update the initialization method in the Member object, instead of having to change the key name in every view that displays that information. Similarly, instantiating a Member object gives the developer more control over the names of attributes and what information is available for use. Perhaps the developer wants to name the party attribute political_party instead, for more clarity. That change would not be possible without instantiating an object because the developer would be dependent on the keys provided via the API.

house-salad's People

Contributors

antoniojacksonii avatar dionew1 avatar dependabot[bot] avatar brianzanti avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.