Class: CPEE::EvalRuby::DoIt

Inherits:
Riddl::Implementation
  • Object
show all
Defined in:
lib/cpee-eval-ruby/implementation.rb

Overview

{{{

Instance Method Summary collapse

Instance Method Details

#exec(__struct, __code, result = nil, headers = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cpee-eval-ruby/implementation.rb', line 36

def exec(__struct,__code,result=nil,headers=nil)
  __ret = {}
  __cat = catch WEEL::Signal::Again do
    __ret[:res] = JSON::generate(__struct.instance_eval(__code))
    WEEL::Signal::Proceed
  end
  if __cat.nil? || __cat == WEEL::Signal::Again
    __ret[:signal] << 'Signal::Again'
  end
rescue => err
  __ret[:signal] = 'Signal::Error'
  __ret[:signal_text] = (err.backtrace ? err.backtrace[0].gsub(/([\w -_]+):(\d+):in.*/,'\\1, Line \2: ') : '') + err.message
rescue WEEL::Signal::Again
  __ret[:signal] = 'Signal::Again'
rescue  WEEL::Signal::Error => err
  __ret[:signal] = 'Signal::Error'
  __ret[:signal_text] = (err.backtrace ? err.backtrace[0].gsub(/([\w -_]+):(\d+):in.*/,'\\1, Line \2: ') : '') + err.message
rescue WEEL::Signal::Stop
  __ret[:signal] = 'Signal::Stop'
rescue SyntaxError => err
  __ret[:signal] = 'Signal::SyntaxError'
  __ret[:signal_text] = err.message
ensure
  return __ret
end

#responseObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cpee-eval-ruby/implementation.rb', line 62

def response
  mode = @a[0]
  code = @p.shift.value
  dataelements = JSON::parse(@p.shift.value.read)
  local = nil
  local = JSON::parse(@p.shift.value.read) if @p[0].name == 'local'
  endpoints = JSON::parse(@p.shift.value.read)
  additional = JSON::parse(@p.shift.value.read)
  status = JSON::parse(@p.shift.value.read) if @p.any? && @p[0].name == 'status'
  status = WEEL::Status.new(status['id'],status['message']) if status
  call_result = JSON::parse(@p.shift.value.read) if @p.any? && @p[0].name == 'call_result'
  call_headers = JSON::parse(@p.shift.value.read) if @p.any? && @p[0].name == 'call_headers'

  local = local[0] if local && local.is_a?(Array)

  # symbolize keys, because JSON
  dataelements.transform_keys!{|k| k.to_sym}
  local.transform_keys!{|k| k.to_sym} if local
  endpoints.transform_keys!{|k| k.to_sym}
  additional.transform_keys!{|k| k.to_sym}
  additional.each_value do |v|
    v.transform_keys!{|k| k.to_sym}
  end

  if status || call_result || call_headers
    struct = WEEL::ManipulateStructure.new(dataelements,endpoints,status,local,additional)
    execresult = exec struct, code, CPEE::EvalRuby::Translation::simplify_structurized_result(call_result), call_headers

    send = []
    send << Riddl::Parameter::Complex.new('result','application/json',execresult[:res] || '')
    if execresult[:signal]
      send << Riddl::Parameter::Simple.new('signal',execresult[:signal])
      send << Riddl::Parameter::Simple.new('signal_text',execresult[:signal_text] || '')
      @status = 555
    end

    res = {}
    struct.changed_data.each do |e|
      res[e] = struct.data[e]
    end
    if res.any?
      send << Riddl::Parameter::Complex.new('dataelements','application/json',JSON::generate(dataelements)) if mode == :full
      send << Riddl::Parameter::Complex.new('changed_dataelements','application/json',JSON::generate(res))
    end
    res = {}
    struct.changed_endpoints.each do |e|
      res[e] = struct.endpoints[e]
    end
    if res.any?
      send << Riddl::Parameter::Complex.new('endpoints','application/json',JSON::generate(endpoints)) if mode == :full
      send << Riddl::Parameter::Complex.new('changed_endpoints','application/json',JSON::generate(res))
    end
    send << Riddl::Parameter::Complex.new('changed_status','application/json',JSON::generate(status)) if struct.changed_status
    send
  else
    struct = WEEL::ReadStructure.new(dataelements,endpoints,local,additional)
    execresult = exec struct, code
    send = []
    send << Riddl::Parameter::Complex.new('result','application/json',execresult[:res])
    if execresult[:signal]
      send << Riddl::Parameter::Simple.new('signal',execresult[:signal])
      send << Riddl::Parameter::Simple.new('signal_text',execresult[:signal_text] || '')
      @status = 555
    end
    send
  end
end