Billy Coover
Billy Coover

Reputation: 3837

uninitialized constant APN::App::RAILS_ENV

I'm struggling with this error after I fire the command rake apn:notifications:deliver on the APN on Rails gem that I've installed.

It's barking about the RAILS_ENV variable. I've tried a couple of forks that change RAILS_ENV to Rails.env but I still get the same error. I've posted my issue over on that repo hoping I might get somewhere.

I don't know enough about rails to dig in any further. I'm hoping someone could point me in the right direction so that I can a) better understand what went wrong and b) fix the problem.

I am using bundler and I'm pointing my gem file to the git repo: rake apn:notifications:deliver

air:apnapp azcoov$ rake apn:notifications:deliver --trace
    /Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/version.rb:2: warning: already initialized constant VERSION
    ** Invoke apn:notifications:deliver (first_time)
    ** Invoke environment (first_time)
    ** Execute environment
    ** Execute apn:notifications:deliver
    rake aborted!
    uninitialized constant APN::App::RAILS_ENV
    /Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:11:in `cert'
    /Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:22:in `send_notifications'
    /Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:32:in `send_notifications'
    /Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:31:in `each'
    /Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:31:in `send_notifications'
    /Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/rails/../tasks/apn.rake:7
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `execute'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `invoke_with_call_chain'
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `run'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
    /Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/bin/rake:33
    /usr/bin/rake:19:in `load'
    /usr/bin/rake:19
    Tasks: TOP => apn:notifications:deliver

Upvotes: 2

Views: 2260

Answers (1)

Nickolay Stanev
Nickolay Stanev

Reputation: 168

Which version of Rails to you use? I just ran into this issue too. My Rails knowledge is not very deep, but it seems like the problem is deprecation of RAILS_* environment variables. I use Rails 3.1.0 and I do NOT have:

RAILS_ROOT/ENV/blah

in my environment. Instead, I have:

Rails.root/env/blah

When I replace RAILS_ENV with Rails.env in app/models/apn/app.rb the problem goes away.

So, is this gem compatible with Rails 3.1.0??? It seems not. Is there a workaround that will allow its use in a Production environment? Maybe define somewhere:

APN::App::RAILS_ENV = Rails.env

But where?

UPDATE:

I got it working with a workaround described by garyfoster here: https://github.com/PRX/apn_on_rails/issues/53.

To summarize, three things:

1) Add this ghetto hack to your config/environment.rb file:

APN::App::RAILS_ENV = Rails.env

2) Insert your certs in the APN::App table. I wrote a one-time rake task for that:

task :init_apn_certs => [:environment] do # NOTE: One-time task...
  print "Creating APN::App with certs..."
  app = APN::App.create(:apn_dev_cert => Rails.root.join('config', 'apple_push_notification_development.pem').read,
                        :apn_prod_cert => Rails.root.join('config', 'apple_push_notification_production.pem').read)
  puts (app.valid? ? "done" : "failed")
end

3) Write your own rake task that bypasses the APN::App.send_notifications method, as that barfs with another error (something about '/config/apple_push_notification_development.pem' not existing). My code for it:

task :deliver_notifications => [:environment] do
  print "Delivering APNs for app ids..."
  apps = APN::App.all
  apps.each do |app|
    app.send_notifications
    print app.id.to_s + "..."
  end
  puts "done."
end

Or, if you have only one app, you can just do this and be done with it:

task :deliver_notifications => [:environment] do
  APN::App.first.send_notifications
end

DISCLAIMER: I'm yet to test this on Staging and Production (will do so by the end of the week), but I see no reason why it shouldn't work.

Upvotes: 2

Related Questions