class RubyCpu

Constants

LANGUAGE
LIBRARY_PATH
OCPU_PATH

Public Class Methods

new(serviceLocation) click to toggle source
# File lib/ruby_cpu.rb, line 18
def initialize serviceLocation
  
  # Remove the trailing slash, if present
  serviceLocation.trim! "/"
  
  @service_location = serviceLocation
  
end

Public Instance Methods

calculate(calculation_request) click to toggle source

Can handle a certain calculation request. The calculation_request passed to this function is sent to the OpenCPU service. This function returns the URLS on which the data can be retrieved.

# File lib/ruby_cpu.rb, line 63
def calculate calculation_request
  
  json_data = JSON.parse(calculation_request.data)

  result = RestClient.post [@service_location, OCPU_PATH, LIBRARY_PATH, calculation_request.package, LANGUAGE, calculation_request.function].join('/'), calculation_request.data , :content_type => :json, :accept => :json
  
  OcpuCallback.build_from_response result
  
end
calculate_and_retrieve(calculation_request) click to toggle source

Calls the given server, with the calculation_request and directly returns the data

# File lib/ruby_cpu.rb, line 51
def calculate_and_retrieve calculation_request
  
  data = calculate calculation_request
  
  retrieve_result data, ReturnTypeEnum::JSON

end
info(package) click to toggle source

Returns information about a given package

# File lib/ruby_cpu.rb, line 41
def info package

        data = RestClient.get [@service_location, OCPU_PATH, LIBRARY_PATH, package.name].join("/")
        
        data
        
end
library() click to toggle source

Returns a list of package objects, retrieved from the OpenCPU server

# File lib/ruby_cpu.rb, line 29
def library
  
 # Rails.logger.debug([@service_location, OCPU_PATH, LIBRARY_PATH].join("/"))
  
  packages = RestClient.get [@service_location, OCPU_PATH, LIBRARY_PATH].join("/")
  
  OcpuPackage.build_from_list packages
  
end
retrieve_result(session, return_type) click to toggle source

Retrieves the result from the given session in the given datatype.

OpenCPU first wants to store the data locally, in order to improve caching. When the call is performed, one gets a callback URL on which a get request should be performed. This is done in this method.

# File lib/ruby_cpu.rb, line 79
def retrieve_result session, return_type 
  
  result = "{}"
  
  result = JSON.parse(RestClient.get [@service_location, session.value, return_type].join("/")) if session.is_a? OcpuCallback
  
  result
  
end