Module: ApiValve::Proxy::Builder

Included in:
ApiValve::Proxy
Defined in:
lib/api_valve/proxy/builder.rb

Instance Method Summary collapse

Instance Method Details

#build(config, &block) ⇒ Object

Creates an instance from a config hash and takes optional block which is executed in scope of the proxy



6
7
8
9
10
# 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) ⇒ Object



12
13
14
15
16
17
18
19
20
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



22
23
24
# File 'lib/api_valve/proxy/builder.rb', line 22

def from_yaml(string)
  from_hash YAML.safe_load(string, permitted_classes: ApiValve.safe_load_classes)
end