Isaac
Isaac

Reputation: 2364

Ruby on Rails - Source a file and save ENV variable as global

I have an API key which I've saved to /home/user/api/keys that I would like to save as a global variable in my Rails app. I thought this would work, this is my config/initializers/my_constants.rb:

`source "/home/user/api/keys"`
API_PASS = ENV["API_PASSWORD"]

And this is /home/user/api/keys (without the real value, obviously):

#!/bin/bash
export API_PASSWORD="--------"

The source command doesn't seem to do anything. API_PASS is just set to nil. How can I do this?

Upvotes: 0

Views: 1097

Answers (1)

Philip Hallstrom
Philip Hallstrom

Reputation: 19879

The above won't work because the backtick command starts its own shell and ends it so those variables are never included in the rest of your script.

I think you'll have to parse the file yourself and add the entries to ENV. I would look at the dotenv gem and see if you can point it at your own environment file. They've done all the hard work for you.

https://github.com/bkeepers/dotenv

Upvotes: 3

Related Questions