An application programming interface (API) is a computing interface to a software component or a system, that defines how other components or systems can use it. It defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, the conventions to follow, etc. It can also provide extension mechanisms so that users can extend existing functionality in various ways and to varying degrees. An API can be entirely custom, specific to a component, or it can be designed based on an industry standard to ensure interoperability. Some APIs have to be documented, others are designed so that they can be “interrogated” to determine supported functionality. Since other components/systems rely only on the API, the system that provides the API can (ideally) change its internal details “behind” that API without affecting its users.
first of all , you add those gems to gem file;
1–)gem ‘dotenv-rails’
2-)gem ‘rest-client’
3-)gem ‘json’, ‘~> 1.8’, ‘>= 1.8.3’
gem ‘dotenv-rails’
One of the crucial things while developing an app is to make it secured, and it starts with making sure that your app’s credentials with another web apps are secured. Dotenv helps us to do that.
after adding these gems, let’s run the bundle in your terminal.
you should be able to have a request to a website which has an api , some websites requires you to do sign up and they send you api key by email.
in your seed file, you should type that url which you get from a website.
@url = “http://www.omdbapi.com/?t=Top+Gun+&apikey=*******"
add your key to the api that you get email from that website.
@response= RestClient.get(@url)
@body = @response.body
@output = JSON.parse(@body)
I just called output ,but you can call any thing you would like.
and then you create your data with using Api.
Movie.create(title: @output[“Title”], year: @output[“Year”], plot: @output[“Plot”], genre: @output[“Genre”],director: @output[“Director”], award: @output[“Awards”], poster: @output[“Poster”] )
references :