From f7636f5ed6dbc36cb703966c0664fbf92efad3bd Mon Sep 17 00:00:00 2001 From: Rada Bogdan Raul Date: Tue, 21 Jun 2016 13:14:11 +0300 Subject: [PATCH 1/3] allow regexs to be used when checking availability for envs --- lib/washout_builder/engine.rb | 17 ++++++------ lib/washout_builder/env_checker.rb | 44 ++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 lib/washout_builder/env_checker.rb diff --git a/lib/washout_builder/engine.rb b/lib/washout_builder/engine.rb index 90aaebc..7b74c91 100644 --- a/lib/washout_builder/engine.rb +++ b/lib/washout_builder/engine.rb @@ -1,19 +1,18 @@ +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 + env_checker = WashoutBuilder::EnvChecker.new( + app.config.washout_builder[:whitelisted_envs], + app.config.washout_builder[:blacklisted_envs] + ) 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 env_checker.enabled_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..397bfe3 --- /dev/null +++ b/lib/washout_builder/env_checker.rb @@ -0,0 +1,44 @@ +module WashoutBuilder + class EnvChecker + + attr_reader :whitelist, :blacklist + + def initialize(whitelist, blacklist) + self.whitelist = get_valid_data(whitelist) + self.blacklist = get_valid_data(blacklist) + end + + def enabled_for_env?(env_name) + if (whitelist.present? || blacklist.present?) && 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 + return false + end + + + private + + def valid_for_env?(env_name) + try_find_suitable_env(env_name).present? + end + + def get_valid_data(list) + list.is_a?(Array) ? list : [list].compact + end + + def try_find_suitable_env(env_name) + # The keys of the map can be strings or regular expressions that are + # matched against the env name and returns the found value + env_list.find do |host_pattern| + if host_pattern.is_a? Regexp + host_pattern.match env_name + elsif host_pattern.is_a? String + host_pattern == env_name + end + end.try(:last) + end + + end +end From 9f78c4587edb7a71e41fd152ac2c7bd2226e851d Mon Sep 17 00:00:00 2001 From: Rada Bogdan Raul Date: Tue, 21 Jun 2016 13:14:45 +0300 Subject: [PATCH 2/3] allow regexs to be used when checking availability for envs --- lib/washout_builder/engine.rb | 2 +- lib/washout_builder/env_checker.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/washout_builder/engine.rb b/lib/washout_builder/engine.rb index 7b74c91..d44bb5c 100644 --- a/lib/washout_builder/engine.rb +++ b/lib/washout_builder/engine.rb @@ -10,7 +10,7 @@ class Engine < ::Rails::Engine app.config.washout_builder[:blacklisted_envs] ) mounted_path = app.config.washout_builder[:mounted_path] - if env_checker.enabled_for_env?(Rails.env) + if env_checker.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 diff --git a/lib/washout_builder/env_checker.rb b/lib/washout_builder/env_checker.rb index 397bfe3..cbc3a22 100644 --- a/lib/washout_builder/env_checker.rb +++ b/lib/washout_builder/env_checker.rb @@ -8,7 +8,7 @@ def initialize(whitelist, blacklist) self.blacklist = get_valid_data(blacklist) end - def enabled_for_env?(env_name) + def available_for_env?(env_name) if (whitelist.present? || blacklist.present?) && whitelist.find{|a| blacklist.include?(a) }.blank? if whitelist.include?('*') || (!valid_for_env?(blacklist, env_name) || valid_for_env?(whitelist, env_name)) return true From 6b6a1b920739727c1b5428971c40db0e4f1a464f Mon Sep 17 00:00:00 2001 From: Rada Bogdan Raul Date: Tue, 21 Jun 2016 13:45:17 +0300 Subject: [PATCH 3/3] allow regexs to be used when checking availability for envs --- lib/washout_builder.rb | 19 +++++++++------- lib/washout_builder/engine.rb | 6 +---- lib/washout_builder/env_checker.rb | 36 +++++++++++++++++------------- lib/washout_builder/version.rb | 2 +- 4 files changed, 34 insertions(+), 29 deletions(-) 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 d44bb5c..229214d 100644 --- a/lib/washout_builder/engine.rb +++ b/lib/washout_builder/engine.rb @@ -5,12 +5,8 @@ class Engine < ::Rails::Engine isolate_namespace WashoutBuilder config.washout_builder = ActiveSupport::OrderedOptions.new initializer 'washout_builder.configuration' do |app| - env_checker = WashoutBuilder::EnvChecker.new( - app.config.washout_builder[:whitelisted_envs], - app.config.washout_builder[:blacklisted_envs] - ) mounted_path = app.config.washout_builder[:mounted_path] - if env_checker.available_for_env?(Rails.env) + 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 diff --git a/lib/washout_builder/env_checker.rb b/lib/washout_builder/env_checker.rb index cbc3a22..c16d57b 100644 --- a/lib/washout_builder/env_checker.rb +++ b/lib/washout_builder/env_checker.rb @@ -2,17 +2,22 @@ module WashoutBuilder class EnvChecker attr_reader :whitelist, :blacklist + attr_writer :whitelist, :blacklist - def initialize(whitelist, blacklist) - self.whitelist = get_valid_data(whitelist) - self.blacklist = get_valid_data(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?) && whitelist.find{|a| blacklist.include?(a) }.blank? - if whitelist.include?('*') || (!valid_for_env?(blacklist, env_name) || valid_for_env?(whitelist, env_name)) - return true + 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 @@ -20,24 +25,25 @@ def available_for_env?(env_name) private - def valid_for_env?(env_name) - try_find_suitable_env(env_name).present? + 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(env_name) + 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 - env_list.find do |host_pattern| - if host_pattern.is_a? Regexp - host_pattern.match env_name - elsif host_pattern.is_a? String - host_pattern == env_name + 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.try(:last) + 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