Class: Vop::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/vop/objects/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op, command_name, param_values = {}, extra = {}) ⇒ Request

Returns a new instance of Request.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/vop/objects/request.rb', line 12

def initialize(op, command_name, param_values = {}, extra = {})
  @op = op
  @command_name = command_name
  raise "unknown command '#{command_name}'" if command.nil?
  @param_values = param_values
  @extra = extra
  @shell = nil

  @current_filter = nil
  @filter_chain = @op.filter_chain.clone
end

Instance Attribute Details

#command_nameObject (readonly)

Returns the value of attribute command_name.



9
10
11
# File 'lib/vop/objects/request.rb', line 9

def command_name
  @command_name
end

#extraObject (readonly)

Returns the value of attribute extra.



9
10
11
# File 'lib/vop/objects/request.rb', line 9

def extra
  @extra
end

#param_valuesObject (readonly)

Returns the value of attribute param_values.



9
10
11
# File 'lib/vop/objects/request.rb', line 9

def param_values
  @param_values
end

#shellObject

Returns the value of attribute shell.



10
11
12
# File 'lib/vop/objects/request.rb', line 10

def shell
  @shell
end

Class Method Details

.from_json(op, json) ⇒ Object



41
42
43
44
# File 'lib/vop/objects/request.rb', line 41

def self.from_json(op, json)
  hash = JSON.parse(json)
  self.new(op, hash["command"], hash["params"], hash["extra"])
end

Instance Method Details

#cache_keyObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vop/objects/request.rb', line 28

def cache_key
  blacklist = %w|shell raw_params|

  ex = Executor.new(@op)
  prepared = ex.prepare_params(self)

  param_string = prepared.map { |k,v|
    next if blacklist.include? k
    [k.to_s,v].join("=")
  }.sort.compact.join(":")
  "vop/request:#{command.name}:" + param_string + ":v1"
end

#commandObject



24
25
26
# File 'lib/vop/objects/request.rb', line 24

def command
  @op.commands[@command_name]
end

#executeObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vop/objects/request.rb', line 58

def execute
  result = nil
  context = nil

  # build a chain out of all filters + the command itself
  filter_chain = @op.filter_chain.clone.map {
    |filter_name| @op.filters[filter_name.split(".").first]
  }
  filter_chain << Executor.new(@op)
  @chain = Chain.new(@op, filter_chain)
  @chain.execute(self)
end

#next_filterObject



54
55
56
# File 'lib/vop/objects/request.rb', line 54

def next_filter
   @chain.next()
end

#to_jsonObject



46
47
48
49
50
51
52
# File 'lib/vop/objects/request.rb', line 46

def to_json
  {
      command: @command_name,
      params: @param_values,
      extra: @extra
  }.to_json
end