Connecting my Rails Web app database to my Rails API on Heroku - ruby-on-rails

I feel like my question is partially answered through a number of other questions, but I just can't piece everything together for my case.
Basically, I'd like to build a fairly simple web app, but also be able to fairly easily build a mobile app for it down the line. From my research, the best practice would be to build a RESTful JSON API, and then use that API both for my web app and for my mobile app.
I've already gone ahead and developed authentication for my JSON API (using : http://lucatironi.net/tutorial/2015/08/23/rails_api_authentication_warden/) and I've got it up on Heroku. Now for my web app, I'm using HTTParty to consume that API, and I'm getting the JSON back all fine and dandy. However, I'm not quite so sure what to do with the database. My first thought was that maybe I need to access the API's database via it's own API calls, but that seems incredibly complex (just the thought of having to make functions for User.find_by_<every attribute> makes me scared). The other thought would be to have my web app use the API app's database. However, I can't find any info on how to do that if the app is on Heroku.
So basically, I'm wondering what the best way to go about this would be. Surely there is a standard out there that every big or small web app with an API uses. I'm really surprised that something so prevalent these days is hard to find good answers for. Or am I going about this all wrong? Any pointers would be greatly appreciated.

Databases are add-ons in Heroku parlance.
Use heroku addons to get a list of the add-ons for your apps.
$ heroku addons -a appname
Add-on Plan Price
──────────────────────────────────────── ───────── ─────
heroku-postgresql (relaxing-purely-8366) hobby-dev free
└─ as DATABASE
First you may want to remove the database attached to your front-end app:
heroku addons:destroy fat-samurai-1234
Then find the name of the database add-on attached to your API app and attach the add-on to your frontend app.
heroku addons:attach shining-sushi-1234 -a sushi.
https://devcenter.heroku.com/articles/managing-add-ons#creating-an-add-on
As to which approach is legitimate that's a matter of discussion. Many apps today use a single page architecture where the entire app is created on the front end - in javascript. So your app just consumes your REST api. Popular frameworks for this are Ember.js, Bootstrap and Angular.
The other approach would be to to create a classic server side app - while it's possible to create an app that would consume the REST api there is a significant overhead involved so it will be more performant to connect directly to the database.

Related

Create offline OSX application from Rails web app

I was recently building an internal business database web app using Ruby on Rails 4 and Postgres. The plan was to make the app available to all our employees using Devise for authentication and hosting on Heroku.
Toward the end of development, a request was made to include some highly sensitive information in the app including social security numbers, etc. Thus, instead of heavily worrying about online security we are planning to make this app only run locally on one Mac and not have it available online. The app will only be used about 5 times a month by one person. Instead of requiring that person to launch a postgres server and a webserver via terminal (such as "rails server"), is there a way to package the entire Rails web app and database in to an application so it could be run similar to other OSX apps?
If anyone knows a PC solution instead, that is also a (less preferred) option.
At this point, we realize we should have developed a xcode app, but now that the development is mostly complete with some tricky UI, we would rather not have to recreate the functionality in xcode.
Though I haven't used it myself, what your describing sounds pretty close to what Rubymotion offers

Rails API / Ember-cli web app: what is the conventional workflow?

In the process of building a SPA, we opted for a combination of Rails API and Ember-cli.
From what we understand, the architecture of the app will be the following:
Rails API will run the back-end of the app, as an API
Ember-cli will run the front-end of the app, as a front-end MV* framework
Data will be served by Rails API to Ember-cli with JSON
What does not seem really clear though, is what should be the development workflow?
In other words, should we:
Build the back-end (rails models, etc), then build the front-end and finally connect both?
Build everything at the same time, but one feature at a time?
Go with another option?
I would recommend building both at the same time, in separate apps (that way you can test your API as an actual API rather than just a backend), but in close proximity to one another. This way you can make sure both play nicely with each other and you're getting the results you actually need, plus if something that you do on one causes an error on the other the bug will become immediately apparent.
Let me know if this answers your question enough, I can clarify/ give additional examples from here if you'd like.

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.

Linking Cocoa desktop app & RubyonRails website

I'm developing a tool that will comprise a central website and a desktop application. I've only really done entirely online developent, or entirely offline/client-side development before and not really had to link the two. I could use some help in how to approach passing data to/from the online site & desktop app.
The desktop app needs to communicate & do some control of iTunes, so initially I'm building this in Cocoa on OSX & making use of the ScriptingBridge framework.
On the website side I'm thinking of using Ruby on Rails, with data stored in a mySQL database, as I'm fairly familiar & seems like a good match for the online job it has to do. (But open to other suggestions if there's a better approach!)
I'm struggling to find the best approach to easily transfer data between the Cocoa app & the online rails database - is there a simple way of having the Cocoa app access the online database directly, or is it typical to dump some XML onto the webserver and have the app read that?
Opening a RESTful API on your RoR system is as simple as you can get. Have your desktop app communicate with that API using JSON or XML.
Advantages to using JSON rather than XML: (1) extremely simple to manipulate in Rails (2) extremely simple to work with in Javascript, should the need arise to build a web client in addition to the desktop one.
It's a very bad idea to have the desktop app communicate directly with your remote database. Two main reasons:
Security. Such a setup just begs to be hacked. Some databases are "built for the web" (CouchDB comes to mind) and would be alright here, but MySQL isn't.
Flexibility. With desktop application in the field, making changes and distributing them to all clients is hard. Should the need arise to change your schema, the web application layer frequently allows you to keep the interface with the desktop clients stable.

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