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) ⇒ Object

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



6
7
8
9
10
11
# File 'lib/api_valve/proxy/builder.rb', line 6

def build(config)
  block = Proc.new if block_given? # capture the yield
  from_hash(config).tap do |proxy|
    proxy.instance_eval(&block) if block
  end
end

#from_config(file_name = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/api_valve/proxy/builder.rb', line 13

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, nil, '-').result if path.fnmatch? '*.erb'
  from_yaml yaml
end

#from_hash(config) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/api_valve/proxy/builder.rb', line 27

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



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

def from_yaml(string)
  from_hash YAML.load(string) # rubocop:disable Security/YAMLLoad
end