Skip to content

Instantly share code, notes, and snippets.

@billforward-alex
Created June 22, 2017 22:53
Show Gist options
  • Save billforward-alex/b8cd74fe4aef2d646abdb2e96bf42a1a to your computer and use it in GitHub Desktop.
Save billforward-alex/b8cd74fe4aef2d646abdb2e96bf42a1a to your computer and use it in GitHub Desktop.
Tidying a bf_ruby2 configuration

Here's how to tidy away the configuration for the bf_ruby2 SDK.

Store your config in config.yml:

config:
  host: "api-sandbox.billforward.net:433"
  scheme: "https"
  base_path: "v1"
  debugging: false
client:
  access_token: "INSERT-ACCESS-TOKEN"
  logger:
    level: "DEBUG"

Make a helper class, acquire_client.rb:

module BillForwardDemo
  class Config
    attr_accessor :config
    attr_accessor :client
    attr_accessor :yam

    def initialize
      @yam = YAML.load_file('config.yml')

      @config = BillForward::Configuration.new
      @config.host = @yam['config']['host']
      @config.scheme = @yam['config']['scheme']
      @config.base_path = @yam['config']['base_path']
      @config.debugging = @yam['config']['debugging']
      @config.logger.level = Logger.const_get(@yam['client']['logger']['level'])

      access_token = @yam['client']['access_token']

      @client = BillForward::ApiClient.new @config
      @client.default_headers.merge! 'Authorization' => "Bearer #{access_token}"
    end
  end
end

Now in your code, whenever you want to access this config, you can grab it by requiring the helper class acquire_client:

my_example.rb

require_relative 'acquire_client'

config = BillForwardDemo::Config.new

sub_api = BillForward::SubscriptionsApi.new config.client
acc_api = BillForward::AccountsApi.new config.client

Note that the same client can be provided to a variety of API controllers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment