简体   繁体   中英

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!

The answer to this question really depends on your use case and your approach. My geral recommendation, however, is to create a Service Object (in the DDD sense) (see the section named "Domain Objects Should Not Know Anything About Infrastructure Underneath" in that link), that handles all communication with the Coinbase API. And then, within this service object, you can simply initialize the Coinbase::Client object once for however many times you call into it. Here's an example:

# app/services/coinbase_service.rb
class CoinbaseService
  cattr_reader :coinbase_client, instance_accessor: false do
    Coinbase::Client.new(ENV['COINBASE_API_KEY'], ENV['COINBASE_API_SECRET'])
  end

  def self.do_something
    coinbase_client.do_something_in_their_api
  end

  def self.do_something_else
    coinbase_client.do_something_else_in_their_api
  end
end

So then you might do, eg:

# From MyController#action_1
if CoinbaseService.do_something
  # ...
else
  # ...
end

Or:

# From MyModel
def do_something
  CoinbaseService.do_something_else
end

To get the service object working, you may need to add app/services to your autoload paths in application.rb file. I normally just add this:

# config/application.rb
config.autoload_paths += %W(#{config.root}/app)

I find this Service Object approach to be very beneficial organizationally, more efficient (only 1 invocation of the new Coinbase client needed), easier to test (easy to mock-out calls to Coinbase::Client ), and simply joyful :).

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!

In the file config/initializers/coinbase.rb

Rails.application.config.after_initialize do
  CoinbaseClient = Coinbase::Client.new(
    Rails.application.credentials.coinbase[:api_key],
    Rails.application.credentials.coinbase[:api_secret])
end

In place of the encrypted credentials, you could also use environment variables: ENV['COINBASE_API_KEY'], ENV['COINBASE_API_SECRET']

The above will make the constant CoinbaseClient available everywhere in your app. It will also ensure all your gems are loaded before the client is initialized.

Note: I am using Rails 6.1.4.4, and Ruby 2.7.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM