I am playing with the heroku api in Rails and have come across a potential issue.
After submitting a login form i am instantiating the heroku object.
heroku = Heroku::API.new(:username => USERNAME, :password => PASSWORD)
I would then like to use the heroku object in all controllers to further querying the api. I have tried @heroku, @@heroku and $heroku, but none work. Is this possible?
The only solution i have found it to use the api to fetch the users api key, store this in a session then use it to re-instantiate the heroku object within each controller method. Is this the best / only solution?
In general, a
before_filtercould solve your re-instantiating problem. If you want to set an instance variable that is available to every controller method, do something like this:You can also user before_filters in the application controller to set instance variables that are accessible in all of your controllers. Read about filters here.
As for storing the API keys to the session, that works, but if you want long term access, you might want to write the API keys to the database. Combined with before filters, you could do something like this: