How do you create an API server based on data from an existing Rails/Postgres web application? - ruby-on-rails

I have an existing web application that's developed with Ruby on Rails and PostgreSQL. I need to create a mobile application (and possibly a separate web application) using the data from that web application, so I'm looking to create an API server. Is it possible to do this without altering the source code from the original Rails/Postgres web application?
Any ideas on the best way to do this? Or can someone point me in the right direction on what to research?

To connect a new application hosted on Heroku to a PostgreSQL database hosted on Heroku just push your new application to Heroku as normal.
Then, under Settings on your new application dashboard, go into Config Variables and add a new config for DATABASE_URL. Put the value of the url for your existing database.
Your new application will need to be under the same account as your existing application. Heroku doesn't allow you to connect across accounts.
You probably want to take a look at this question for additional details.

Sounds like essentially you want to have two applications connecting to the same database offering the same methods, but respond in different formats (html vs, for example, json). One way of doing that relatively easily might be pushing another api only Rails app to heroku that connects to the same Postgres database (which was mentioned in the comments), but you would have to figure out how to handle authentication differently for your API end points. This depends on whether you are exposing these end points to the public or to something like a mobile front-end. You may want to switch to token-based authentication if you were formerly using sessions on the web-app. Once you implement secure authenticatoin for your api routes, all you have to do is make sure your methods, instead of rendering erb or haml templates, are returning raw data consumable by your intended client.

Related

Stand Alone ROR App Security Issues

I have a Ruby on Rails-based website from which I want to create a stand-alone application (desktop app) so that users have an interface from their own computer without opening the browser and so that access is restricted to the people that have the app.
I am looking at the security of this and not sure how exactly to best go about this.
In a typical ROR site you have a database.yml file with your database information in it.
If I use RubyScript2Exe I think it would be too easy for someone to access this information. Maybe I am wrong.
My question is should I build this app as an API only thru my main site or should I build it to directly interface with the database.
One other option is to create its own database on each desktop computer to save their posts and an interface with the main database thru an api call.
My app will need to:
Create New Records in Database
Search Database
Delete records created by user
Main site uses Devise for log-in authenication

Heroku: encapsulate data access in an API

I have a Heroku app with a PostgreSQL DB. Now I want to have a seperate process, possibly on a different machine, to access the DB. The suggestion on the Herkou site is actually what I wanted to do myself:
No, connecting to your database from machines outside of Heroku is not supported.
We recommend that you encapsulate data access in an API to manipulate it.
But I'm not sure how that's done best. I'd like to be able to send some JSON to the API and get back JSON as a result of that request. Like "give me all posts that are expired for a bunch of given userIds" or "update all users to be suspended if their posts contain any of the given words" in an example micropost app.
What's the best way to achieve that? Can I write an extra ruby program that is accessible via TCP and accepts JSON input? Is that even possible with Heroku? Or do I have to integrate the API into my rails app somehow? How?
Thankful for any ideas,
Tom
You will have to integrate the API to your rails app, there are many solutions for this, one of them is using a tool specialized for API building like Grape and mount it with your Rails app. This way you could have your Rails app running and also the API, both sharing the same codebase.

Communication between Rails apps

I have built two rails apps that need to communicate and send files between each other. For example one rails app would send a request to view a table in the other apps' database. The other app would then render json of that table and send it back. I would also like one app to send a text file stored in its public directory to the other app's public directory.
I have never done anything like this so I don't even know where to begin. Any help would be appreciated. Thanks!
You requirement is common for almost all the web apps irrespective of rails, Communicating with each other is required by most modern web apps. But there is a small understanding that you need to get hold on,
Web sites should not directly access each others internal data (such as tables), (even if they are build by the same language (in this case Rails) by the same developer),
That is where the web-services comes in to play, So you should expose your data through web services so that not only rails application can consume that, but also any app that knows how to consume a web service will get benefit.
Coming back to your question with Rails, rails supports REST web services out of the box, So do some googling about web services, REST web services with rails
HTH
As a starting point, look at ActiveResource.
Railscast
docs
Message queuing systems such as RabbitMQ may be used to communicate things internally between different apps such as a "mailer" app and a main "hub" application.
Alternatively, you can use a shared connection to something like redis stick things onto a "queue" in one app and read them for processing from the other.
In recent Rails versions, it is rather easy to develop API only applications. In the Rails core master, there was even a special application type for these apps briefly (until it got yanked again). But it is still available as a plugin and probably one day becomes actually part of Rails core again. See http://blog.wyeworks.com/2012/4/20/rails-for-api-applications-rails-api-released for more information.
To actually develop and maintain the API of the backend service and make sure both backend and frontend have the same understanding of the resources, you can use ROAR which is great way to build great APIs.
Generally, you should fully define your backend application with an API. Trying to be clever and to skip some of the design steps will only bring you headaches in the long run...
Check out Morpheus. It lets you create RESTful services and use familiar ActiveRecord syntax in the client.

Ruby on Rails: how to design a SaaS infrastructure?

I'm building a Rails 3 application that I plan on provided as a subscription-based SaaS (Software as a Service) product. Basically, I want users to be able to hit my "Sign up" page, create a new account, and immediately start using the software.
A good example of what I'm trying to accomplish is: http://www.getharvest.com/
Here's what I need to happen when someone signs up:
A MySQL database for them is generated on the db server
A sub-domain is created (e.g., companyx.awesomeapp.com)
The Rails app should know the appropriate database to connect to based on the sub-domain
Are there any good guides out there for setting this stuff up? Even better, are there services that you can purchase to automate this type of thing? Ideally, I'd like to just worry about writing my Rails app and then be able to plop it atop some awesome Rails SaaS infrastructure.
(Also, I need a way to bill them monthly, but I think that's a separate question/problem.)
Heroku would let you get up and running quickly. You can manage the infrastructure using the heroku gem. Here is the documentation to the client which should allow you to manage heroku applications remotely. Using heroku would allow you to scale applications on an individual level and let you focus on the code of the application instead of the hardware.

How do I build an API for my Rails app, so that multiple sites can share one database?

I have a Rails application that right now is pretty standard: Heroku/PostgreSQL backend, users go directly to my site to update data, there's no mobile app or anything. We're going to start licensing out the tech to other companies, so that different versions of the interface live on company1.mywebsite.com, company2.mywebsite.com, etc, where all of these interfaces share the same database.
I want some advice on how to go about building this. Do I create a separate Rails app for company1, company2, etc (with a lot of redundant code) and then set up each of them with API keys to query my master app, using its RESTful routes?
Any tutorials to point me to would be great as well.
I recommend you the book Service Oriented Design with Ruby and Rails, by Paul Dix. It has a lot of info about the kind of system that you want to build.
To answer your question:
Build an API server. It serves a JSON – for example – RESTful interface.
api.mydomain/client1/users.json
Build a frontend server. It consume the API service – using typhoeus for example – and serves the final pages. It uses a subdomain or domain name for identification of different clients.
client1.mydomain/users
We have a similar "platform".
What we did:
build a master API app (REST + Push)
build a core plugin for rails which has all the shared code
build a separate rails app for each client which has all the client specific code
We are using this setup for 3 years now and I'm pretty happy with it.

Resources