Posts

Showing posts from October, 2022

Rails: Create an API Wrapper of an External Service

  Introduction Have  you ever had the problem when you wanted to use an external API that didn’t have a ready-made library and you needed to write everything from scratch? You weren’t sure how everything should be split and separated in order to write readable and clear code? If you answered yes to any of these questions, this article is for you. I’ll show you how to write code that is responsible for connecting with the external API and how to split everything in order to write clear and maintainable code. It doesn’t matter if you use Rails or plain Ruby code, this code is flexible, so you can use it wherever you want. Let’s add a structure Your API wrappers should be kept in the app/apis folder. In the Rails application there is a folder for models, views, controllers – so why not add another one, for APIs? Let’s assume that there is no ready-made gem for Github API and we need to write their API wrapper. Start with creating a  github_api  folder in the  apis ...