# # Copyright (C) 2007 Mobio Networks, Inc. # # This program is free software: you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation, either version 3 of the License, or (at your option) any later # version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. # # You should have received a copy of the GNU General Public License along with # this program. If not, see . # # # This module manages yaml-based configuration sets. Uses some logic from the # rfacebook gem. # # The config_manager is a yaml-based property management system which follows # the same methodology for managing property sets as database.yml. To use the # config_manager, perform the following steps: # # 1. In environment.rb, add the following two lines to the bottom of the file # (we want to load the configuration statically): # #
require 'rmobio/config_manager'
# # 2. Run the following rake task to generate the boilerplate configs: # #
rake rmobio:setup
# # 3. Edit config/rmobio.yml and add your properties (here we set a property # called "ad_network" with a value of "ad_sense") # #
#    development:
#       ad_network: ad_sense
# 
# # 4. To use a property, reference the hash accordingly. e.g. @adnetwork = # MOBIO_CONFIG['ad_network'] # module Rmobio module ConfigManager EXTERNAL_CONTEXT_PROP = '_external_context' unless defined? EXTERNAL_CONTEXT_PROP if defined? RAILS_DEFAULT_LOGGER logger = RAILS_DEFAULT_LOGGER else logger = nil end begin app_yaml_file = YAML.load_file("#{RAILS_ROOT}/config/rmobio.yml") rescue Exception => e raise StandardError, "config/rmobio.yml could not be loaded." end if app_yaml_file if app_yaml_file[RAILS_ENV] remove_const :MOBIO_CONFIG if defined? MOBIO_CONFIG MOBIO_CONFIG = app_yaml_file[RAILS_ENV] # Replace all properties containing @somekey@ with the value of # "somekey" MOBIO_CONFIG.each do |key, value| # Run the value filter process filter_value = value.to_s.strip filter_key = /@(.*)@/.match(filter_value).to_s.strip if filter_key and filter_key != '' filter_key = filter_key.gsub(/@/,'') if filter_key and filter_value and filter_key != '' filter_value = filter_value.gsub(/@.*@/, MOBIO_CONFIG[filter_key.strip]) logger.debug('ConfigManager: filtered key: ' + key + ' filtered value: ' + filter_value) unless logger.nil? MOBIO_CONFIG[key] = filter_value end end end else raise StandardError, "config/rmobio.yml exists, but doesn't" + "have a configuration for RAILS_ENV=#{RAILS_ENV}." end else raise StandardError, "config/rmobio.yml does not exist." end # get_request_context This method checks for the existence of a # "mobio-context" header. If the header exists, then the method returns the # filtered value for the MOBIO_CONFIG array with the key "url." # # Below is the flow: # # A request is made to the server which contains a mobio-context header with # a value of "mycustomcontext" to "/news" # # Calling get_request_context(some_url_property) would return # # "/mycustomcontext/news" # # If no header exists, the method returns the value of the property, # unfiltered # def get_external_context(url=nil) context = request.env['HTTP_MOBIO_CONTEXT'] if url if context and context != '' external_context = MOBIO_CONFIG[url].gsub(EXTERNAL_CONTEXT_PROP, context) RAILS_DEFAULT_LOGGER.debug 'ConfigManager: Setting the uri to: "' + external_context + '" for the current request.' unless not defined? RAILS_DEFAULT_LOGGER return external_context else return MOBIO_CONFIG[url].gsub('/' + EXTERNAL_CONTEXT_PROP, '') end end end end end # Now we want to initialize the MOBIO_CONFIG constant include Rmobio::ConfigManager