Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consuming REST API from Rails Application

I'm building my first Rails App and I want it to consume everything from a REST API. What I want to do is to have Rails serve my web application as a frontend to my API. As far as I've read (I'm staring with Rails right now), Rails has a lot of potential with ORMs and direct access to database systems. My platform, on the other hand, is designed in such a way that every layer is accessed via a defined interface (in this case a REST API), so no databases are read from any client, but via their interfaces.

For example, my API exposes the following resource:

https://api.example.com/v1/users/feature-xxx [GET] 

And I want my web app to have a page like:

https://example.com/feature 

So the users will visit this URL and when logged in, the Rails app will request the data to generate this dynamic content from my API.

The question is:

  • What are the necessary steps for my Rails Application to consume its data from a HTTP/Rest Backend? and,
  • Is this a good design for a Rails app?

Thanks!

like image 982
licorna Avatar asked Mar 22 '11 02:03

licorna


People also ask

Is Rails API RESTful?

Having spent a lot of time working with JavaScript and React recently, I thought I'd revisit Rails for some practice, and wanted to write a post about creating a RESTful API using Ruby on Rails. Rails now has the option to create an API application which is a slimmed down version of a traditional Rails web app.


1 Answers

ActiveResource is no longer being included in Rails 4.0. Word is that it is barely maintained these days, and is difficult to customize for REST API endpoints that are not formulated according to the "Rails way".

After some research, I am heavily favoring using Faraday. It includes support for utilizing different HTTP request adapters. It can use EventMachine directly, or libraries like Typhoeus for when you decide to get concurrent. It also supports Rack-like middleware for seamlessly including, say, authentication.

For a REST ORM in Rails that is easily configurable, the relatively new (about a year old) Her looks very promising, and utilizes Faraday.

Update

I completely <3 RestClient. So simple. If you need ORM-style functionality, then you'll need a higher-level abstraction, but for simply consuming an API, you can't beat its simplicity. It just does exactly what it's supposed to do without fuss, has sensible defaults, and has the ability to set advanced options such as auth headers.

like image 113
Duke Avatar answered Sep 18 '22 15:09

Duke