module ApiValve::Proxy::Builder
Public Instance Methods
build(config, &block)
click to toggle source
Creates an instance from a config hash and takes optional block which is executed in scope of the proxy
# File lib/api_valve/proxy/builder.rb, line 6 def build(config, &block) from_hash(config).tap do |proxy| proxy.instance_eval(&block) if block end end
from_config(file_name = nil)
click to toggle source
# File lib/api_valve/proxy/builder.rb, line 12 def from_config(file_name = nil) file_name ||= name.underscore path = find_config(file_name) raise "Config not found for #{name.underscore}(.yml|.yml.erb) in #{ApiValve.config_paths.inspect}" unless path yaml = File.read(path) yaml = ERB.new(yaml, trim_mode: '-').result if path.fnmatch? '*.erb' from_yaml yaml end
from_hash(config)
click to toggle source
# File lib/api_valve/proxy/builder.rb, line 26 def from_hash(config) config = config.with_indifferent_access forwarder = Forwarder.new(forwarder_config(config)) new(forwarder).tap do |proxy| Array.wrap(config[:use]).each { |mw| proxy.use mw } add_routes_from_config proxy, config[:routes] proxy.use Middleware::PermissionCheck, config[:permission_handler] if config[:permission_handler] end end
from_yaml(string)
click to toggle source
# File lib/api_valve/proxy/builder.rb, line 22 def from_yaml(string) from_hash YAML.load(string) # rubocop:disable Security/YAMLLoad end
Private Instance Methods
add_routes_from_config(proxy, routes_config)
click to toggle source
# File lib/api_valve/proxy/builder.rb, line 38 def add_routes_from_config(proxy, routes_config) return proxy.forward_all unless routes_config routes_config.each do |route_config| method, path_regexp = *route_config.values_at('method', 'path') method ||= 'any' # no method defined means all methods if route_config['raise'] proxy.deny method, path_regexp, with: route_config['raise'] else proxy.forward method, path_regexp, route_config.except('method', 'path') end end end
find_config(file_name)
click to toggle source
# File lib/api_valve/proxy/builder.rb, line 52 def find_config(file_name) ApiValve.config_paths.each do |dir| path = dir.join("#{file_name}.yml") return path if path.exist? path = dir.join("#{file_name}.yml.erb") return path if path.exist? end nil end
forwarder_config(config)
click to toggle source
# File lib/api_valve/proxy/builder.rb, line 63 def forwarder_config(config) { request: {klass: request}, response: {klass: response} }.with_indifferent_access.deep_merge config.slice(*FORWARDER_OPTIONS) end