Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use external API in Ruby in Rails

I need to use an external API in my app in order to have companies informations. Beginning and having never used API in ruby, I don't know where to start. Maybe there is a gem for it but I have found 2 API that returns me JSON : https://datainfogreffe.fr/api/v1/documentation and https://firmapi.com/ (they're in french sorry about that). Does someone have a good tutorial or hints to help me begin ?

The final need is to retrieve companies datas just by giving the company ID.

like image 744
Charles Duporge Avatar asked Oct 12 '16 09:10

Charles Duporge


2 Answers

You can use Net::HTTP to call APIs in Ruby on Rails.

  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(uri.path, {'Content-Type' => 'application/json'})

  request.body = {} # SOME JSON DATA e.g {msg: 'Why'}.to_json

  response = http.request(request)

  body = JSON.parse(response.body) # e.g {answer: 'because it was there'}

http://ruby-doc.org/stdlib-2.3.1/libdoc/net/http/rdoc/Net/HTTP.html

like image 184
Gokul Avatar answered Sep 26 '22 01:09

Gokul


You can use gem for calling REST APIs in ruby.

Also, if you want to find any ruby gem for any purpose you can have a look at this.

You can have a look at this to get company details.

like image 28
Jayaprakash Avatar answered Sep 22 '22 01:09

Jayaprakash