Class: RubyCpu

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_cpu.rb

Constant Summary collapse

OCPU_PATH =
"ocpu"
LIBRARY_PATH =
"library"
LANGUAGE =
"R"

Instance Method Summary collapse

Constructor Details

#initialize(serviceLocation) ⇒ RubyCpu

Returns a new instance of RubyCpu.



18
19
20
21
22
23
24
25
# File 'lib/ruby_cpu.rb', line 18

def initialize serviceLocation
  
  # Remove the trailing slash, if present
  serviceLocation.trim! "/"
  
  @service_location = serviceLocation
  
end

Instance Method Details

#calculate(calculation_request) ⇒ Object

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.



63
64
65
66
67
68
69
70
71
# 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) ⇒ Object

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



51
52
53
54
55
56
57
# 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) ⇒ Object

Returns information about a given package



41
42
43
44
45
46
47
# File 'lib/ruby_cpu.rb', line 41

def info package

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

#libraryObject

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



29
30
31
32
33
34
35
36
37
# 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) ⇒ Object

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.



79
80
81
82
83
84
85
86
87
# 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