maybe_client 1.1.2 → 1.2
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 +4 -4
- data/lib/maybe_client.rb +16 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4de8ef0b57f0b513d1864406a35f9acd709d8ce0
|
4
|
+
data.tar.gz: 88928a37c7299adee47b324de61fcfbcb395b06c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8255d97b3dd445d8d10e484c38f6f450391345ffebcf592e830d3296cba7464d0d6288420a737ad5a06de63bb55c893ee59d648e914d23d07099a37b485e4af8
|
7
|
+
data.tar.gz: 1aba696735ab9e9d5c20792719e3667c1c3524c5ce9e7c22dcd5af01511e92b6b9faed9c9afdecd327a9ffc2d8ab635be73368d86c9eae0e7bdb8074d9aa1603
|
data/lib/maybe_client.rb
CHANGED
@@ -2,14 +2,12 @@ class MaybeClient
|
|
2
2
|
DELAY = 60
|
3
3
|
|
4
4
|
def initialize(client: nil, client_class: nil, connect_params: nil)
|
5
|
-
raise ArgumentError.new('Either client or client_class has to be supplied') \
|
6
|
-
if !client && !client_class
|
7
|
-
|
8
5
|
@client = client
|
9
6
|
@connect_params = connect_params
|
10
7
|
@client_class = client_class
|
8
|
+
@should_initialize = !!@client_class
|
11
9
|
|
12
|
-
initialize_client
|
10
|
+
initialize_client if client_initialization_needed?
|
13
11
|
end
|
14
12
|
|
15
13
|
def respond_to? method
|
@@ -19,8 +17,8 @@ class MaybeClient
|
|
19
17
|
# Used to delegate everything to @client
|
20
18
|
def method_missing(method, *args, &block)
|
21
19
|
return if noop?
|
22
|
-
initialize_client
|
23
|
-
return if
|
20
|
+
initialize_client if client_initialization_needed?
|
21
|
+
return if in_delay?
|
24
22
|
|
25
23
|
# Raises NoMethodError
|
26
24
|
super unless @client.respond_to? method
|
@@ -39,6 +37,14 @@ class MaybeClient
|
|
39
37
|
|
40
38
|
private
|
41
39
|
def noop?
|
40
|
+
no_client? || in_delay?
|
41
|
+
end
|
42
|
+
|
43
|
+
def no_client?
|
44
|
+
!@client && !@should_initialize
|
45
|
+
end
|
46
|
+
|
47
|
+
def in_delay?
|
42
48
|
@fail_at && @fail_at + DELAY > Time.now
|
43
49
|
end
|
44
50
|
|
@@ -50,6 +56,10 @@ class MaybeClient
|
|
50
56
|
exception_handler(e)
|
51
57
|
end
|
52
58
|
|
59
|
+
def client_initialization_needed?
|
60
|
+
!@client && @should_initialize
|
61
|
+
end
|
62
|
+
|
53
63
|
def initialize_client
|
54
64
|
begin
|
55
65
|
@client = @client_class.new(@connect_params)
|