class Fanforce::Plugin::CoreConfig attr_accessor :bugsnag attr_reader :show_error_details, :is_bugsnag_enabled attr_writer :redis_url def initialize(base_dir) @base_dir = base_dir @show_error_details = (ENV['RACK_ENV']=='development') ? true : false @bugsnag = (ENV['BUGSNAG_API_KEY'].present?) ? {api_key: ENV['BUGSNAG_API_KEY']} : nil end def _id raw_config[:_id] || (raise 'base_dir is missing') end def base_dir @base_dir || (raise 'base_dir is missing') end def factory_dir Fanforce::PluginFactory.base_dir || (raise 'factory_dir is missing') end def show_error_details=(opt) @show_error_details = ['1','text'].include?(opt.to_s) ? opt.to_s : false end def redis_url @redis_url || ENV['REDIS_URL'] || 'redis://localhost:6379' end ###################################################################################################################### def raw_config @raw_config ||= validate_config_file(MultiJson.load(File.open("#{@base_dir}/config.json").read, symbolize_keys: true)) end def validate_config_file(raw_config) raw_config.is_a?(Hash) || (raise "#{@base_dir}/config.json must contain a json object (not a #{raw_config.class})") raw_config[:_id].present? || (raise '#{@base_dir}/config.json must include an _id field') return raw_config end ###################################################################################################################### def load_bugsnag return if !@bugsnag.is_a?(Hash) or ENV['RACK_ENV'] == 'test' require 'bugsnag' Bugsnag.configure do |config| config.api_key = ENV['BUGSNAG_API_KEY'] || @bugsnag[:api_key] config.notify_release_stages = @bugsnag[:notify_release_stages] || ['development','staging','production'] config.release_stage = @bugsnag[:release_stage] || ENV['RACK_ENV'] config.app_version = @bugsnag[:app_version] || "fanforce-plugin-factory-#{Fanforce::PluginFactory::VERSION}" config.project_root = @base_dir end @is_bugsnag_enabled = true end def load_routes $: << File.join(@base_dir,'lib') if File.directory?("#{@base_dir}/lib") $: << @base_dir if File.directory?("#{@base_dir}") require "#{factory_dir}/sinatra/helper_routes/com_database" require "#{factory_dir}/sinatra/helper_routes/com_campaign" require "#{factory_dir}/sinatra/helper_routes/com_identifier" require "#{factory_dir}/sinatra/helper_routes/com_js_widget" require "#{factory_dir}/sinatra/helper_routes/plugin" Dir["#{@base_dir}/initializers/*.rb"].each {|f| require f } if File.directory?("#{@base_dir}/initializers") require "#{factory_dir}/Routes" require "#{@base_dir}/Routes" end def load_env if (!ENV['RACK_ENV'] or ENV['RACK_ENV'] == 'development') load_env_from_file("#{@base_dir}/.powenv") if File.exists?("#{@base_dir}/.powenv") load_env_from_file("#{@base_dir}/.pluginenv") if File.exists?("#{@base_dir}/.pluginenv") end end def load_env_from_file(file) File.open(file, 'rb') do |file| contents = file.read lines = contents.gsub('export ', '').split(/\n\r?/).reject{|line| line.blank?} lines.each do |line| keyValue = line.split("=", 2) next unless keyValue.count == 2 ENV[keyValue.first] = keyValue.last.gsub("'",'').gsub('"','') end end end end