opencpu 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 37fa420600c3c0b203df369157539613a264c712
4
- data.tar.gz: ab9735f30154946995ac41eb95ca80ba861db227
3
+ metadata.gz: 34c952212689a1551ecfcb120620c509f11e8524
4
+ data.tar.gz: e7962d79b6ffca4407d2fb677ca8703e5ff8ed57
5
5
  SHA512:
6
- metadata.gz: d19c144faef4d6ed4a4c1840056b73e103ef99e76bf433b27b98da73b40818b8f577d5fc306e93db3821e06c897b44cc4fe19919ab9288b0bd24dcd7ca0a67ae
7
- data.tar.gz: b445cdb1fbf2eecb6bc2f2168be8bff03718968089c83903370743c4c4472301f2e11481f13ea6909a18fdcf467a74717bd494287b44caa3208586a22859bec9
6
+ metadata.gz: 813129283d4c47ef0da68327772940d2a715eeac3111e11ce97766a590cad61beb5f9e9f33b4217c5291e3155ef2952771a05fe9e266eb175746e54c5d844616
7
+ data.tar.gz: 5f1ee6a0a9948020c5d058cf688988cec3d4d5f71f99ca25940b063cabb5bc279332a2bc56bfbbaef7d32d003a4096102c29379af40a4940db6d17b27a66393b
@@ -23,9 +23,10 @@ module OpenCPU
23
23
  private
24
24
 
25
25
  def process_query(url, data, &block)
26
+ return fake_response_for(url) if OpenCPU.test_mode?
26
27
  options = { body: data.to_json, headers: { "Content-Type" => 'application/json' } }
27
28
  response = self.class.post(url, options)
28
-
29
+
29
30
  case response.code
30
31
  when 200..201
31
32
  return yield(response)
@@ -37,7 +38,18 @@ module OpenCPU
37
38
  end
38
39
 
39
40
  def package_url(package, function, format = nil)
40
- "/library/#{package.to_s}/R/#{function.to_s}/#{format.to_s}"
41
+ ['', 'library', package, 'R', function, format.to_s].join('/')
42
+ end
43
+
44
+ def fake_response_for(url)
45
+ key = derive_key_from_url(url)
46
+ OpenCPU.configuration.fake_responses.delete key
47
+ end
48
+
49
+ def derive_key_from_url(url)
50
+ url_parts = url.gsub!(/^\//, '').split('/')
51
+ remove_items = ['R', 'library', 'json']
52
+ (url_parts - remove_items).join('/')
41
53
  end
42
54
  end
43
55
  end
@@ -3,5 +3,19 @@ module OpenCPU
3
3
  attr_accessor :endpoint_url
4
4
  attr_accessor :username
5
5
  attr_accessor :password
6
+ attr_accessor :mode
7
+ attr_accessor :fake_responses
8
+
9
+ def initialize
10
+ @fake_responses = {}
11
+ end
12
+
13
+ def add_fake_response(key, response)
14
+ @fake_responses[key] = response
15
+ end
16
+
17
+ def remove_fake_response(key)
18
+ @fake_responses.delete key
19
+ end
6
20
  end
7
21
  end
@@ -1,3 +1,6 @@
1
1
  module OpenCPU
2
- VERSION = "0.5.0"
2
+ MAJOR = 0
3
+ MINOR = 6
4
+ TINY = 0
5
+ VERSION = [MAJOR, MINOR, TINY].join('.')
3
6
  end
data/lib/opencpu.rb CHANGED
@@ -8,7 +8,6 @@ end
8
8
 
9
9
 
10
10
  module OpenCPU
11
-
12
11
  class << self
13
12
  attr_writer :configuration
14
13
  end
@@ -24,6 +23,25 @@ module OpenCPU
24
23
  def self.configure
25
24
  yield(configuration)
26
25
  end
26
+
27
+ def self.enable_test_mode!
28
+ self.configuration.fake_responses = {}
29
+ self.configuration.mode = 'test'
30
+ end
31
+
32
+ def self.disable_test_mode!
33
+ self.configuration.mode = nil
34
+ self.configuration.fake_responses = {}
35
+ end
36
+
37
+ def self.test_mode?
38
+ self.configuration.mode == 'test'
39
+ end
40
+
41
+ def self.set_fake_response!(package, script, response = nil)
42
+ key = [package, script].join('/')
43
+ self.configuration.add_fake_response key, response
44
+ end
27
45
  end
28
46
 
29
47
 
@@ -50,6 +50,9 @@ describe OpenCPU::Client do
50
50
  end
51
51
 
52
52
  describe '#execute' do
53
+ after do
54
+ OpenCPU.disable_test_mode!
55
+ end
53
56
  let(:client) { described_class.new }
54
57
 
55
58
  it 'is used to quickly return JSON results' do
@@ -71,6 +74,29 @@ describe OpenCPU::Client do
71
74
  expect { client.execute(:digest, :hmac) }.to raise_error
72
75
  end
73
76
  end
77
+
78
+ context 'when in test mode' do
79
+ it 'has an empty fake response when just enabled' do
80
+ OpenCPU.enable_test_mode!
81
+ response = client.execute(:digest, :hmac, key: 'baz', object: 'qux')
82
+ expect(response).to be_nil
83
+ end
84
+
85
+ context 'setting fake responses' do
86
+ before do
87
+ OpenCPU.enable_test_mode!
88
+ OpenCPU.set_fake_response! :digest, :hmac, 'response'
89
+ OpenCPU.set_fake_response! :animation, 'flip.coin', {response: 2}
90
+ OpenCPU.set_fake_response! :animation, 'flip.test', {response: 3}
91
+ end
92
+
93
+ it 'returns a fake response per R-script' do
94
+ expect(client.execute(:digest, :hmac)).to eq 'response'
95
+ expect(client.execute(:animation, 'flip.coin')).to eq({response: 2})
96
+ expect(client.execute(:animation, 'flip.test')).to eq({response: 3})
97
+ end
98
+ end
99
+ end
74
100
  end
75
101
 
76
102
  end
@@ -14,6 +14,16 @@ describe OpenCPU::Configuration do
14
14
  expect(described_class.new).to respond_to :password
15
15
  end
16
16
 
17
+ it 'has a #mode' do
18
+ expect(described_class.new).to respond_to :mode
19
+ end
20
+
21
+ it 'defines #fake_responses Hash' do
22
+ config = described_class.new
23
+ expect(config).to respond_to :fake_responses
24
+ expect(config.fake_responses).to be_a Hash
25
+ end
26
+
17
27
  describe '#endpoint_url=' do
18
28
  it 'sets an url' do
19
29
  config = described_class.new
@@ -37,4 +47,29 @@ describe OpenCPU::Configuration do
37
47
  expect(config.password).to eq 'bar'
38
48
  end
39
49
  end
50
+
51
+ describe '#mode=' do
52
+ it 'sets the mode to test' do
53
+ config = described_class.new
54
+ config.mode = 'test'
55
+ expect(config.mode).to eq 'test'
56
+ end
57
+ end
58
+
59
+ describe '#fake_responses' do
60
+ let(:config) { described_class.new }
61
+ it 'sets an empty fake responses' do
62
+ expect(config.fake_responses).to eq({})
63
+ end
64
+
65
+ it 'sets fake responses for R packages' do
66
+ config.add_fake_response 'foo/bar', {baz: 'qux'}
67
+ expect(config.fake_responses).to eq({'foo/bar' => {baz: 'qux'}})
68
+ end
69
+
70
+ it 'removes a fake response by key' do
71
+ config.remove_fake_response 'foo/bar'
72
+ expect(config.fake_responses).to eq({})
73
+ end
74
+ end
40
75
  end
@@ -14,6 +14,14 @@ describe OpenCPU do
14
14
  expect(described_class).to respond_to :configuration
15
15
  end
16
16
 
17
+ it "responds to #enable_test_mode" do
18
+ expect(described_class).to respond_to :enable_test_mode!
19
+ end
20
+
21
+ it "responds to #disable_test_mode" do
22
+ expect(described_class).to respond_to :disable_test_mode!
23
+ end
24
+
17
25
  describe '#configure' do
18
26
  before do
19
27
  described_class.configure do |config|
@@ -26,4 +34,36 @@ describe OpenCPU do
26
34
  end
27
35
  end
28
36
 
37
+ describe 'test mode' do
38
+ after do
39
+ described_class.disable_test_mode!
40
+ end
41
+ it 'enables test mode' do
42
+ expect(described_class.configuration.mode).to be_nil
43
+ described_class.enable_test_mode!
44
+ expect(described_class.configuration.mode).to eq 'test'
45
+ end
46
+
47
+ it 'disables test mode' do
48
+ described_class.enable_test_mode!
49
+ expect(described_class.configuration.mode).to eq 'test'
50
+ described_class.disable_test_mode!
51
+ expect(described_class.configuration.mode).to be_nil
52
+ end
53
+
54
+ it 'checks for a test mode' do
55
+ described_class.enable_test_mode!
56
+ expect(described_class.test_mode?).to be_true
57
+ described_class.disable_test_mode!
58
+ expect(described_class.test_mode?).to be_false
59
+ end
60
+
61
+ it 'can set a fake response' do
62
+ described_class.enable_test_mode!
63
+ described_class.set_fake_response!('foo', 'bar', {baz: 'qux'})
64
+ described_class.set_fake_response!('hoi', 'hai')
65
+ expect(OpenCPU.configuration.fake_responses['foo/bar']).to eq({baz: 'qux'})
66
+ expect(OpenCPU.configuration.fake_responses['hoi/hai']).to be_nil
67
+ end
68
+ end
29
69
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opencpu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Malykh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-01 00:00:00.000000000 Z
11
+ date: 2014-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yajl-ruby