문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top