class Manywho::Engine

Public Class Methods

new() click to toggle source

Initialize instance variables

# File lib/manywho.rb, line 32
def initialize
    reset()
end

Public Instance Methods

create_EngineInitializationRequest(flowResponse, annotations = nil, inputs = [], mode = nil) click to toggle source

Creates an EngineInitializationRequest to be sent to the server using get_EngineInitializationResponse

# 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") click to toggle source

Create an EngineInvokeRequest to be posted to the server using get_EngineInvokeResponse

# 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) click to toggle source

Gets an EngineInitializationResponse from the server, using a HTTP POST request.

# 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) click to toggle source

Post the EngineInvokeRequest, and return the EngineInvokeResponse

# 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) click to toggle source

Gets a FlowResponse object from the server for the provided ID

# 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) click to toggle source

Tests if a value is of a specified class type, otherwise raises an error

# 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) click to toggle source

Tests if a request has completed successfully, otherwise raises an error

# 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) click to toggle source

Tests if an id is valid, otherwise raises an error

# 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="") click to toggle source

Load a flow, given the tenantId, flowId and logon details the first EngineInvokeResponse will be returned

# 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.authorizationContext.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) click to toggle source

Logs in to the flow. Flows may require you to log in, this only has to be done once before executing the flow

# 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.authorizationContext.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") click to toggle source

Create a EngineInvokeRequest from an engineInvokeResponse - such as when an outcome is selected, and a new EngineInvokeRequest is required

# 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() click to toggle source
# File lib/manywho.rb, line 36
def reset
    @TenantUID = false
    @LoginToken = false
end
select_OutcomeResponse(engineInvokeResponse, outcomeResponseDeveloperName, invokeType="FORWARD") click to toggle source

Select an outcomeResponse, and get the next EngineInvokeResponse

# 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) click to toggle source

This method sets the Tenant Unique ID

# File lib/manywho.rb, line 42
    def set_tenant(tenantUniqueID)
if ( is_valid_id(tenantUniqueID, "tenantUniqueId") )
    @TenantUID = tenantUniqueID
end
    end