diff --git a/lib/washout_builder.rb b/lib/washout_builder.rb index be7cba1..2a8ce1f 100644 --- a/lib/washout_builder.rb +++ b/lib/washout_builder.rb @@ -2,16 +2,19 @@ require 'active_support/core_ext/object/blank' require 'active_support/core_ext/hash/keys' require 'active_support/concern' -require 'active_support/core_ext/string/output_safety.rb' +require 'active_support/core_ext/string/output_safety' require 'active_support/ordered_options' +require 'active_support/core_ext/string/starts_ends_with' Gem.find_files('washout_builder/**/*.rb').each { |path| require path } ActionDispatch::Routing::Mapper.class_eval do alias_method :original_wash_out, :wash_out - # Adds the routes for a SOAP endpoint at +controller+. - def wash_out(controller_name, options={}) + # Adds the routes for a SOAP endpoint at +controller+. + def wash_out(controller_name, options={}) + env_checker = WashoutBuilder::EnvChecker.new(Rails.application) + if env_checker.available_for_env?(Rails.env) options = options.symbolize_keys if options.is_a?(Hash) if @scope scope_frame = @scope.respond_to?(:frame) ? @scope.frame : @scope @@ -24,12 +27,12 @@ def wash_out(controller_name, options={}) end match "#{controller_name}/soap_doc" => WashoutBuilder::Engine, via: :get, - defaults: { name: "#{controller_class_name}" }, - format: false, - as: "#{controller_class_name}_soap_doc" - - original_wash_out(controller_name, options) + defaults: { name: "#{controller_class_name}" }, + format: false, + as: "#{controller_class_name}_soap_doc" end + original_wash_out(controller_name, options) + end end # finds all the exception class and extends them by including the ExceptionModel module in order to be # able to generate documentation for exceptions diff --git a/lib/washout_builder/engine.rb b/lib/washout_builder/engine.rb index 90aaebc..229214d 100644 --- a/lib/washout_builder/engine.rb +++ b/lib/washout_builder/engine.rb @@ -1,19 +1,14 @@ +require_relative './env_checker' module WashoutBuilder # the engine that is used to mount inside the rails application class Engine < ::Rails::Engine isolate_namespace WashoutBuilder config.washout_builder = ActiveSupport::OrderedOptions.new initializer 'washout_builder.configuration' do |app| - blacklisted_envs = app.config.washout_builder[:blacklisted_envs] - blacklisted_envs = blacklisted_envs.is_a?(Array) ? blacklisted_envs : [blacklisted_envs].compact - whitelisted_envs = app.config.washout_builder[:whitelisted_envs] - whitelisted_envs = whitelisted_envs.is_a?(Array) ? whitelisted_envs : [whitelisted_envs].compact mounted_path = app.config.washout_builder[:mounted_path] - if (whitelisted_envs.present? || blacklisted_envs.present?) && whitelisted_envs.find{|a| blacklisted_envs.include?(a) }.blank? - if whitelisted_envs.include?('*') || (!blacklisted_envs.include?(Rails.env) || whitelisted_envs.include?(Rails.env)) - app.routes.append do - mount WashoutBuilder::Engine => mounted_path if mounted_path.is_a?(String) && mounted_path.starts_with?('/') - end + if WashoutBuilder::EnvChecker.new(app).available_for_env?(Rails.env) + app.routes.append do + mount WashoutBuilder::Engine => mounted_path if mounted_path.is_a?(String) && mounted_path.starts_with?('/') end end end diff --git a/lib/washout_builder/env_checker.rb b/lib/washout_builder/env_checker.rb new file mode 100644 index 0000000..c16d57b --- /dev/null +++ b/lib/washout_builder/env_checker.rb @@ -0,0 +1,50 @@ +module WashoutBuilder + class EnvChecker + + attr_reader :whitelist, :blacklist + attr_writer :whitelist, :blacklist + + def initialize(app) + self.whitelist = get_valid_data(app.config.washout_builder[:whitelisted_envs]) + self.blacklist = get_valid_data(app.config.washout_builder[:blacklisted_envs]) + end + + def available_for_env?(env_name) + if (whitelist.present? || blacklist.present?) + if whitelist.find{|a| blacklist.include?(a) }.blank? + if whitelist.include?('*') || (!valid_for_env?(blacklist, env_name) && valid_for_env?(whitelist, env_name)) + return true + end + end + else + return true + end + return false + end + + + private + + def valid_for_env?(list, env_name) + try_find_suitable_env(list, env_name).present? + end + + def get_valid_data(list) + list.is_a?(Array) ? list : [list].compact + end + + def try_find_suitable_env(list, env_name) + return if list.blank? + # The keys of the map can be strings or regular expressions that are + # matched against the env name and returns the found value + list.find do |env_pattern| + if env_pattern.is_a? Regexp + env_pattern.match env_name + elsif env_pattern.is_a? String + env_pattern == env_name + end + end + end + + end +end diff --git a/lib/washout_builder/version.rb b/lib/washout_builder/version.rb index 252502c..cb8ee8b 100644 --- a/lib/washout_builder/version.rb +++ b/lib/washout_builder/version.rb @@ -12,7 +12,7 @@ module VERSION # the minor version of the gem MINOR = 5 # the tiny version of the gem - TINY = 0 + TINY = 1 # if the version should be a e PRE = nil