cubits 0.6.1 → 0.7.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: 4773f39e2f12ee25f1bbb2ac028cb60a998c9168
4
- data.tar.gz: cc3da9ea18047f888b9d6c40090778aeda046454
3
+ metadata.gz: 5f2978ac613f0cf68c3b4f7df8034d0899af48ee
4
+ data.tar.gz: dddd910c78caded6f60d9f9676ce7f8ff9038403
5
5
  SHA512:
6
- metadata.gz: 6836e10c834ee5479f834a35f617f5ae9efc7dcae03c402e5b55da38d92ea77aad46e790df06776d0557505b3cf9625478dd2f14409455ef13937d24ad067458
7
- data.tar.gz: d7a1182726a2119748c057c6993cc62812c3d73d87f9505d09cb2ce87645b4363fe39a9f2bb64ff1ad34e7d595c6ecf4adf86487c10814d045092f306e527f5d
6
+ metadata.gz: 9f404caa09bcd22b99e09f32702a4ece7f7dc7764248c9549cd3649557dca2fdc98b719cb84dbb6fcb5dd8b0eead2cf4fbac03050b6366591721de58a28b66d8
7
+ data.tar.gz: 674bc505e2cb6614d7751d85558934c001b9621ea21304ffaaa29d383fbd51aa96753bfbffd1182a4acdb42d3c115ed741feca490c41273946ee0a03616c4d61
data/CHANGES.md CHANGED
@@ -1,3 +1,10 @@
1
+ # 0.7.0
2
+
3
+ * Added support of concurrent connections. Use `Cubits.active_connection_key()`,
4
+ `Cubits.active_connection_key=()` and `Cubits.with_connection_key() { ... }`
5
+ to manipulate the connections.
6
+ * The last configured connection is being used by default now.
7
+
1
8
  # 0.6.0
2
9
 
3
10
  * Added Cubits::ResourceCollection, which implements Ruby [Enumerable](http://ruby-doc.org/core-2.2.1/Enumerable.html)
data/README.md CHANGED
@@ -40,6 +40,8 @@ Cubits.configure(key: '***', secret: '***')
40
40
  Cubits.available? # => true
41
41
  ```
42
42
 
43
+ If you configured multiple connections the last one will be used by default. You can get the current active connection key with `Cubits.active_connection_key` and switch between connections with `Cubits.active_connection_key=`. There is also a possibility to temporary switch the connection for a block: `Cubits.with_connection_key() { ... }`
44
+
43
45
  ## Invoices
44
46
 
45
47
  Using the `cubits` Ruby client you can create and retrieve invoices.
@@ -33,16 +33,46 @@ module Cubits
33
33
  #
34
34
  # @param key [String] (optional) Cubits API key of the configured connection
35
35
  #
36
- # @return [Connection] Connection object matching the requested Cubits API key or first
37
- # configured connection
36
+ # @return [Connection] Connection object matching the requested Cubits API key
37
+ # or Connection object matching Cubits.active_connection_key
38
38
  #
39
- def self.connection(key = nil)
40
- @connections ||= {}
41
- c = key ? @connections[key] : @connections.values.first
42
- unless c
39
+ def self.connection(key = active_connection_key)
40
+ if @connections.nil? || @connections[key].nil?
43
41
  fail ConnectionError, "Cubits connection is not configured for key #{key || '(default)'}"
44
42
  end
45
- c
43
+ @connections[key]
44
+ end
45
+
46
+ # Returns current Cubits connection key (Cubits API key)
47
+ #
48
+ # @return [String] current thread-local active connection key
49
+ # or the last configured connection key
50
+ #
51
+ def self.active_connection_key
52
+ Thread.current[:cubits_active_connection_key] || @connections&.keys&.last
53
+ end
54
+
55
+ # Sets current Cubits connection to given Cubits API key
56
+ #
57
+ # @param key [String] Cubits API key of the configured connection
58
+ #
59
+ def self.active_connection_key=(key)
60
+ if @connections.nil? || @connections[key].nil?
61
+ fail ConnectionError, "Cubits connection is not configured for key #{key}"
62
+ end
63
+ Thread.current[:cubits_active_connection_key] = key
64
+ end
65
+
66
+ # Sets current Cubits connection to given Cubits API key and yields given block
67
+ #
68
+ # @param key [String] Cubits API key of the configured connection
69
+ #
70
+ def self.with_connection_key(key)
71
+ connection_key_was = active_connection_key
72
+ self.active_connection_key = key
73
+ yield if block_given?
74
+ ensure
75
+ self.active_connection_key = connection_key_was
46
76
  end
47
77
 
48
78
  # Returns current Logger object
@@ -1,3 +1,3 @@
1
1
  module Cubits
2
- VERSION = '0.6.1'
2
+ VERSION = '0.7.0'
3
3
  end
@@ -4,13 +4,22 @@ describe Cubits do
4
4
  let(:key) { 'blablakey' }
5
5
  let(:secret) { 'blablasecret' }
6
6
 
7
+ let(:another_key) { 'another_cubits_api_key' }
8
+ let(:another_secret) { 'another_cubits_api_secret' }
9
+
10
+ after { Thread.current[:cubits_active_connection_key] = nil }
11
+
7
12
  context '.configure' do
8
13
  it 'fails if key is missing' do
9
- expect { Cubits.configure(secret: secret) }.to raise_error
14
+ expect { Cubits.configure(secret: secret) }.to raise_error(
15
+ ArgumentError, 'String is expected as :key'
16
+ )
10
17
  end
11
18
 
12
19
  it 'fails if secret is missing' do
13
- expect { Cubits.configure(key: key) }.to raise_error
20
+ expect { Cubits.configure(key: key) }.to raise_error(
21
+ ArgumentError, 'String is expected as :secret'
22
+ )
14
23
  end
15
24
 
16
25
  it 'runs without errors if key and secret are present' do
@@ -22,13 +31,23 @@ describe Cubits do
22
31
  subject { Cubits.connection }
23
32
 
24
33
  it 'fails when connection is not configured' do
25
- expect { subject }.to raise_error
34
+ expect { subject }.to raise_error(
35
+ Cubits::ConnectionError, 'Cubits connection is not configured for key (default)'
36
+ )
26
37
  end
27
38
 
28
- it 'returns Connection object when connection is configured' do
39
+ it 'returns Connection object when connection is configured', :aggregate_failures do
29
40
  Cubits.configure(key: key, secret: secret)
30
41
  expect { subject }.to_not raise_error
31
42
  expect(subject).to be_a Cubits::Connection
43
+ expect(subject.instance_variable_get(:@key)).to eql(key)
44
+ end
45
+
46
+ it 'returns Connection object by active connection key', :aggregate_failures do
47
+ Cubits.configure(key: another_key, secret: another_secret)
48
+ expect(Cubits).to receive(:active_connection_key).and_return(another_key)
49
+ expect(subject).to be_a Cubits::Connection
50
+ expect(subject.instance_variable_get(:@key)).to eql(another_key)
32
51
  end
33
52
  end # .connection
34
53
 
@@ -38,4 +57,65 @@ describe Cubits do
38
57
  it { is_expected.to be_a Logger }
39
58
  end # .logger
40
59
 
60
+ describe '.active_connection_key=' do
61
+ it 'does not allow to set the key when there are no configured connections' do
62
+ expect { Cubits.active_connection_key = '123' }.to raise_exception(
63
+ Cubits::ConnectionError, 'Cubits connection is not configured for key 123'
64
+ )
65
+ end
66
+
67
+ it 'does not allow to set the key of non-existent connection' do
68
+ Cubits.configure(key: key, secret: secret)
69
+ expect { Cubits.active_connection_key = '123' }.to raise_exception(
70
+ Cubits::ConnectionError, 'Cubits connection is not configured for key 123'
71
+ )
72
+ end
73
+
74
+ it 'allows to set the key of the existing connection', :aggregate_failures do
75
+ Cubits.configure(key: key, secret: secret)
76
+ Cubits.configure(key: another_key, secret: another_secret)
77
+ expect { Cubits.active_connection_key = key }.to_not raise_exception
78
+ expect { Cubits.active_connection_key = another_key }.to_not raise_exception
79
+ end
80
+
81
+ it 'sets cubits_active_connection_key thread variable' do
82
+ Cubits.configure(key: key, secret: secret)
83
+ Cubits.active_connection_key = key
84
+ expect(Thread.current[:cubits_active_connection_key]).to eql(key)
85
+ end
86
+ end # .active_connection_key=
87
+
88
+ describe '.active_connection_key' do
89
+ it 'returns thread-local connection key if exists' do
90
+ Cubits.configure(key: key, secret: secret)
91
+ Cubits.configure(key: another_key, secret: another_secret)
92
+ Cubits.active_connection_key = key
93
+ expect(Cubits.active_connection_key).to eql(key)
94
+ end
95
+
96
+ it 'returns the last configured connection key otherwise' do
97
+ Cubits.configure(key: key, secret: secret)
98
+ Cubits.configure(key: another_key, secret: another_secret)
99
+ expect(Cubits.active_connection_key).to eql(another_key)
100
+ end
101
+
102
+ it 'returns nil if there are no configured connections' do
103
+ expect(Cubits.active_connection_key).to be_nil
104
+ end
105
+ end # .active_connection_key
106
+
107
+ describe '.with_connection_key' do
108
+ it 'toggles active connection key' do
109
+ Cubits.configure(key: key, secret: secret)
110
+ Cubits.configure(key: another_key, secret: another_secret)
111
+
112
+ Cubits.active_connection_key = key
113
+
114
+ Cubits.with_connection_key(another_key) do
115
+ expect(Cubits.active_connection_key).to eql(another_key)
116
+ end
117
+
118
+ expect(Cubits.active_connection_key).to eql(key)
119
+ end
120
+ end # .with_connection_key
41
121
  end # describe Cubits
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cubits
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Kukushkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-27 00:00:00.000000000 Z
11
+ date: 2018-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -155,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
155
  version: '0'
156
156
  requirements: []
157
157
  rubyforge_project:
158
- rubygems_version: 2.2.2
158
+ rubygems_version: 2.6.11
159
159
  signing_key:
160
160
  specification_version: 4
161
161
  summary: Ruby client for Cubits Merchant API