Skip to content

Commit

Permalink
allow regexs to be used when checking availability for envs
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanRada committed Jun 21, 2016
1 parent 9f78c45 commit 6b6a1b9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 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
6 changes: 1 addition & 5 deletions lib/washout_builder/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 21 additions & 15 deletions lib/washout_builder/env_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,48 @@ 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


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
Expand Down
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 6b6a1b9

Please sign in to comment.