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 config_json[:_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 worker_should_bypass_enqueue ENV['FANFORCE_WORKER_SHOULD_BYPASS_ENQUEUE'] == 'true' end def worker_should_bypass_enqueue=(bool) if bool == true ENV['FANFORCE_WORKER_SHOULD_BYPASS_ENQUEUE'] = 'true' else ENV.delete('FANFORCE_WORKER_SHOULD_BYPASS_ENQUEUE') end end ###################################################################################################################### def config_json @config_json ||= validate_config_json(MultiJson.load(File.open("#{@base_dir}/config.json").read, symbolize_keys: true)) end def validate_config_json(config_json) config_json.is_a?(Hash) || (raise "#{@base_dir}/config.json must contain a json object (not a #{config_json.class})") config_json[:_id].present? || (raise "#{@base_dir}/config.json must include an _id field") return config_json 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_global_paths $: << @base_dir if File.directory?("#{@base_dir}") $: << File.join(@base_dir,'lib') if File.directory?("#{@base_dir}/lib") end def load_routes load_global_paths require "#{factory_dir}/sinatra/route_helpers/base" require "#{factory_dir}/sinatra/route_helpers/com_database" require "#{factory_dir}/sinatra/route_helpers/com_campaign" require "#{factory_dir}/sinatra/route_helpers/com_identifier" require "#{factory_dir}/sinatra/route_helpers/com_widget" require "#{factory_dir}/sinatra/route_helpers/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