-
Notifications
You must be signed in to change notification settings - Fork 46
/
oauth_daily_request.rb
52 lines (40 loc) · 1.4 KB
/
oauth_daily_request.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Daily requests of a Resource Owner on a specific client
class OauthDailyRequest
include Mongoid::Document
field :created_at, type: Time # creation time
field :time_id # unique key for the day
field :day # request day
field :month # request month
field :year # request year
field :times, type: Integer, default: 0 # daily request times
# resource owner's client access
embedded_in :oauth_access, inverse_of: :oauth_daily_requests
after_create :init_times
# Increment the times counter that track the number of
# requests a client have made in behalf of a resource
# owner in a specific day
def increment!
self.times += 1
self.save
end
class << self
# Find a daily requests record
def find_day(time)
time_id = time_id(time)
where(time_id: time_id)
end
# Define an identifier for a specific day
def time_id(time)
time.strftime("%Y%m%d")
end
end
private
# Add statistical informations
def init_times
self.day = self.created_at.strftime("%d")
self.month = self.created_at.strftime("%m")
self.year = self.created_at.strftime("%Y")
self.time_id = self.class.time_id(created_at)
self.save
end
end