ruby_cpu 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/ruby_cpu.rb +89 -0
- data/lib/ruby_cpu/core_ext.rb +3 -0
- data/lib/ruby_cpu/core_ext/json.rb +1 -0
- data/lib/ruby_cpu/core_ext/json/is_json.rb +10 -0
- data/lib/ruby_cpu/core_ext/string.rb +1 -0
- data/lib/ruby_cpu/core_ext/string/trim.rb +9 -0
- data/lib/ruby_cpu/enums/return_type_enum.rb +4 -0
- data/lib/ruby_cpu/models/calculation_request.rb +7 -0
- data/lib/ruby_cpu/models/ocpu_callback.rb +42 -0
- data/lib/ruby_cpu/models/ocpu_package.rb +22 -0
- data/spec/lib/core_ext/json/is_json_spec.rb +20 -0
- data/spec/lib/core_ext/string/trim_spec.rb +33 -0
- data/spec/lib/ruby_cpu_spec.rb +67 -0
- data/spec/spec_helper.rb +31 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f4689d3e9e79efaafe6d82fd2b114e8d552255ce
|
4
|
+
data.tar.gz: 68d1e03872e89aa472bf081e81363938c5b2bc5b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9ccdf9c7818328bdf0fb8057067a0c0e6db14d21e5a0d67e3964eb4cce32c7f98d8e2191acd626fc6843519893637c2b0eb87f4ca055341b299cc949f3d498ce
|
7
|
+
data.tar.gz: e3430f8651f5db50f8c310369dfa573b5e2955d54fd2c308b2a3d43acea63b721c5f2b88f1c12d2d5ca35f36d65f0968fdbaf405039c47f91c0ab4232c5e0131
|
data/lib/ruby_cpu.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
class RubyCpu
|
2
|
+
#include WebserviceHelper
|
3
|
+
require 'rest_client'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
require 'ruby_cpu/models/calculation_request'
|
7
|
+
require 'ruby_cpu/models/ocpu_callback'
|
8
|
+
require 'ruby_cpu/models/ocpu_package'
|
9
|
+
|
10
|
+
require 'ruby_cpu/core_ext'
|
11
|
+
|
12
|
+
require 'ruby_cpu/enums/return_type_enum'
|
13
|
+
|
14
|
+
OCPU_PATH = "ocpu"
|
15
|
+
LIBRARY_PATH = "library"
|
16
|
+
LANGUAGE = "R"
|
17
|
+
|
18
|
+
def initialize serviceLocation
|
19
|
+
|
20
|
+
# Remove the trailing slash, if present
|
21
|
+
serviceLocation.trim! "/"
|
22
|
+
|
23
|
+
@service_location = serviceLocation
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
##
|
28
|
+
# Returns a list of package objects, retrieved from the OpenCPU server
|
29
|
+
def library
|
30
|
+
|
31
|
+
# Rails.logger.debug([@service_location, OCPU_PATH, LIBRARY_PATH].join("/"))
|
32
|
+
|
33
|
+
packages = RestClient.get [@service_location, OCPU_PATH, LIBRARY_PATH].join("/")
|
34
|
+
|
35
|
+
OcpuPackage.build_from_list packages
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# Returns information about a given +package+
|
41
|
+
def info package
|
42
|
+
|
43
|
+
data = RestClient.get [@service_location, OCPU_PATH, LIBRARY_PATH, package.name].join("/")
|
44
|
+
|
45
|
+
data
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
##
|
50
|
+
# Calls the given server, with the +calculation_request+ and directly returns the data
|
51
|
+
def calculate_and_retrieve calculation_request
|
52
|
+
|
53
|
+
data = calculate calculation_request
|
54
|
+
|
55
|
+
retrieve_result data, ReturnTypeEnum::JSON
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
##
|
60
|
+
# Can handle a certain calculation request. The +calculation_request+ passed
|
61
|
+
# to this function is sent to the OpenCPU service.
|
62
|
+
# This function returns the URLS on which the data can be retrieved.
|
63
|
+
def calculate calculation_request
|
64
|
+
|
65
|
+
json_data = JSON.parse(calculation_request.data)
|
66
|
+
|
67
|
+
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
|
68
|
+
|
69
|
+
OcpuCallback.build_from_response result
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# Retrieves the result from the given +session+ in the given +datatype+.
|
75
|
+
#
|
76
|
+
# OpenCPU first wants to store the data locally, in order to improve caching.
|
77
|
+
# When the call is performed, one gets a callback URL on which a get request
|
78
|
+
# should be performed. This is done in this method.
|
79
|
+
def retrieve_result session, return_type
|
80
|
+
|
81
|
+
result = "{}"
|
82
|
+
|
83
|
+
result = JSON.parse(RestClient.get [@service_location, session.value, return_type].join("/")) if session.is_a? OcpuCallback
|
84
|
+
|
85
|
+
result
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'ruby_cpu/core_ext/json/is_json'
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'ruby_cpu/core_ext/string/trim'
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class OcpuCallback
|
2
|
+
|
3
|
+
attr_accessor :value
|
4
|
+
attr_accessor :stdout
|
5
|
+
attr_accessor :source
|
6
|
+
attr_accessor :console
|
7
|
+
attr_accessor :info
|
8
|
+
attr_accessor :session_code
|
9
|
+
|
10
|
+
VALUE_LOCATION = 0
|
11
|
+
STDOUT_LOCATION = 1
|
12
|
+
SOURCE_LOCATION = 2
|
13
|
+
CONSOLE_LOCATION = 3
|
14
|
+
INFO_LOCATION = 4
|
15
|
+
|
16
|
+
def self.build_from_response response_urls
|
17
|
+
|
18
|
+
response_urls = response_urls.split("\n")
|
19
|
+
|
20
|
+
ocpu_callback = new
|
21
|
+
|
22
|
+
if response_urls.length >= 5 then
|
23
|
+
|
24
|
+
ocpu_callback.value = response_urls[OcpuCallback::VALUE_LOCATION].trim "/"
|
25
|
+
|
26
|
+
ocpu_callback.stdout = response_urls[OcpuCallback::STDOUT_LOCATION].trim "/"
|
27
|
+
|
28
|
+
ocpu_callback.source = response_urls[OcpuCallback::SOURCE_LOCATION].trim "/"
|
29
|
+
|
30
|
+
ocpu_callback.console = response_urls[OcpuCallback::CONSOLE_LOCATION].trim "/"
|
31
|
+
|
32
|
+
ocpu_callback.info = response_urls[OcpuCallback::INFO_LOCATION].trim "/"
|
33
|
+
|
34
|
+
ocpu_callback.session_code = ocpu_callback.value.split("/")[3]
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
ocpu_callback
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class OcpuPackage
|
2
|
+
|
3
|
+
attr_accessor :name
|
4
|
+
|
5
|
+
def self.build_from_list package_list
|
6
|
+
|
7
|
+
package_list = package_list.split(" ")
|
8
|
+
|
9
|
+
packages = Array.new
|
10
|
+
|
11
|
+
package_list.each do |package|
|
12
|
+
current_package = new
|
13
|
+
current_package.name = package
|
14
|
+
packages << current_package
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
packages
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe JSON do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@json = '{"x":[1,4,2,3,5,3], "y":[1,6,7,5,4,3]}'
|
7
|
+
@not_json= "{x:[1,2,3,4],y:[4,3,2,1]}"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should detect a string as being valid json" do
|
11
|
+
result = JSON.is_json? @json
|
12
|
+
result.should eq true
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should detect a string as being invalid json" do
|
16
|
+
result = JSON.is_json? @not_json
|
17
|
+
result.should eq false
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe String do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@data = "test"
|
7
|
+
@data_pre = "/#{@data}"
|
8
|
+
@data_ap = "#{@data}/"
|
9
|
+
@data_both = "/#{@data}/"
|
10
|
+
@data_both_multi = "/#{@data}/#{@data}/#{@data}/#{@data}/"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "remove a prepended char" do
|
14
|
+
result = @data_pre.trim "/"
|
15
|
+
result.should eq @data
|
16
|
+
end
|
17
|
+
|
18
|
+
it "remove a apended char" do
|
19
|
+
result = @data_ap.trim "/"
|
20
|
+
result.should eq @data
|
21
|
+
end
|
22
|
+
|
23
|
+
it "remove both pre- and apended char" do
|
24
|
+
result = @data_both.trim "/"
|
25
|
+
result.should eq @data
|
26
|
+
end
|
27
|
+
|
28
|
+
it "remove not remove inner chars" do
|
29
|
+
result = @data_both_multi.trim "/"
|
30
|
+
result.should eq "#{@data}/#{@data}/#{@data}/#{@data}"
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "An instance of", RubyCpu do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@calculation_request = CalculationRequest.new
|
7
|
+
@calculation_request.package = "stats"
|
8
|
+
@calculation_request.function = "cor"
|
9
|
+
@calculation_request.data = '{"x":[1,4,2,3,5,3], "y":[1,6,7,5,4,3]}'
|
10
|
+
@correlation = [0.32733]
|
11
|
+
|
12
|
+
@ocpu_package = OcpuPackage.new
|
13
|
+
@ocpu_package.name = @calculation_request.package
|
14
|
+
end
|
15
|
+
|
16
|
+
before :each do
|
17
|
+
@ruby_cpu = RubyCpu.new "https://public.opencpu.org"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should be properly initialized" do
|
21
|
+
expect(@ruby_cpu).to be_a(RubyCpu)
|
22
|
+
expect(@ocpu_package).to be_a(OcpuPackage)
|
23
|
+
expect(@calculation_request).to be_a(CalculationRequest)
|
24
|
+
end
|
25
|
+
|
26
|
+
context "success" do
|
27
|
+
|
28
|
+
it "returns information of a package" do
|
29
|
+
data = @ruby_cpu.info @ocpu_package
|
30
|
+
data.should be_a(String)
|
31
|
+
data.should include "Information on package '#{@ocpu_package.name}'"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "returns a list of packages" do
|
35
|
+
|
36
|
+
result = @ruby_cpu.library
|
37
|
+
expect(result).to be_a(Array)
|
38
|
+
expect(result).not_to be_empty
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returns an filled OcpuCallback object" do
|
43
|
+
|
44
|
+
@callback = @ruby_cpu.calculate @calculation_request
|
45
|
+
expect(@callback).to be_a(OcpuCallback)
|
46
|
+
expect(@callback.source).not_to be_empty
|
47
|
+
expect(@callback.value).not_to be_empty
|
48
|
+
expect(@callback.stdout).not_to be_empty
|
49
|
+
expect(@callback.console).not_to be_empty
|
50
|
+
expect(@callback.info).not_to be_empty
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should retrieve the correct results" do
|
55
|
+
@callback = @ruby_cpu.calculate @calculation_request
|
56
|
+
result = @ruby_cpu.retrieve_result @callback, "json"
|
57
|
+
expect(result).to eq(@correlation)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should calculate the correlation in one go" do
|
61
|
+
result = @ruby_cpu.calculate_and_retrieve @calculation_request
|
62
|
+
expect(result).to eq(@correlation)
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
2
|
+
require 'rspec'
|
3
|
+
require 'rspec/autorun'
|
4
|
+
require 'ruby_cpu'
|
5
|
+
|
6
|
+
# Setup coveralls
|
7
|
+
require 'coveralls'
|
8
|
+
Coveralls.wear!
|
9
|
+
|
10
|
+
# Checks for pending migrations before tests are run.
|
11
|
+
# If you are not using ActiveRecord, you can remove this line.
|
12
|
+
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
|
13
|
+
|
14
|
+
RSpec.configure do |config|
|
15
|
+
# ## Mock Framework
|
16
|
+
#
|
17
|
+
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
|
18
|
+
#
|
19
|
+
# config.mock_with :mocha
|
20
|
+
# config.mock_with :flexmock
|
21
|
+
# config.mock_with :rr
|
22
|
+
|
23
|
+
config.color_enabled = true
|
24
|
+
config.formatter = 'documentation'
|
25
|
+
|
26
|
+
# Run specs in random order to surface order dependencies. If you find an
|
27
|
+
# order dependency and want to debug it, you can fix the order by providing
|
28
|
+
# the seed, which is printed after each run.
|
29
|
+
# --seed 1234
|
30
|
+
config.order = "random"
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_cpu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Frank Blaauw
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.14.1
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.14.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: coveralls
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: A gem which exposes the OpenCPU interface to ruby
|
42
|
+
email: frank.blaauw@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- lib/ruby_cpu/core_ext/json/is_json.rb
|
48
|
+
- lib/ruby_cpu/core_ext/json.rb
|
49
|
+
- lib/ruby_cpu/core_ext/string/trim.rb
|
50
|
+
- lib/ruby_cpu/core_ext/string.rb
|
51
|
+
- lib/ruby_cpu/core_ext.rb
|
52
|
+
- lib/ruby_cpu/enums/return_type_enum.rb
|
53
|
+
- lib/ruby_cpu/models/calculation_request.rb
|
54
|
+
- lib/ruby_cpu/models/ocpu_callback.rb
|
55
|
+
- lib/ruby_cpu/models/ocpu_package.rb
|
56
|
+
- lib/ruby_cpu.rb
|
57
|
+
- spec/lib/core_ext/json/is_json_spec.rb
|
58
|
+
- spec/lib/core_ext/string/trim_spec.rb
|
59
|
+
- spec/lib/ruby_cpu_spec.rb
|
60
|
+
- spec/spec_helper.rb
|
61
|
+
homepage: http://frbl.eu
|
62
|
+
licenses:
|
63
|
+
- none
|
64
|
+
metadata: {}
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 2.0.7
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: RubyCPU
|
85
|
+
test_files:
|
86
|
+
- spec/lib/core_ext/json/is_json_spec.rb
|
87
|
+
- spec/lib/core_ext/string/trim_spec.rb
|
88
|
+
- spec/lib/ruby_cpu_spec.rb
|
89
|
+
- spec/spec_helper.rb
|