Module: Mushy::Builder::Api

Defined in:
lib/mushy/builder/api.rb

Class Method Summary collapse

Class Method Details

.get_flow(file) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mushy/builder/api.rb', line 67

def self.get_flow file
  puts "trying to get: #{file}"
  file = "#{file}.json" unless file.downcase.end_with?('.json')
  data = JSON.parse File.open(file).read
  data['fluxs']
    .reject { |x| x['parents'] }
    .each   { |x| x['parents'] = [x['parent']].select { |y| y } }
  data['fluxs']
    .select { |x| x['parent'] }
    .each   { |x| x.delete 'parent' }
  data['fluxs']
    .select { |x| x['parents'] }
    .each   { |x| x['parents'] = x['parents'].select { |y| y } }
  data
rescue
  { fluxs: [] }
end

.get_fluxsObject



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
# File 'lib/mushy/builder/api.rb', line 85

def self.get_fluxs
  {
    fluxs: Mushy::Flux.all.select { |x| x.respond_to? :details }.map do |flux|
                   details = flux.details
                   details[:config][:incoming_split] = { type: 'text', shrink: true, description: 'Split an incoming event into multiple events by this key, an each event will be processed independently.', default: '' }
                   details[:config][:outgoing_split] = { type: 'text', shrink: true, description: 'Split an outgoing event into multiple events by this key.', default: '' }
                   details[:config][:merge] = { type: 'text', shrink: true, description: 'A comma-delimited list of fields from the event to carry through. Use * to merge all fields.', default: '' }
                   details[:config][:group] = { type: 'text', shrink: true, description: 'Group events by a field, which is stored in a key. The format is group_by|group_key.', default: '' }
                   details[:config][:limit] = { type: 'integer', shrink: true, description: 'Limit the number of events to this number.', default: '' }
                   details[:config][:join] = { type: 'text', shrink: true, description: 'Join all of the events from this flux into one event, under this name.', default: '' }
                   details[:config][:sort] = { type: 'text', shrink: true, description: 'Sort by this key.', default: '' }
                   details[:config][:model] = { type: 'keyvalue', shrink: true, description: 'Reshape the outgoing events.', value: {}, default: {} }

                   details[:config]
                          .select { |_, v| v[:type] == 'keyvalue' }
                          .select { |_, v| v[:editors].nil? }
                          .each   do |_, v|
                                    v[:editors] = [
                                              { id: 'new_key', target: 'key', field: { type: 'text', value: '', default: '' } },
                                              { id: 'new_value', target: 'value', field: { type: 'text', value: '', default: '' } }
                                            ]
                            end

                   details
           end
  }
end

.run(data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mushy/builder/api.rb', line 9

def self.run data

  data = SymbolizedHash.new JSON.parse(data)

  event = SymbolizedHash.new JSON.parse(data[:setup][:event].to_json)

  config = SymbolizedHash.new data[:config]

  flux = Mushy::Flow.build_flux( { type: data[:setup][:flux], config: config } )

  result = flux.execute event

  [result].flatten
end

.run_as_a_daemon(&block) ⇒ Object



62
63
64
65
# File 'lib/mushy/builder/api.rb', line 62

def self.run_as_a_daemon &block
  #block.call
  Daemons.call(&block).pid.pid
end

.save(file, data) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/mushy/builder/api.rb', line 24

def self.save file, data

  file = "#{file}.json" unless file.downcase.end_with?('.json')

  data = SymbolizedHash.new JSON.parse(data)
  Mushy::WriteFile.new.process( {}, { name: file, data: data.to_json })

end

.start(file, event) ⇒ Object



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
# File 'lib/mushy/builder/api.rb', line 33

def self.start file, event
  file = "#{file}.json" unless file.downcase.end_with?('.json')
  flow = File.open(file).read
  flow = Mushy::Flow.parse flow

  service_fluxes = flow.fluxs.select { |x| x.respond_to? :loop }

  pwd = Dir.pwd

  if service_fluxes.any?
    calls = service_fluxes
       .map { |s| { flux: s, proc: ->(e) do
                                           Dir.chdir pwd
                                           Mushy::Runner.new.start e, s, flow
                                         end } }
       .map { |p| ->() { p[:flux].loop &p[:proc] } }
       .map { |x| ->() { loop &x } }
       .map { |x| run_as_a_daemon &x }

    puts calls.inspect

    exit
  end

  cli_flux = flow.fluxs.select { |x| x.kind_of?(Mushy::Cli) }.first

  Mushy::Runner.new.start event, cli_flux, flow
end