request_my_turn 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/request_my_turn/version.rb +5 -0
- data/lib/request_my_turn.rb +141 -0
- metadata +45 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 7de338d9c27b44a4775be1525220b7bdd6e523f5d22efb05e77df9897e00d8b6
|
|
4
|
+
data.tar.gz: 8d536553f4fd9a630bd4de438fc52e19e5fc2b1c214490152ec04f3e4d56ed1b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 621f529c44f820030520a4079fcb0a7741e3fcf37f795fb067b9cd2f8d5756e3378d641a62f6e0304cd62d6fc91c224f8d78a883285d616e2198e5c73d2dc6b9
|
|
7
|
+
data.tar.gz: '008c58efef617d3ce9a866274b34ef8770424b982b6f570f729f512b0f9bcd8939eb889232e1eca753aba2a7741bbe9c15de36e366b7080800a738c17e881d09'
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'net/http'
|
|
5
|
+
require 'ostruct'
|
|
6
|
+
require 'timeout'
|
|
7
|
+
require 'uri'
|
|
8
|
+
|
|
9
|
+
require_relative 'request_my_turn/version'
|
|
10
|
+
|
|
11
|
+
class RequestMyTurn
|
|
12
|
+
class WithoutBlock < StandardError
|
|
13
|
+
def initialize
|
|
14
|
+
super 'This service must be used with `block\'!'
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class InvalidUrl < StandardError
|
|
19
|
+
def initialize(url)
|
|
20
|
+
super "Invalid url: #{url}"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class TimeoutError < StandardError
|
|
25
|
+
def initialize(time)
|
|
26
|
+
super "I didn't get the turn within #{time} seconds"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
class << self
|
|
31
|
+
def configure
|
|
32
|
+
block_given? ? yield(settings) : settings
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def settings
|
|
36
|
+
@settings ||= OpenStruct.new(
|
|
37
|
+
url: nil,
|
|
38
|
+
switch: nil
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def initialize(queue_name, **options)
|
|
44
|
+
@queue_name = queue_name
|
|
45
|
+
|
|
46
|
+
@url = options[:url]
|
|
47
|
+
@after = options[:after]
|
|
48
|
+
@before = options[:before]
|
|
49
|
+
@timeout = options[:timeout]
|
|
50
|
+
@lock_seconds = options[:lock_seconds]
|
|
51
|
+
@headers = options[:headers]
|
|
52
|
+
@ignore_timeout_error = options[:ignore_timeout_error]
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def perform(&block)
|
|
56
|
+
raise WithoutBlock unless block
|
|
57
|
+
return yield unless switched_on?
|
|
58
|
+
|
|
59
|
+
id = take_my_turn
|
|
60
|
+
@before.call(id) if valid_callback? @before
|
|
61
|
+
|
|
62
|
+
result = yield
|
|
63
|
+
return result unless present? id
|
|
64
|
+
|
|
65
|
+
locked = leave_my_turn(id)
|
|
66
|
+
@after.call(locked) if valid_callback? @after
|
|
67
|
+
result
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def switched_on?
|
|
73
|
+
return settings.switch.call(self) if settings.switch.is_a? Proc
|
|
74
|
+
|
|
75
|
+
present? current_url
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def settings
|
|
79
|
+
self.class.settings.clone
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def current_url
|
|
83
|
+
@current_url ||= build_url
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def build_url
|
|
87
|
+
url = @url || settings.url
|
|
88
|
+
return false unless present?(url)
|
|
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
|
|
100
|
+
|
|
101
|
+
raise TimeoutError, @timeout
|
|
102
|
+
end
|
|
103
|
+
read_response(response, :id)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def request(url, method: :get)
|
|
107
|
+
uri = URI(url)
|
|
108
|
+
https = Net::HTTP.new(uri.host, uri.port)
|
|
109
|
+
https.use_ssl = url.start_with? 'https'
|
|
110
|
+
|
|
111
|
+
request = method == :delete ? Net::HTTP::Delete.new(uri) : Net::HTTP::Get.new(uri)
|
|
112
|
+
@headers.each { |key, value| request[key] = value } if @headers.is_a?(Hash)
|
|
113
|
+
https.request(request)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def leave_my_turn(id)
|
|
117
|
+
response = request "#{current_url}/#{@queue_name}/#{id}", method: :delete
|
|
118
|
+
read_response(response, :locked)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def read_response(response, key)
|
|
122
|
+
hash = JSON.parse response.body
|
|
123
|
+
hash[key.to_s]
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def present?(value)
|
|
127
|
+
if value.nil?
|
|
128
|
+
false
|
|
129
|
+
elsif value.is_a?(Numeric) || value.length.positive?
|
|
130
|
+
true
|
|
131
|
+
else
|
|
132
|
+
false
|
|
133
|
+
end
|
|
134
|
+
rescue StandardError
|
|
135
|
+
false
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def valid_callback?(callback)
|
|
139
|
+
callback.is_a?(Proc) || callback.is_a?(Method)
|
|
140
|
+
end
|
|
141
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: request_my_turn
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- ralph baesso
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-02-10 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email:
|
|
15
|
+
- ralphsbaesso@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- lib/request_my_turn.rb
|
|
21
|
+
- lib/request_my_turn/version.rb
|
|
22
|
+
homepage:
|
|
23
|
+
licenses:
|
|
24
|
+
- MIT
|
|
25
|
+
metadata: {}
|
|
26
|
+
post_install_message:
|
|
27
|
+
rdoc_options: []
|
|
28
|
+
require_paths:
|
|
29
|
+
- lib
|
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - ">="
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: 2.5.0
|
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
requirements: []
|
|
41
|
+
rubygems_version: 3.2.32
|
|
42
|
+
signing_key:
|
|
43
|
+
specification_version: 4
|
|
44
|
+
summary: Request my turn to project my-turn.
|
|
45
|
+
test_files: []
|