Class: Hyperloop::ServerOp

Inherits:
Operation show all
Defined in:
lib/hyper-operation/server_op.rb

Direct Known Subclasses

ControllerOp

Constant Summary

Constants inherited from Operation

Operation::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Operation

_Railway, _run, #abort!, #add_error, add_error, async, fail, failed, #has_errors?, inbound, inherited, #initialize, on_dispatch, outbound, param, #params, run, step, #succeed!, then, validate

Constructor Details

This class inherits a constructor from Hyperloop::Operation

Class Method Details

.deserialize_dispatch(hash) ⇒ Object



73
74
75
# File 'lib/hyper-operation/server_op.rb', line 73

def deserialize_dispatch(hash)
  hash
end

.deserialize_params(hash) ⇒ Object



57
58
59
# File 'lib/hyper-operation/server_op.rb', line 57

def deserialize_params(hash)
  hash
end

.deserialize_response(hash) ⇒ Object



65
66
67
# File 'lib/hyper-operation/server_op.rb', line 65

def deserialize_response(hash)
  hash
end

.dispatch_from_server(params_hash) ⇒ Object



96
97
98
99
# File 'lib/hyper-operation/server_op.rb', line 96

def dispatch_from_server(params_hash)
  params = _Railway.params_wrapper.new(deserialize_dispatch(params_hash)).lock
  _Railway.receivers.each { |receiver| receiver.call params }
end

.dispatch_to(*args, &regulation) ⇒ Object



77
78
79
# File 'lib/hyper-operation/server_op.rb', line 77

def dispatch_to(*args, &regulation)
  _dispatch_to(nil, args, &regulation) if RUBY_ENGINE != 'opal'
end

.remote(path, *args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hyper-operation/server_op.rb', line 35

def remote(path, *args)
  promise = Promise.new
  uri = URI("#{path}execute_remote_api")
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
  if uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  request.body = {
    operation: name,
    params: Hyperloop::Operation::ParamsWrapper.combine_arg_array(args)
  }.to_json
  promise.resolve http.request(request)
rescue Exception => e
  promise.reject e
end

.run_from_client(security_param, controller, operation, params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hyper-operation/server_op.rb', line 20

def run_from_client(security_param, controller, operation, params)
  operation.constantize.class_eval do
    if _Railway.params_wrapper.method_defined?(:controller)
      params[:controller] = controller
    elsif !_Railway.params_wrapper.method_defined?(security_param)
      raise AccessViolation
    end
    run(params)
    .then { |r| return { json: { response: serialize_response(r) } } }
    .fail { |e| return { json: { error: e }, status: 500 } }
  end
rescue Exception => e
  { json: {error: e}, status: 500 }
end

.serialize_dispatch(hash) ⇒ Object



69
70
71
# File 'lib/hyper-operation/server_op.rb', line 69

def serialize_dispatch(hash)
  hash
end

.serialize_params(hash) ⇒ Object



53
54
55
# File 'lib/hyper-operation/server_op.rb', line 53

def serialize_params(hash)
  hash
end

.serialize_response(hash) ⇒ Object



61
62
63
# File 'lib/hyper-operation/server_op.rb', line 61

def serialize_response(hash)
  hash
end

Instance Method Details

#_dispatch_to(context, args = [], &regulation) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hyper-operation/server_op.rb', line 81

def _dispatch_to(context, args=[], &regulation)
  if args.count == 0 && regulation.nil?
    raise "must provide either a list of channel classes or a block to regulate_dispatch"
  elsif args.count > 0 && regulation
    raise "cannot provide both a list of channel classes and a block to regulate_dispatch"
  end
  regulation ||= proc { args }
  on_dispatch do |params, operation|
    serialized_params = serialize_dispatch(params.to_h)
    [operation.instance_exec(*context, &regulation)].flatten.compact.uniq.each do |channel|
      Hyperloop.dispatch(channel: Hyperloop::InternalPolicy.channel_to_string(channel), operation: operation.class.name, params: serialized_params)
    end
  end
end

#run(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/hyper-operation/server_op.rb', line 5

def run(*args)
  hash = _Railway.params_wrapper.combine_arg_array(args)
  hash = serialize_params(hash)
  HTTP.post(
    "#{`window.HyperloopEnginePath`}/execute_remote",
    payload: {json: {operation: name, params: hash}.to_json},
    headers: {'X-CSRF-Token' => Hyperloop::ClientDrivers.opts[:form_authenticity_token] }
    )
  .then do |response|
    deserialize_response response.json[:response]
  end.fail do |response|
    Exception.new response.json[:error]
  end
end