DEV Community

blindbat
blindbat

Posted on

Requesting data from an API with Rails

I'll be using polygon.io API in the below examples.

Gem to Install

Gemfile:

gem 'faraday'
gem 'json', '~> 2.5', '>= 2.5.1'
gem 'dotenv-rails', groups: [:development, :test]
Enter fullscreen mode Exit fullscreen mode

Alt Text
Execute:

bundle
Enter fullscreen mode Exit fullscreen mode

Generate Controller & View for making API requests

rails g resource stock
Enter fullscreen mode Exit fullscreen mode

Alt Text

Now open /config/routes.rb and add this:

Alt Text
We'll make API requests using this route and display responses on a browser on the same route.
Alt Text

Open /app/views/stocks/search and fill it with:

Alt Text
Which gives us a form to enter a ticker whose stock data we can pull from Polygon API. And in the if statement, we have a response variable set to response from API if we made a call in the controller.

if params['stock']
  @response = Searchstock::Search.by_stock(params['stock'])
end
Enter fullscreen mode Exit fullscreen mode

Create API module

Now let's actually get into the process of making an API request to Searchstock.
Although we have Faraday installed, it can't be directly called from within the controller.
Add it inside /controller/concerns/searchstock.rb. Make sure not to change the name of the folder.

I'll create searchstock.rb inside /concerns/searchstock.rb

The class inside the module and the method inside the class can be named anything.

From the code above, we can make out that - by calling Searchstock::Search.by_stock method with a ticker parameter would make an API call to Searchstock, but what about ENV['API_KEY']?
We sure can replace ENV['API_KEY'] with Polygon's actual API key, but leaving an API key within the code is not right. So, we are going to place our API key as an Environment variable.
First, let's place the API key as an Environment variable in our local machine.
To do this, we'll be using dotenv gem.

Install Dotenv-rails

Add this line to the top of your application's Gemfile:
gem 'dotenv-rails', groups: [:development, :test]
And then execute:
bundle
Now add a file named .env to the root of your application. Make sure not to change the name of the file. Add your API key in the first line like so:
API_KEY=your_api_key
Replace your_api_key with your actual key provided by OpenWeather.
If you're going to commit your work, add the .env file to .gitignore so that git does not track it.

Call API from the controller

Now for the final part, add this to the search action of your controller /app/controllers/stock_controller.rb

if params['stock']
  @response = Searchstock::Search.by_stock(params['stock'])
end
Enter fullscreen mode Exit fullscreen mode

Make sure to match the names of the module, class, and function that you named earlier.
Alt Text

Check the result

Now, open the server
rails server
And visit http://localhost:3000/stocks/search from your browser.
Search for a ticker to see the response.

Top comments (0)