Domanda

I have a variable assigned like this:

cryptsy = Cryptsy::API::Client.new(key, secret)

How can I make this variable accessible through out the application?

Currently its in the Application controller, but I am using GRAPE gem to create a simple api. I need to be able to access the variable in /app/api/v1/bla.rb and other files in the v1 folder.

È stato utile?

Soluzione

You can create an initializer to instantiate on application load. In config/application.rb do something like

require 'cryptsy/api'

module YourApp 
  Cryptsy = Cryptsy::API::Client.new(key, secret)
  ...
end

This will give you a universally accessible instance of this object.

Altri suggerimenti

How about creating a class in lib and adding it there. Make sure to add lib to autoload path in config/application.rb

class Cryptsy
  def self.client
    @cryptsy ||= Cryptsy::API::Client.new(key, secret)
  end
end

And whereever you want to use it, you can call

Cryptsy.client

you can add the following code to the file app_root/config/initializers/cryptsy.rb

require 'cryptsy-api' # this line might be needed
$cryptsy = Cryptsy::API::Client.new(key, secret)

then you can access $cryptsy through out the app.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top