Class: RequestsCounter
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- RequestsCounter
show all
- Defined in:
- lib/requests_counter.rb
Defined Under Namespace
Classes: AvailableAttemptsNotInitialized, WaitTimeNotInitialized
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Class Attribute Details
.available_attempts ⇒ Object
Returns the value of attribute available_attempts.
8
9
10
|
# File 'lib/requests_counter.rb', line 8
def available_attempts
@available_attempts
end
|
.wait_time ⇒ Object
Returns the value of attribute wait_time.
9
10
11
|
# File 'lib/requests_counter.rb', line 9
def wait_time
@wait_time
end
|
Class Method Details
.permit?(token, resource = nil, params = {}) ⇒ Boolean
46
47
48
|
# File 'lib/requests_counter.rb', line 46
def self.permit?(token, resource = nil, params = {})
self.with_token(token, resource, params).permit?
end
|
.permitted?(token, resource = nil, params = {}) ⇒ Boolean
29
30
31
|
# File 'lib/requests_counter.rb', line 29
def self.permitted?(token, resource = nil, params = {})
self.with_token(token, resource, params).permitted?
end
|
.remaining(token, resource = nil, params = {}) ⇒ Object
60
61
62
|
# File 'lib/requests_counter.rb', line 60
def self.remaining(token, resource = nil, params = {})
self.with_token(token, resource, params).remaining
end
|
.with_token(token, target = nil, params = {}) ⇒ Object
Searches for instance element with provided token and resource and if it does not find one - will create new and return it to us
19
20
21
22
23
24
25
26
27
|
# File 'lib/requests_counter.rb', line 19
def self.with_token(token, target = nil, params = {})
unless el = self.where(:token => token.to_s, :resource => target.to_s).first
el = self.create({
:token => token.to_s,
:resource => target.to_s
}.merge(params))
end
el
end
|
Instance Method Details
#permit? ⇒ Boolean
50
51
52
53
54
|
# File 'lib/requests_counter.rb', line 50
def permit?
perm = permitted?
incr!
perm
end
|
#permitted? ⇒ Boolean
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/requests_counter.rb', line 33
def permitted?
if attempts < available_attempts
true
else
if time_permitted?
reset!
true
else
false
end
end
end
|
#remaining ⇒ Object
64
65
66
67
68
|
# File 'lib/requests_counter.rb', line 64
def remaining
r = available_attempts - attempts
r = 0 if r < 0
r
end
|
#reset! ⇒ Object
56
57
58
|
# File 'lib/requests_counter.rb', line 56
def reset!
self.update_attributes(:attempts => 0)
end
|