request_my_turn 0.0.1 → 0.0.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/request_my_turn/version.rb +1 -1
- data/lib/request_my_turn.rb +46 -32
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 49bfb4eb8f4642237f380883f8fcd6c037185cbcd798968fe3401fb573ca6e79
|
|
4
|
+
data.tar.gz: 06ccd80467f6dc64b6bb33227fbaa9a7a87b3fb9e4dc54ddccf1eace769ebb1c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d5939532dc27f74e38a209ac9f6e619055c1d78db5dc76d6fec5b3a7bf74763210c2f7d47608a3cff09c920c3bb8a148823dda75ab6b7eefe9eaeb79774b4599
|
|
7
|
+
data.tar.gz: 22af5a57eb80c50490e3500d559b258617bc420332ee3634d615b82b61a6d958a8444b294c82b34b42fc4ce16dbfd8cea6ee9ae51300106b5ef099a31d03b16d
|
data/lib/request_my_turn.rb
CHANGED
|
@@ -9,6 +9,8 @@ require 'uri'
|
|
|
9
9
|
require_relative 'request_my_turn/version'
|
|
10
10
|
|
|
11
11
|
class RequestMyTurn
|
|
12
|
+
attr_accessor :id, :locked
|
|
13
|
+
|
|
12
14
|
class WithoutBlock < StandardError
|
|
13
15
|
def initialize
|
|
14
16
|
super 'This service must be used with `block\'!'
|
|
@@ -35,19 +37,34 @@ class RequestMyTurn
|
|
|
35
37
|
def settings
|
|
36
38
|
@settings ||= OpenStruct.new(
|
|
37
39
|
url: nil,
|
|
38
|
-
|
|
40
|
+
after: nil,
|
|
41
|
+
before: nil,
|
|
42
|
+
switch: true,
|
|
43
|
+
timeout: nil,
|
|
44
|
+
lock_seconds: nil,
|
|
45
|
+
headers: nil,
|
|
46
|
+
ignore_timeout_error: nil
|
|
39
47
|
)
|
|
40
48
|
end
|
|
41
49
|
end
|
|
42
50
|
|
|
51
|
+
%i[url after before switch timeout lock_seconds headers ignore_timeout_error method_added].each do |name|
|
|
52
|
+
define_method(name) do
|
|
53
|
+
value = instance_variable_get "@#{name}"
|
|
54
|
+
result = value.nil? ? self.class.settings[name] : value
|
|
55
|
+
result.is_a?(Proc) ? result.call(self) : result
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
43
59
|
def initialize(queue_name, **options)
|
|
44
60
|
@queue_name = queue_name
|
|
45
61
|
|
|
46
62
|
@url = options[:url]
|
|
47
63
|
@after = options[:after]
|
|
48
64
|
@before = options[:before]
|
|
65
|
+
@switch = options[:switch]
|
|
49
66
|
@timeout = options[:timeout]
|
|
50
|
-
@lock_seconds = options[:lock_seconds]
|
|
67
|
+
@lock_seconds = options[:lock_seconds] || 60
|
|
51
68
|
@headers = options[:headers]
|
|
52
69
|
@ignore_timeout_error = options[:ignore_timeout_error]
|
|
53
70
|
end
|
|
@@ -56,27 +73,33 @@ class RequestMyTurn
|
|
|
56
73
|
raise WithoutBlock unless block
|
|
57
74
|
return yield unless switched_on?
|
|
58
75
|
|
|
59
|
-
id = take_my_turn
|
|
60
|
-
|
|
76
|
+
self.id = take_my_turn
|
|
77
|
+
before.call(id) if valid_callback? before
|
|
61
78
|
|
|
62
79
|
result = yield
|
|
63
80
|
return result unless present? id
|
|
64
81
|
|
|
65
|
-
locked = leave_my_turn(id)
|
|
66
|
-
|
|
82
|
+
self.locked = leave_my_turn(id)
|
|
83
|
+
after.call(locked) if valid_callback? after
|
|
67
84
|
result
|
|
68
85
|
end
|
|
69
86
|
|
|
70
87
|
private
|
|
71
88
|
|
|
72
89
|
def switched_on?
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
present? current_url
|
|
90
|
+
switch
|
|
76
91
|
end
|
|
77
92
|
|
|
78
|
-
def
|
|
79
|
-
self.
|
|
93
|
+
def take_my_turn
|
|
94
|
+
timeout = self.timeout
|
|
95
|
+
response = Timeout.timeout(timeout.to_f) do
|
|
96
|
+
request "#{current_url}/#{@queue_name}?seconds=#{lock_seconds}"
|
|
97
|
+
rescue Timeout::Error
|
|
98
|
+
return false if ignore_timeout_error
|
|
99
|
+
|
|
100
|
+
raise TimeoutError, timeout
|
|
101
|
+
end
|
|
102
|
+
read_response(response, :id)
|
|
80
103
|
end
|
|
81
104
|
|
|
82
105
|
def current_url
|
|
@@ -84,23 +107,10 @@ class RequestMyTurn
|
|
|
84
107
|
end
|
|
85
108
|
|
|
86
109
|
def build_url
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
raise InvalidUrl, url unless url =~ URI::DEFAULT_PARSER.make_regexp
|
|
91
|
-
|
|
92
|
-
url
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
def take_my_turn
|
|
96
|
-
response = Timeout.timeout(@timeout.to_f) do
|
|
97
|
-
request "#{current_url}/#{@queue_name}?seconds=#{@lock_seconds}"
|
|
98
|
-
rescue Timeout::Error
|
|
99
|
-
return false if @ignore_timeout_error
|
|
110
|
+
current_url = url
|
|
111
|
+
raise InvalidUrl, current_url unless current_url =~ URI::DEFAULT_PARSER.make_regexp
|
|
100
112
|
|
|
101
|
-
|
|
102
|
-
end
|
|
103
|
-
read_response(response, :id)
|
|
113
|
+
current_url
|
|
104
114
|
end
|
|
105
115
|
|
|
106
116
|
def request(url, method: :get)
|
|
@@ -109,10 +119,14 @@ class RequestMyTurn
|
|
|
109
119
|
https.use_ssl = url.start_with? 'https'
|
|
110
120
|
|
|
111
121
|
request = method == :delete ? Net::HTTP::Delete.new(uri) : Net::HTTP::Get.new(uri)
|
|
112
|
-
|
|
122
|
+
current_headers.each { |key, value| request[key] = value } if current_headers.is_a?(Hash)
|
|
113
123
|
https.request(request)
|
|
114
124
|
end
|
|
115
125
|
|
|
126
|
+
def current_headers
|
|
127
|
+
@current_headers ||= headers
|
|
128
|
+
end
|
|
129
|
+
|
|
116
130
|
def leave_my_turn(id)
|
|
117
131
|
response = request "#{current_url}/#{@queue_name}/#{id}", method: :delete
|
|
118
132
|
read_response(response, :locked)
|
|
@@ -123,6 +137,10 @@ class RequestMyTurn
|
|
|
123
137
|
hash[key.to_s]
|
|
124
138
|
end
|
|
125
139
|
|
|
140
|
+
def valid_callback?(callback)
|
|
141
|
+
callback.is_a?(Proc) || callback.is_a?(Method)
|
|
142
|
+
end
|
|
143
|
+
|
|
126
144
|
def present?(value)
|
|
127
145
|
if value.nil?
|
|
128
146
|
false
|
|
@@ -134,8 +152,4 @@ class RequestMyTurn
|
|
|
134
152
|
rescue StandardError
|
|
135
153
|
false
|
|
136
154
|
end
|
|
137
|
-
|
|
138
|
-
def valid_callback?(callback)
|
|
139
|
-
callback.is_a?(Proc) || callback.is_a?(Method)
|
|
140
|
-
end
|
|
141
155
|
end
|