Skip to content

Commit

Permalink
Merge branch 'feature/allow-regexs-for-whitelistig-and-blacklisting-e…
Browse files Browse the repository at this point in the history
…nvs' into develop
  • Loading branch information
bogdanRada committed Jun 21, 2016
2 parents c1c3901 + 6b6a1b9 commit b912526
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 18 deletions.
19 changes: 11 additions & 8 deletions lib/washout_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 4 additions & 9 deletions lib/washout_builder/engine.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
50 changes: 50 additions & 0 deletions lib/washout_builder/env_checker.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/washout_builder/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b912526

Please sign in to comment.