Class: MicroservicesEngine::V1::DataController

Inherits:
ApplicationController show all
Defined in:
app/controllers/microservices_engine/v1/data_controller.rb

Instance Method Summary collapse

Instance Method Details

#registerObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/microservices_engine/v1/data_controller.rb', line 7

def register
  # TO-DO
  # . . .

  # Current assumption of example request format
  #
  # {
  #     'build': 1.0.0,
  #     'token': 'a72!j*^bQ34dE%SS$#haBd%67#cD',
  #     'content': {
  #       {
  #         'name': 'Endpoint 1'
  #         'object': 'FieldTrip'
  #         'url': 'http://example.com/microservices_engine/v1/data'
  #       },
  #       {
  #         'name': 'Endpoint 2'
  #         'object': 'Survey'
  #         'url': 'http://potatoes.com/microservices_engine/v1/data'
  #       }
  #     }
  # }
  #

  data = params['content']
  data_objects = data.map { |d| d['object'] }

  # Disabled until router implements token authorization
  # verify_token(params['token'])
  verify_build(params['build'])

  existing = Connection.all
  in_request = existing.dup.to_a.keep_if { |c| data_objects.include? c.object }
  not_in_request = existing - in_request

  # Remove all objects not included in the request
  not_in_request.each do |unwanted|
    Connection.destroy(unwanted.id)
  end

  # 'Find and update' or 'Create' all remaining models
  data.each do |endpoint|
    desired = Connection.find_by object: endpoint['object']
    if desired.present?
      desired.update_attributes(
        name: endpoint.require(:name),
        url: endpoint.require(:url)
      )
    else
      Connection.create(
        name: endpoint.require(:name),
        url: endpoint.require(:url),
        object: endpoint.require(:object)
      )
    end
  end
  render json: { 'response': 200 }, status: :ok
end