Commit 62e550be authored by sergio1990's avatar sergio1990

Refactored loading config

Closed #23
parent deb49b0a
......@@ -20,6 +20,12 @@ module BetfairApiNgRails
@formatter = BetfairApiNgRails::Api::FormatterFactory.initialize_formatter value
end
def load_for_environment(file_path, env)
require 'yaml'
load_config = YAML.load_file(file_path)[env]
Api::Constants::LOADABLE_CONFIG_OPTIONS.each { |opt| self.send("#{opt}=", load_config[opt.to_s]) }
end
end
end
......
......@@ -46,6 +46,15 @@ module BetfairApiNgRails
"listVenues"
]
LOADABLE_CONFIG_OPTIONS = [
:application_key,
:login_url,
:api_url,
:keep_alive_url,
:username,
:password
]
end
end
end
\ No newline at end of file
BetfairApiNgRails.config do |config|
require 'yaml'
config.load_for_environment "#{Rails.root}/config/betfair_api_ng_rails.yml", Rails.env
load_config = YAML.load_file("#{Rails.root}/config/betfair_api_ng_rails.yml")[Rails.env]
config.application_key = load_config['application_key']
config.ssl_key_filepath = "#{Rails.root}/public/client-2048.key"
config.ssl_crt_filepath = "#{Rails.root}/public/client-2048.crt"
config.login_url = load_config['login_url']
config.api_url = load_config['api_url']
config.keep_alive_url = load_config['keep_alive_url']
config.username = load_config['username']
config.password = load_config['password']
config.locale = :ru
config.formatter = nil #out of box available formatter for JsTree
config.formatter = nil
config.keep_alive_session = true
end
\ No newline at end of file
require 'spec_helper'
describe BetfairApiNgRails::Api::Config do
subject(:object) { described_class }
describe ".formatter=" do
let(:formatter) { double(:formatter) }
let(:value) { double(:value) }
it "inits formatter by passed value" do
expect(BetfairApiNgRails::Api::FormatterFactory).to receive(:initialize_formatter)
.with(value)
.and_return formatter
object.formatter = value
expect(object.instance_variable_get(:@formatter)).to eq formatter
end
end
describe ".load_for_environment" do
let(:path) { "path" }
let(:opts) { [:opt1] }
let(:config) { {'env' => { 'opt1' => :val1 }} }
before do
stub_const("BetfairApiNgRails::Api::Constants::LOADABLE_CONFIG_OPTIONS", opts)
allow(YAML).to receive(:load_file).with(path).and_return config
end
it "loads config from file and apply it" do
expect(object).to receive('opt1=').with(:val1)
object.load_for_environment path, 'env'
end
end
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment