Module: Mushy::Builder::Api
- Defined in:
- lib/mushy/builder/api.rb
Class Method Summary collapse
- .get_flow(file) ⇒ Object
- .get_fluxs ⇒ Object
- .organize_as_a_flattened_tree_based_on_parents(fluxs) ⇒ Object
- .run(data) ⇒ Object
- .run_this_as_a_daemon(&block) ⇒ Object
- .run_this_inline(&block) ⇒ Object
- .save(file, data) ⇒ Object
- .standardize_these(fluxs) ⇒ Object
- .start(file, event) ⇒ Object
Class Method Details
.get_flow(file) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/mushy/builder/api.rb', line 101 def self.get_flow(file) file = "#{file}.mushy" unless file.downcase.end_with?('.mushy') data = JSON.parse File.open(file).read data['fluxs'] = standardize_these data['fluxs'] data['fluxs'] = organize_as_a_flattened_tree_based_on_parents data['fluxs'] data rescue { fluxs: [] } end |
.get_fluxs ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/mushy/builder/api.rb', line 147 def self.get_fluxs { fluxs: Mushy::Flux.all .select { |x| x.respond_to? :details } .select(&:details) .map do |flux| details = flux.details details[:documentation] = Mushy::Builder::Documentation.build_from 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 this key, with the value as the key. If a group key is provided like group_by|group_key, then multiple events with the results under group_key will be returned.', 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][:ignore] = { type: 'text', shrink: true, description: 'Ignore these keys.', value: '', default: '' } details[:config][:model] = { type: 'keyvalue', shrink: true, description: 'Reshape the outgoing events.', value: {}, default: {} } details[:config][:error_strategy] = { description: 'Error strategy. (return to return an event with "exception" returning the error, or ignore to ignore the exception)', type: 'select', options: ['', 'return', 'ignore'], value: '', shrink: true } if flux.new.respond_to? :loop details[:config][:run_strategy] = { description: 'Run this using this strategy. (select "daemon" if this should be run in the background)', type: 'select', options: ['', 'inline', 'daemon'], value: '', shrink: true } end 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.sort_by { |x| x[:name] } } end |
.organize_as_a_flattened_tree_based_on_parents(fluxs) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mushy/builder/api.rb', line 127 def self.organize_as_a_flattened_tree_based_on_parents(fluxs) fluxs = fluxs.sort_by { |x| x['parents'].count } new_fluxs = [fluxs.first] loop do next_fluxs = fluxs.select { |x| x['parents'].include? new_fluxs[-1]['id'] } unless next_fluxs.any? next_fluxs = [fluxs.reject { |x| new_fluxs.map { |y| y['id'] }.include?(x['id']) }[0]].select { |x| x } end new_fluxs = [new_fluxs, next_fluxs].flatten break unless next_fluxs.any? end new_fluxs end |
.run(data) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mushy/builder/api.rb', line 7 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_this_as_a_daemon(&block) ⇒ Object
97 98 99 |
# File 'lib/mushy/builder/api.rb', line 97 def self.run_this_as_a_daemon(&block) Daemons.call(&block).pid.pid end |
.run_this_inline(&block) ⇒ Object
93 94 95 |
# File 'lib/mushy/builder/api.rb', line 93 def self.run_this_inline(&block) block.call end |
.save(file, data) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/mushy/builder/api.rb', line 21 def self.save(file, data) file = "#{file}.mushy" unless file.downcase.end_with?('.mushy') data = SymbolizedHash.new JSON.parse(data) Mushy::WriteFile.new.process({}, { name: file, data: JSON.pretty_generate(data) }) end |
.standardize_these(fluxs) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/mushy/builder/api.rb', line 113 def self.standardize_these(fluxs) fluxs .reject { |x| x['parents'] } .each { |x| x['parents'] = [x['parent']].select { |y| y } } fluxs .select { |x| x['parent'] } .each { |x| x.delete 'parent' } fluxs .select { |x| x['parents'] } .each { |x| x['parents'] = x['parents'].select { |y| y } } fluxs end |
.start(file, event) ⇒ Object
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 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 |
# File 'lib/mushy/builder/api.rb', line 28 def self.start(file, event) original_file = file file = [file, "#{Dir.home}/.mushy/#{file}"] .map { |x| (x.downcase.end_with?('.mushy') ? x : "#{x}.mushy") } .select { |x| File.exist?(x) } .first unless file puts "#{original_file} does not exist." return end 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? things = service_fluxes .map do |s| { flux: s, proc: lambda do |e| Dir.chdir(pwd) Mushy::Runner.new.start(e, s, flow) end, run_method: (s.config[:run_strategy] == 'daemon' ? :run_this_as_a_daemon : :run_this_inline) } end.group_by { |x| x[:run_method] } (things[:run_this_as_a_daemon] || []) .map { |p| -> { p[:flux].loop(&p[:proc]) } } .map { |x| -> { loop(&x) } } .map { |x| run_this_as_a_daemon(&x) } (things[:run_this_inline] || []) .map { |p| -> { p[:flux].loop(&p[:proc]) } } .map { |x| -> { loop(&x) } } .map { |x| run_this_inline(&x) } exit end web_flux = flow.fluxs.select { |x| x.is_a?(Mushy::Web) }.first if web_flux if event[:method] return Mushy::Runner.new.start event, web_flux, flow else return Mushy::FireUpAWebServer.new.tap do |x| x.id = SecureRandom.uuid x.flow_id = flow.id x.flow = flow x.flux = web_flux end end end cli_flux = flow.fluxs.select { |x| x.is_a?(Mushy::Cli) }.first Mushy::Runner.new.start event, cli_flux, flow end |