- How we created the gem
- How we wrapped the API
- How we tested the API
- Using Formulae in a Rails project
In previous episodes, we saw the back-end of Formulae and the front-end. Today, we will see how we built our ruby library to be used with any ruby project, and check how Formulae can be used inside a Rails project.
We have here at DailyDrip two interesting episodes that can help you create a gem for Ruby.
First episode is [wrapping a REST API into a gem]((https://www.dailydrip.com/topics/rails/drips/wrapping-a-rest-api).
The second episode is Testing HTTP requests using VCR.
I will get the best parts of these two episodes: explaining how we wrap our Formulae API library into a gem, and test the HTTP requests.
How our Gem should be
Our API has some endpoints: Form, Sections, Questions and others. We need to have a way to interact with this API from a ruby code. We will worry only with the Form endpoint.
With this in mind, our gem should have have at least two functionalities:
We will have a
FormClient class, we pass the
API_KEY and then we can look for a specific form or get all the forms.
Wrapping the REST Api
To make requests to our API, we will need to use some library to make HTTP requests, we will use Faraday.
We have in our gem clients and resources. The clients connect with the API and the resources store the data we receive.
We created a gem to do that. Here you can see the gemspec and the dependencies we are using.
# Development Dependencies
spec.add_development_dependency 'bundler', '~> 1.14'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
Faraday to handle the HTTP requests, rspec to our tests and webmock. We will talk more about tests in a minute.
On this file, we import all the files we are using:
Here are all the endpoints we have and the
FormClient, the only client we are using for the moment.
Testing our API
We used rspec and webmock to test our app. Rspec to test our models and webmock to help us with the client test. We shouldn't rely on our API to make tests. Additionally, our tests make HTTP requests. It can make our API slow, especially if your API has a rate limit.
Here is an example of how we are testing our api using webmock.
json = JSON.parse(File.read('./spec/responses/form_all.json'))
.to_return(status: 200, body: json)
Here we are mocking our requests. So, our requests coming from here will return what we want and they will not touch the API. Take some time watching these two episodes they will give you an overview on testing an API and Wrapping it into a gem.
Using Formulae in a Rails project
Let's take a look at how we can use a Formulae in a rails application. We have a Rails 5.1 project, and we want to import use of our formulae library on it. How can we use it?
package.json we can add the
formulae_react as a npm package.
We can import our
} from "@dailydrip/formulae_react";
And we can use it. In our pack, we pass our store and we render the element in a div.
<RespondToForm displaySectionsAs="HEADINGS" />
// Example for AdministerForm
In our rails view, we can call the function we created in our pack and pass the arguments from our rails controller. Running it with webpack will make our component work :)
var el = document.getElementById("respond-to-form");
loadRespondToForm(el, "<%= @form_id %>", "<%= @form_display %>");
We saw we wrapped our API into a gem, how we tested this gem and how to use our Formulae react library in a Rails project using webpack.