Cloud Foundry Rails - Multi-tenancy based on subdomain - ruby-on-rails

My rails application currently works as follows:
In the database, there are tenants, also known as 'companies'. Everything in the database belongs to one of these companies, and so have a company_id attribute in order to separate them.
My application determines which company to use based on the subdomain in the link inputted by the user, so for example:
apple.myapplicationsnamehere.com
Should look at the companies in my database, and find out which one is called "apple", then separate all of the data by apples ID.
This works fine on the localhost (if you're wondering how this is possible, I used a redirect to my localhost that I found online - http://apple.lvh.me:3000/)
However, this does not work on Cloud Foundry.
I have tried to use vmc's map function to create the Url: apple.myapplicationsnamehere.cloudfoundry.com, but this does not work, and simply appending a subdomain to my application doesn't work either.
Does anyone know of any way around this? Or perhaps I'm missing something? The database was seeded with caldecott already, so the companies certainly exist in the database.

You should be able to run a:
vmc map myapplicationnamehere apple.myapplicationsnamehere.com
vmc map myapplicationnamehere orange.myapplicationsnamehere.com
vmc map myapplicationnamehere pear.myapplicationsnamehere.com
Though i don't believe you can setup a wildcard match, so you'll need to run a vmc command each time a new company is added into your environment.

Related

Ruby on Rails. Using Google Client API to parse emails

I am new to Ruby and have a question about how to do a specific task on Rails.
I have a list of inventory and each item has a specific Stock ID that is emailed to my personal Gmail account. I want my web application to listen for emails from a specific email account. When my gmail receives an email from that specific account I want my application to parse it for a couple of fields and insert the stock ID into my database.
For example:
Let's say my database has an item with style code: A5U31 and size:10.
The email will say something like item with style code: A5U31 and size:10 has Stock ID:329193020.
I want my Rails application to search the database for an entry with that specific style code and size, and when it finds an entry to simply insert the stock ID into the row.
I am trying to using the Google-API-Client gem to this, but I am struggling, because I am still a beginner. So far I have done this quick-start guide to authenticate my gmail account with my rails app.
https://developers.google.com/gmail/api/quickstart/ruby?authuser=2
If someone could help me figure out how to write this sort of code and where to put it in my app(models, controllers, views) that would be very appreciated. Thanks!
I know it's been several months since you posted this, so you probably already have it worked out, but in case you still need a solution (or if someone else wants to do something similar), I'll share my thoughts:
At a high level, it sounds like your plan is
Identify when a new email has come in (either by polling or by using a push notification).
Grab the new email's content.
Parse the email's content in order to extract relevant data.
Use the data to query and update a database.
Based on the documentation for the Gmail API, it does look like you should be able to set up push notifications, so you won't have to poll the endpoint to get the information you need.
However, my big takeaway from this list is that none of the items on it really require Rails, since you're not exposing an external web API for requests. I suppose that you could leverage ActiveRecord to create an item model and use that to manage the database; however, since it seems like you'd only need to make some basic SQL queries (and the same ones each time), I'm not sure that bringing in ActiveRecord adds much value.
If I were trying to solve this problem myself, I would probably create a simple Ruby program that (a) uses the gem you mentioned to handle push notifications from the Gmail API, and (b) uses another gem to connect to whatever kind of database you're using (e.g. pg for Postgres) and make the necessary queries.
(All of this assumes, of course, that you aren't specifically using Rails for some other reason, e.g. adding this feature to an existing Rails application).

How to configure custom domain for links in Branch.io

How do I set up the custom subdomain for my branch links? Currently I have pointed the cname records to xxxx.app.link and xxxx.test-app.link (I have added 2 CNAME records)
I get the following error when I try to add the sudomain in the dashboard – “Domain not properly configured; please CNAME your domain to ‘custom.bnc.lt’. If you’ve already done this, note that it can take up to several hours to propagate”
Do I have to point the subdoiman to “custom.bnc.lt” If yes, then how will the live and test environment urls be different? Do I have to setup 2 different CNAME records - one for the live version and one for the test version?
EDIT:
It has been several hours since I set up the CNAME records. A simple dig <my custom subdomain> gives the correct result.
Alex from Branch here: You should use custom.bnc.lt as the CNAME for your subdomain(s). The Live and Test environments can use the same URL or can be set separately — completely up to you, since our backend takes care of all the routing details. Our guide for this is here, which you might find useful for reference.

Preventing Rails from connecting to database during initialization

I am quite new at Ruby/Rails. I am building a service that make an API available to users and ends up with some files created in the local filesystem, without any need to connect to any database. Then, once every few hours, I want to run a piece of ruby code that takes these local files, uploads them to Amazon S3 and registers their location into a Postgres database.
Right now both codes live together in the same project. I am observing that every time a user does something the system connects to the database. I have seen this answer which recommends to eliminate all traces of ActiveRecord in my code, but given that I want to have my background bookkeeping process connect to the database I am stuck on what to do.
Is it possible to define two different profiles (one with database and one without) and specify which profile a certain function call should run on? would this work?
I'm a bit confused by this, the db does not magically connect to the database for kicks on every request, it does so because of a specific request requires it. Generally through ActiveRecord but not exclusively
If your system is connecting every time you make a request, then that implies you have some sort of user metric or authorisation based code in there. Just killing off the database will cause this to fail, and likely you'll have to find it anyways, to then get your system to work. I'd advise locating it.
Things to look for are before_filters in controllers, or database session management, for example, or look for what is in the logs - the query should appear - and that will tell you what is being loaded, modified or whatnot.
It might even work to stop your database, just before doing a user activity, and see where the error leads you. Rinse and repeat until the user activity works, without the database.

How to create a multi-domain app in Rails 4

I want to create a multi-domain app with Rails and have been wondering about the best approach.
The app will have a set of functionality that will be the same functionality across all domains, so the change in domain will only mean a change in the db used and the styling (and content obviously) but the models and controller will be the same.
The app will use MongoDB as the main storage (Redis for workers and other stuff that not directly relevant to model storage)
My idea is: Once a user signs up, a new record will be created but also a database will be created with the user id. The user model will store the database id (recently created), and the domain the user have just added. A vhost file would be created and the server reloaded.
Once the user visits the app (using the new domain). the app would check the url and find the correct db to use based on url. This would be saved into variable to be used throughout the app.
I imagine have this check to happen on each request.
I would appreciate thoughts and comments on this approach and best way to achieve this.

Rails - hide database credentials

Say one is in need to create Ruby On Rails application using third party's database without the possibility to see/get the database credentials.
Above may seem pretty crazy, but I am in this exact situation right now. The application will run on my server (only I have physical/remote access), the database will be theirs (third party company) and I cannot have access to it.
Any way of encrypting the database.yml file is pointless since I can do the following:
config = Rails.configuration.database_configuration
username = config[Rails.env]["username"]
password = config[Rails.env]["password"]
I desperately need a way to achieve given goals, and I hope it is possible. Any idea will be greatly appreciated.
Well you can always run a rails console and do whatever you want on their database :/ I don't really see the point in this.
They can create a new user on the database and give you it's credentials and then limit what you can see and do on database level. You can read how to do it for PostgreSQL here.

Resources