Last Updated: February 25, 2016
·
4.216K
· wilhelmbot

Environment variables in your RubyMotion project

As any rubyist know, ENV lets you read variables from your local environment. This works pretty well for web projects, but you can't rely on this for a RubyMotion project, since the final product will be placed in a device without access to this data.

An easy solution could be storing your ENV vars inside the app's configuration plist on build time:

Rakefile

app.info_plist['ID'] = ENV['ID']
app.info_plist['SECRET'] = ENV['SECRET']

Reference: http://www.rubymotion.com/developer-center/guides/project-management/#_advanced_info_plist_settings

... and accessing them in your code through the mainBundle:

your_class.rb

id = NSBundle.mainBundle.objectForInfoDictionaryKey('ID')

3 Responses
Add your response

Adding the data to your info.plist will expose the data in the final application bundle instead of compiling the data into the binary. any way to get around this?

over 1 year ago ·

haven't thought of that

what about removing the file from the repo (and ignoring it in your SCM)?

over 1 year ago ·

You could generate a "secret.rb" file at compile time that isn't checked into git. Add something like this to your Rakefile

task :'build:device' => 'secret'
task :'build:simulator' => 'secret'

task :secret do
if ENV['SECRETAPPLICATIONID'].nil? || ENV['SECRETAPIKEY'].nil?
raise 'Need to set the env variables "SECRETAPPLICATIONID" and "SECRETAPIKEY"'
end

File.open('app/secrets.rb', 'w') do |f|
f.write <<-FILE
SECRETAPPLICATIONID = '#{ENV['SECRETAPPLICATIONID']}'
SECRETAPIKEY = '#{ENV['SECRETAPIKEY' ]}'
FILE
end
end

over 1 year ago ·