Class: Jerakia::Policy

Inherits:
Object
  • Object
show all
Defined in:
lib/jerakia/policy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, _opts) ⇒ Policy

_opts currently does not get used, but is included here as a placeholder for allowing policies to be declared with options; policy :foo, :option => :value do


16
17
18
19
20
# File 'lib/jerakia/policy.rb', line 16

def initialize(name, _opts)
  @name = name
  @lookups = []
  @datasources = {}
end

Instance Attribute Details

#datasourcesObject (readonly)

Returns the value of attribute datasources


10
11
12
# File 'lib/jerakia/policy.rb', line 10

def datasources
  @datasources
end

#lookupsObject

Returns the value of attribute lookups


8
9
10
# File 'lib/jerakia/policy.rb', line 8

def lookups
  @lookups
end

#nameObject (readonly)

Returns the value of attribute name


9
10
11
# File 'lib/jerakia/policy.rb', line 9

def name
  @name
end

Instance Method Details

#clone_request(request) ⇒ Object


55
56
57
# File 'lib/jerakia/policy.rb', line 55

def clone_request(request)
  Marshal.load(Marshal.dump(request))
end

#register_datasource(datasource) ⇒ Object


51
52
53
# File 'lib/jerakia/policy.rb', line 51

def register_datasource(datasource)
  Jerakia::Datasource.load_datasource(datasource)
end

#run(request) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jerakia/policy.rb', line 22

def run(request)

  if request.use_schema && Jerakia.config[:enable_schema]
    schema_config = Jerakia.config[:schema] || {}
    @schema = Jerakia::Schema.new(request, schema_config)
  end

  scope = Jerakia::Scope.new(request)
  answer = Jerakia::Answer.new(request.lookup_type, request.merge)

  response_entries = []
  lookups.each do |lookup|
    lookup_instance = lookup.call clone_request(request), scope
    next unless lookup_instance.valid?
    register_datasource lookup_instance.datasource[:name]
    responses = Jerakia::Datasource.run(lookup_instance)
    lookup_instance.output_filters.each do |filter|
      Jerakia.log.debug("Using output filter #{filter[:name]}")
      responses.filter! filter[:name], filter[:opts]
    end
    lookup_answers = responses.entries.map { |r| r}
    response_entries << lookup_answers if lookup_answers
    break unless lookup_instance.proceed?
  end
  answer.process_response(response_entries)
  Jerakia.log.debug(answer)
  return answer
end