lib/flexmls_api/configuration/yaml.rb in flexmls_api-0.7.0 vs lib/flexmls_api/configuration/yaml.rb in flexmls_api-0.7.3

- old
+ new

@@ -1,37 +1,24 @@ require 'erb' + module FlexmlsApi module Configuration class YamlConfig KEY_CONFIGURATIONS = VALID_OPTION_KEYS + [:oauth2] + OAUTH2_KEYS - + DEFAULT_OAUTH2_PROVIDER = "FlexmlsApi::Authentication::OAuth2Impl::PasswordProvider" attr_accessor *KEY_CONFIGURATIONS - attr_reader :client_keys, :oauth2_keys + attr_reader :client_keys, :oauth2_keys, :provider def initialize(filename=nil) - @client_keys = {} - @oauth2_keys = {} @oauth2 = false load_file(filename) unless filename.nil?() end def load_file(file) - @client_keys = {} - @oauth2_keys = {} @file = file @name = File.basename(file, ".yml") config = YAML.load(ERB.new(File.read(file)).result)[api_env] - config.each do |key,val| - sym = key.to_sym - if KEY_CONFIGURATIONS.include? sym - self.send("#{sym}=", val) - if VALID_OPTION_KEYS.include?(sym) - @client_keys[sym] = val - elsif OAUTH2_KEYS.include?(sym) - @oauth2_keys[sym] = val - end - end - end + config["oauth2"] == true ? load_oauth2(config) : load_api_auth(config) rescue => e FlexmlsApi.logger().error("Unable to load config file #{file}[#{api_env}]") raise e end @@ -52,11 +39,11 @@ return current_env end # Used to specify the root of where to look for flexmlsApi config files def self.config_path - "config/flexmls_api" + path_prefix + "config/flexmls_api" end def self.config_keys() files = Dir["#{config_path}/*.yml"] files.map {|f| File.basename(f,".yml") } @@ -72,10 +59,43 @@ protected def env ENV end - + + private + def load_api_auth(config={}) + @client_keys = {} + @oauth2_keys = {} + config.each do |key,val| + sym = key.to_sym + if VALID_OPTION_KEYS.include?(sym) + self.send("#{sym}=", val) + @client_keys[sym] = val + end + end + end + def load_oauth2(config={}) + @oauth2_provider = DEFAULT_OAUTH2_PROVIDER + @client_keys = {:oauth2_provider => @oauth2_provider } + @oauth2_keys = {} + @oauth2 = true + config.each do |key,val| + sym = key.to_sym + if VALID_OPTION_KEYS.include?(sym) + self.send("#{sym}=", val) + @client_keys[sym] = val + elsif OAUTH2_KEYS.include? sym + self.send("#{sym}=", val) + @oauth2_keys[sym] = val + end + end + end + # In a rails app, default to the rails root, regardless of where that may be + def self.path_prefix + "#{Rails.root}/" + rescue => e + "" + end end end end -