Module: Simpleflow

Defined in:
lib/simpleflow.rb,
lib/simpleflow/yql.rb,
lib/simpleflow/http.rb,
lib/simpleflow/json.rb,
lib/simpleflow/ping.rb,
lib/simpleflow/liquid.rb,
lib/simpleflow/jsonpath.rb,
lib/simpleflow/urlencode.rb,
lib/simpleflow/dns_resolve.rb,
lib/simpleflow/object_builder.rb,
lib/simpleflow/string_builder.rb

Defined Under Namespace

Modules: Modules

Constant Summary collapse

VERSION =
'0.0.1'

Class Method Summary collapse

Class Method Details

.run(config, params = {}, debug = false, find_method = nil) ⇒ 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
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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
# File 'lib/simpleflow.rb', line 33

def self.run(config, params = {}, debug = false, find_method = nil)
	
   wires = config["wires"] || []
   modules = config["modules"] || []
       
	# Store the results of each sub-module
   execValues = {} 

	# list all of the modules that we can execute
   moduleIdsToExecute = [] 

	# List all already executed modules
   executedModules = []


   firstTimeInLoop = true
   moreModulesToRun = false
   while moreModulesToRun or firstTimeInLoop do
       firstTimeInLoop = false
     
       # Recalculate all modules that can be executed :
       mId = 0
       modules.each { |m2|
         if !moduleIdsToExecute.include?(mId) and !executedModules.include?(mId) # don't re-execute modules
           mayEval = true
           # runnable if all wires which target is this mId have a source output evaluated.
           wires.each { |w|
             if w["tgt"]["moduleId"] == mId
               if !execValues[ w["src"]["moduleId"] ]
                 mayEval = false
               end
             end
           }            
           moduleIdsToExecute << mId if mayEval
         end  
         mId += 1
       }
     
       if moduleIdsToExecute.size == 0
         moreModulesToRun = false
       else
         moreModulesToRun = true
         
           moduleId = moduleIdsToExecute.shift
           m = modules[moduleId]
       
           # Build the parameters for the execution
           p = {}
           # params passed to the run method
           params.each { |k,v| p[k] = v }

           # default form values
           m["value"].each { |k,v| p[k] = v }

					
					puts "Wires: "+wires.inspect

           # incoming wire params
           wires.each { |w|
             if w["tgt"]["moduleId"] == moduleId

							puts "Incoming wire: "+w.inspect

							key =  w["tgt"]["terminal"]
							val = execValues[ w["src"]["moduleId"] ][ w["src"]["terminal"] ]

							# We simply want to do p[key] = val,
							# except that key might look like   "myvar[1][firstname]"
							# which mean we have to store this val in p["myvar"][1]["firstname"] instead
							push_to = p					
							pathItems = key.scan(/\[([^\]]*)\]/).map { |i| i[0].match(/\d+/) ? i[0].to_i : i[0] }
							if pathItems.size > 0
								push_to = push_to[key.match(/^([^\[]*)/)[0]]
							end
							if pathItems.size > 1
								0.upto(pathItems.size-2) { |i| push_to = push_to[  pathItems[i]  ] }
							end
							if pathItems.size > 0
								key = pathItems.last
							end
               push_to[key] = val

             end
           }


           puts "\n Starting "+m["name"]+" MODULE with : "+p.inspect
       
           # Execute Ruby base modules
           if !Modules.methods.index( m["name"] ).nil?
						begin
             	execValues[moduleId] = Modules.send( m["name"].to_sym, p)
						rescue Exception => e
							puts "Simpleflow "+e.message
						end

           else
             # Try to execute composed module
						if find_method
							w = find_method.call(m["name"])
							if !w
								raise "Module "+m["name"]+" not found !"
							end
              execValues[moduleId] = Simpleflow.run(w, p, false, find_method)
						else
							raise "Module "+m["name"]+" not found !"
						end
														
           end
       
					puts "\n Finished "+m["name"]+" MODULE with : "+execValues[moduleId].inspect
           

           # Mark this modules as executed
           executedModules.push(moduleId)
     
       end
       
   end  
   
   # Return internal execution values if debug mode
   return execValues if debug

   
   # OUTPUTS 
  outputValues = {}
   moduleIndex = 0
  modules.each { |m|
    if m["name"] == "output"
      wires.each { |w|
        if w["tgt"]["moduleId"] == moduleIndex and execValues[w["src"]["moduleId"]]
          outputValues[m["value"]["name"]] = execValues[w["src"]["moduleId"]][ w["src"]["terminal"] ]
         end
       }
     end
     moduleIndex += 1
   }
   return outputValues
	
end