Search code examples
ruby-on-railscoinbase-api

Where is a good place to initialize an API?


I wanted to use this api: https://github.com/coinbase/coinbase-ruby and the first step is to initialize the API, like this:

coinbase = Coinbase::Client.new(ENV['COINBASE_API_KEY'], ENV['COINBASE_API_SECRET'])

I was wondering what the best place to put this code is, and how would I access it if I put it "there"? I want this variable (coinbase) to be accessible ANYWHERE in the application.

Thanks!


Solution

  • One way to go about having a global variable can be done as similar as initializing redis in a Rails application by creating an initializer in config/initializers/coinbase.rb with:

    $coinbase = Coinbase::Client.new(ENV['COINBASE_API_KEY'], ENV['COINBASE_API_SECRET'])
    

    Now, you can access $coinbase anywhere in the application!