Class: Manywho::Engine
- Inherits:
-
Object
- Object
- Manywho::Engine
- Defined in:
- lib/manywho.rb
Instance Method Summary collapse
-
#create_EngineInitializationRequest(flowResponse, annotations = nil, inputs = [], mode = nil) ⇒ Object
Creates an EngineInitializationRequest to be sent to the server using get_EngineInitializationResponse.
-
#create_EngineInvokeRequest(engineInitializationResponse, flowResponse = nil, invokeType = "FORWARD") ⇒ Object
Create an EngineInvokeRequest to be posted to the server using get_EngineInvokeResponse.
-
#get_EngineInitializationResponse(engineInitializationRequest) ⇒ Object
Gets an EngineInitializationResponse from the server, using a HTTP POST request.
-
#get_EngineInvokeResponse(engineInvokeRequest) ⇒ Object
Post the EngineInvokeRequest, and return the EngineInvokeResponse.
-
#get_FlowResponse(flowId) ⇒ Object
Gets a FlowResponse object from the server for the provided ID.
-
#initialize ⇒ Engine
constructor
Initialize instance variables.
-
#is_class(value, expectedClass, methodName, parameter) ⇒ Object
Tests if a value is of a specified class type, otherwise raises an error.
-
#is_ok(resp, url) ⇒ Object
Tests if a request has completed successfully, otherwise raises an error.
-
#is_valid_id(idString, idType) ⇒ Object
Tests if an id is valid, otherwise raises an error.
-
#load_flow(tenant, flowId, username = "", password = "") ⇒ Object
Load a flow, given the tenantId, flowId and logon details the first EngineInvokeResponse will be returned.
-
#login(engineInitializationResponse, username, password) ⇒ Object
Logs in to the flow.
-
#recreate_EngineInvokeRequest(engineInvokeResponse, selectedOutcomeId, invokeType = "FORWARD") ⇒ Object
Create a EngineInvokeRequest from an engineInvokeResponse - such as when an outcome is selected, and a new EngineInvokeRequest is required.
- #reset ⇒ Object
-
#select_OutcomeResponse(engineInvokeResponse, outcomeResponseDeveloperName, invokeType = "FORWARD") ⇒ Object
Select an outcomeResponse, and get the next EngineInvokeResponse.
-
#set_tenant(tenantUniqueID) ⇒ Object
This method sets the Tenant Unique ID.
Constructor Details
#initialize ⇒ Engine
Initialize instance variables
32 33 34 |
# File 'lib/manywho.rb', line 32 def initialize reset() end |
Instance Method Details
#create_EngineInitializationRequest(flowResponse, annotations = nil, inputs = [], mode = nil) ⇒ Object
Creates an EngineInitializationRequest to be sent to the server using get_EngineInitializationResponse
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/manywho.rb', line 95 def create_EngineInitializationRequest(flowResponse, annotations = nil, inputs = [], mode = nil) # Ensure that all of the arguments are valid if ( is_class(flowResponse, FlowResponse, "create_EngineInitializationRequest", 1) ) && ( (annotations == nil) or (is_class( annotations, Hash, "create_EngineInitializationRequest", "annotations")) ) && ( (is_class( inputs, Array, "create_EngineInitializationRequest", "inputs")) ) && ( (mode == nil) or (is_class( mode, String, "create_EngineInitializationRequest", "mode")) ) # Create a hash to initialize the EngineInitializationRequest engineInitializationJSON = { "flowId" => flowResponse.id, "annotations" => annotations, "inputs" => inputs, "playerURL" => "https://flow.manywho.com/"+@TenantUID+"/play/myplayer", "joinPlayerURL" => "https://flow.manywho.com/"+@TenantUID+"/play/myplayer", "mode" => mode } return EngineInitializationRequest.new(engineInitializationJSON) end return false end |
#create_EngineInvokeRequest(engineInitializationResponse, flowResponse = nil, invokeType = "FORWARD") ⇒ Object
Create an EngineInvokeRequest to be posted to the server using get_EngineInvokeResponse
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/manywho.rb', line 161 def create_EngineInvokeRequest(engineInitializationResponse, flowResponse=nil, invokeType="FORWARD") # Ensure that all of the arguments are valid if ( is_class(engineInitializationResponse, EngineInitializationResponse, "create_EngineInvokeRequest", 1) ) && ( is_class(invokeType, String, "create_EngineInvokeRequest", "invokeType") ) && ( (flowResponse == nil) || (is_class(flowResponse, FlowResponse, "create_EngineInvokeRequest", "flowResponse")) ) # If a flowResponse is provided, use the startMapElementId # If no flowResponse is provided, such as the engine is syncing, use the currentMapElementId if (flowResponse) currentMapElementId = flowResponse.startMapElementId elsif currentMapElementId = engineInitializationResponse.currentMapElementId end # Create and return a new EngineInvokeRequest engineInvokeJSON = { "stateId" => engineInitializationResponse.stateId, "stateToken" => engineInitializationResponse.stateToken, "currentMapElementId" => currentMapElementId, #"7b2e4865-bd54-4073-b4f4-a4ec001afc9a", #### BODGE #### engineInitializationResponse.currentMapElementId, "invokeType" => invokeType, "geoLocation" => { "latitude" => 0, "longitude" => 0, "accuracy" => 0, "altitude" => 0, "altitudeAccuracy" => 0, "heading" => 0, "speed" => 0 }, "mapElementInvokeRequest" => { "selectedOutcomeId" => nil }, "mode" => nil } return EngineInvokeRequest.new(engineInvokeJSON) end return false end |
#get_EngineInitializationResponse(engineInitializationRequest) ⇒ Object
Gets an EngineInitializationResponse from the server, using a HTTP POST request.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/manywho.rb', line 115 def get_EngineInitializationResponse(engineInitializationRequest) # Ensure that all of the arguments are valid if ( is_class(engineInitializationRequest, EngineInitializationRequest, "get_EngineInitializationResponse", 1) ) # POST the EngineInitializationRequest resp, data = HTTP.post("/api/run/1/", engineInitializationRequest.to_json(), { "ManyWhoTenant" => @TenantUID , "content-type" => "application/json"} ) # If everything went well, return a new EngineInitializationResponse created from the server's response if ( is_ok(resp, "/api/run/1/") ) parsedJSON = JSON.parse(resp.body) return EngineInitializationResponse.new(parsedJSON) end end return false end |
#get_EngineInvokeResponse(engineInvokeRequest) ⇒ Object
Post the EngineInvokeRequest, and return the EngineInvokeResponse
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/manywho.rb', line 233 def get_EngineInvokeResponse(engineInvokeRequest) # Ensure that all arguments are valid if ( is_class(engineInvokeRequest, EngineInvokeRequest, "get_EngineInvokeResponse", 1) ) if (@LoginToken) # POST the EngineInvokeRequest, with authentication resp, data = HTTP.post("/api/run/1/state/" + engineInvokeRequest.stateId, engineInvokeRequest.to_json, { "ManyWhoTenant" => @TenantUID , "content-type" => "application/json", "Authorization" => @LoginToken} ) else # POST the EngineInvokeRequest, without authentication resp, data = HTTP.post("/api/run/1/state/" + engineInvokeRequest.stateId, engineInvokeRequest.to_json, { "ManyWhoTenant" => @TenantUID , "content-type" => "application/json"} ) end # If everything went well, return a new EngineInvokeResponse created from the server's response if ( is_ok(resp, "/api/run/1/state/" + engineInvokeRequest.stateId) ) parsedJSON = JSON.parse(resp.body) return EngineInvokeResponse.new(parsedJSON) end end return false end |
#get_FlowResponse(flowId) ⇒ Object
Gets a FlowResponse object from the server for the provided ID
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/manywho.rb', line 81 def get_FlowResponse(flowId) if ( is_valid_id(flowId, "FlowId") ) resp, data = HTTP.get("/api/run/1/flow/" + flowId, { "ManyWhoTenant" => @TenantUID , "content-type" => "application/json"} ) # If everything went well, return a new FlowResponse from the JSON object retrieved if ( is_ok(resp, "/api/run/1/flow/" + flowId) ) parsedJSON = JSON.parse(resp.body) return FlowResponse.new(parsedJSON) end end return false end |
#is_class(value, expectedClass, methodName, parameter) ⇒ Object
Tests if a value is of a specified class type, otherwise raises an error
70 71 72 73 74 75 76 77 78 |
# File 'lib/manywho.rb', line 70 def is_class(value, expectedClass, methodName, parameter) if (value.class.to_s == expectedClass.to_s) return true else puts "Error: parameter " + parameter.to_s + " of " + methodName + " must be a " + expectedClass.to_s + ". " + "Parameter " + parameter.to_s + " was a " + value.class.to_s return false end end |
#is_ok(resp, url) ⇒ Object
Tests if a request has completed successfully, otherwise raises an error
60 61 62 63 64 65 66 67 |
# File 'lib/manywho.rb', line 60 def is_ok(resp, url) if (resp.code == "200") && (resp.body.length) return true else puts "Error: something went wrong in the rsponse (" + url +")" return false end end |
#is_valid_id(idString, idType) ⇒ Object
Tests if an id is valid, otherwise raises an error
49 50 51 52 53 54 55 56 57 |
# File 'lib/manywho.rb', line 49 def is_valid_id(idString, idType) if (idString.is_a? String) && (idString =~ /^[-0-9a-f]+$/) && (idString.length == 36) && (idString.count("-") == 4) return true else puts "Error: id is not valid (" + idType + "): " + idString.to_s return false end end |
#load_flow(tenant, flowId, username = "", password = "") ⇒ Object
Load a flow, given the tenantId, flowId and logon details the first EngineInvokeResponse will be returned
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/manywho.rb', line 282 def load_flow(tenant, flowId, username="", password="") # Ensure all the arguments are valid if ( is_valid_id(tenant, "tenantId") ) && ( is_valid_id(flowId, "flowId") ) && ( is_class(username, String, "load_flow", "username") ) && ( is_class(password, String, "load_flow", "password") ) # Set the tenant set_tenant(tenant) # Get the FlowResponse for the flow by id flowResponse = get_FlowResponse(flowId) # Create an EngineInitializationRequest, and use it to retreive an EngineInitializationResponse from the server engineInitializationResponse = get_EngineInitializationResponse( create_EngineInitializationRequest( flowResponse ) ) # If required to log in to the flow if (engineInitializationResponse.statusCode == "401") # If login details, attempt to login if (username != "") && (password != "") login(engineInitializationResponse, username, password) else return "You need to login to run this flow: " + engineInitializationResponse..loginUrl#["loginUrl"] end end # Get a new EngineInvokeResponse from the server return engineInvokeResponse = get_EngineInvokeResponse( create_EngineInvokeRequest(engineInitializationResponse, flowResponse=flowResponse) ) end return false end |
#login(engineInitializationResponse, username, password) ⇒ Object
Logs in to the flow. Flows may require you to log in, this only has to be done once before executing the flow
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 |
# File 'lib/manywho.rb', line 134 def login(engineInitializationResponse, username, password) # Ensure that all of the arguments are valid if ( is_class(engineInitializationResponse, EngineInitializationResponse, "login", 1) ) && ( ( is_class(username, String, "login", 2) ) && (username.length) ) && ( ( is_class(password, String, "login", 3) ) && (password.length) ) # Create a logon request loginRequest = { "loginUrl" => engineInitializationResponse..loginUrl, #engineInitializationResponse.authorizationContext["loginUrl"], "username" => username, "password" => password } # POST the login request resp, data = HTTP.post("/api/run/1/authentication", loginRequest.to_json(), { "ManyWhoTenant" => @TenantUID , "content-type" => "application/json"} ) # If everything went well, set the logon token if ( is_ok(resp, "/api/run/1/authentication") ) @LoginToken = resp.body[1...resp.body.length-1] end end return false end |
#recreate_EngineInvokeRequest(engineInvokeResponse, selectedOutcomeId, invokeType = "FORWARD") ⇒ Object
Create a EngineInvokeRequest from an engineInvokeResponse - such as when an outcome is selected, and a new EngineInvokeRequest is required
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/manywho.rb', line 201 def recreate_EngineInvokeRequest(engineInvokeResponse, selectedOutcomeId, invokeType="FORWARD") # Ensure that all of the arguments are valid if ( is_class(engineInvokeResponse, EngineInvokeResponse, "recreate_EngineInvokeRequest", 1) ) && ( is_valid_id(selectedOutcomeId, "selectedOutcomeId") ) && ( is_class(invokeType, String, "create_EngineInvokeRequest", "invokeType") ) # Create and return a new EngineInvokeRequest engineInvokeJSON = { "stateId" => engineInvokeResponse.stateId, "stateToken" => engineInvokeResponse.stateToken, "currentMapElementId" => engineInvokeResponse.currentMapElementId, "invokeType" => invokeType, "geoLocation" => { "latitude" => 0, "longitude" => 0, "accuracy" => 0, "altitude" => 0, "altitudeAccuracy" => 0, "heading" => 0, "speed" => 0 }, "mapElementInvokeRequest" => { "selectedOutcomeId" => selectedOutcomeId }, "mode" => nil } return EngineInvokeRequest.new(engineInvokeJSON) end return false end |
#reset ⇒ Object
36 37 38 39 |
# File 'lib/manywho.rb', line 36 def reset @TenantUID = false @LoginToken = false end |
#select_OutcomeResponse(engineInvokeResponse, outcomeResponseDeveloperName, invokeType = "FORWARD") ⇒ Object
Select an outcomeResponse, and get the next EngineInvokeResponse
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/manywho.rb', line 258 def select_OutcomeResponse(engineInvokeResponse, outcomeResponseDeveloperName, invokeType="FORWARD") # Ensure that all arguments are valid if ( is_class(engineInvokeResponse, EngineInvokeResponse, "select_OutcomeResponse", 1) ) && ( is_class(outcomeResponseDeveloperName, String, "select_OutcomeResponse", 2) ) && ( is_class(invokeType, String, "select_OutcomeResponse", "invokeType") ) # Get the ID of the selected outcome, using the outcome's developerName selectedOutcomeId = nil engineInvokeResponse.mapElementInvokeResponses[0].outcomeResponses.each do |outcomeResp| if (outcomeResp.developerName == outcomeResponseDeveloperName) selectedOutcomeId = outcomeResp.id end end # Create the EngineInvokeRequest from the EngineInvokeResponse engineInvokeRequest = recreate_EngineInvokeRequest(engineInvokeResponse, selectedOutcomeId) # Return a new EngineInvokeResponse, created from data received from the server return get_EngineInvokeResponse( engineInvokeRequest ) end return false end |
#set_tenant(tenantUniqueID) ⇒ Object
This method sets the Tenant Unique ID
42 43 44 45 46 |
# File 'lib/manywho.rb', line 42 def set_tenant(tenantUniqueID) if ( is_valid_id(tenantUniqueID, "tenantUniqueId") ) @TenantUID = tenantUniqueID end end |