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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7de338d9c27b44a4775be1525220b7bdd6e523f5d22efb05e77df9897e00d8b6
4
- data.tar.gz: 8d536553f4fd9a630bd4de438fc52e19e5fc2b1c214490152ec04f3e4d56ed1b
3
+ metadata.gz: 49bfb4eb8f4642237f380883f8fcd6c037185cbcd798968fe3401fb573ca6e79
4
+ data.tar.gz: 06ccd80467f6dc64b6bb33227fbaa9a7a87b3fb9e4dc54ddccf1eace769ebb1c
5
5
  SHA512:
6
- metadata.gz: 621f529c44f820030520a4079fcb0a7741e3fcf37f795fb067b9cd2f8d5756e3378d641a62f6e0304cd62d6fc91c224f8d78a883285d616e2198e5c73d2dc6b9
7
- data.tar.gz: '008c58efef617d3ce9a866274b34ef8770424b982b6f570f729f512b0f9bcd8939eb889232e1eca753aba2a7741bbe9c15de36e366b7080800a738c17e881d09'
6
+ metadata.gz: d5939532dc27f74e38a209ac9f6e619055c1d78db5dc76d6fec5b3a7bf74763210c2f7d47608a3cff09c920c3bb8a148823dda75ab6b7eefe9eaeb79774b4599
7
+ data.tar.gz: 22af5a57eb80c50490e3500d559b258617bc420332ee3634d615b82b61a6d958a8444b294c82b34b42fc4ce16dbfd8cea6ee9ae51300106b5ef099a31d03b16d
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class RequestMyTurn
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
@@ -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
- switch: nil
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
- @before.call(id) if valid_callback? @before
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
- @after.call(locked) if valid_callback? @after
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
- return settings.switch.call(self) if settings.switch.is_a? Proc
74
-
75
- present? current_url
90
+ switch
76
91
  end
77
92
 
78
- def settings
79
- self.class.settings.clone
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
- 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
110
+ current_url = url
111
+ raise InvalidUrl, current_url unless current_url =~ URI::DEFAULT_PARSER.make_regexp
100
112
 
101
- raise TimeoutError, @timeout
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
- @headers.each { |key, value| request[key] = value } if @headers.is_a?(Hash)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: request_my_turn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ralph baesso