Class: SupplejackApi::User

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::ForbiddenAttributesProtection, Mongoid::Document, Mongoid::Timestamps, Sortable::Query
Defined in:
app/models/supplejack_api/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.custom_find(id) ⇒ Object

Raises:

  • (Mongoid::Errors::DocumentNotFound)


192
193
194
195
196
197
198
199
200
201
# File 'app/models/supplejack_api/user.rb', line 192

def self.custom_find(id)
  user = if id.to_s.length == 24
           find(id)
         else
           find_by_api_key(id)
         end

  raise Mongoid::Errors::DocumentNotFound.new(self, id, id) unless user
  user
end

.find_by_api_key(api_key) ⇒ Object



188
189
190
# File 'app/models/supplejack_api/user.rb', line 188

def self.find_by_api_key(api_key)
  where(authentication_token: api_key).first
end

Instance Method Details

#admin?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'app/models/supplejack_api/user.rb', line 203

def admin?
  RecordSchema.roles[role.to_sym].try(:admin)
end

#calculate_last_30_days_requestsObject



157
158
159
160
161
# File 'app/models/supplejack_api/user.rb', line 157

def calculate_last_30_days_requests
  count = 0
  user_activities.gt(created_at: Time.now - 30.days).each { |activity| count += activity.total.to_i }
  self.monthly_requests = count
end

Returns:

  • (Boolean)


207
208
209
# File 'app/models/supplejack_api/user.rb', line 207

def can_change_featured_sets?
  admin?
end

#check_daily_requestsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/supplejack_api/user.rb', line 85

def check_daily_requests
  if updated_today?
    increment_daily_requests
  else
    self.daily_requests = 1
  end

  if daily_requests == (max_requests * 0.9).floor
    SupplejackApi::RequestLimitMailer.at90percent(self).deliver
    SupplejackApi::RequestLimitMailer.at90percent_admin(self).deliver
  elsif daily_requests == max_requests
    SupplejackApi::RequestLimitMailer.at100percent(self).deliver
    SupplejackApi::RequestLimitMailer.at100percent_admin(self).deliver
  end
end

#increment_daily_requestsObject



101
102
103
104
# File 'app/models/supplejack_api/user.rb', line 101

def increment_daily_requests
  self.daily_requests ||= 0
  self.daily_requests += 1
end

#nameObject



76
77
78
79
# File 'app/models/supplejack_api/user.rb', line 76

def name
  name = self[:name]
  name.present? ? name : username
end

#name_or_userObject



176
177
178
# File 'app/models/supplejack_api/user.rb', line 176

def name_or_user
  name.present? ? split_on_at_symbol(name) : split_on_at_symbol(username)
end

#over_limit?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'app/models/supplejack_api/user.rb', line 153

def over_limit?
  updated_today? && daily_requests > max_requests
end

#requests_per_day(days = 30) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/models/supplejack_api/user.rb', line 163

def requests_per_day(days = 30)
  today = Time.zone.now.to_date
  user_activities = self.user_activities.gt(created_at: today - days.days).asc(:created_at).to_a

  requests = []
  date = today - days.days
  while date < today
    date += 1.days
    requests << (user_activities.find { |ua| ua.created_at.to_date == date }.try(:total) || 0)
  end
  requests
end

#reset_daily_activityObject



147
148
149
150
151
# File 'app/models/supplejack_api/user.rb', line 147

def reset_daily_activity
  self.daily_requests = 0
  self.daily_activity = nil
  self.daily_activity_stored = true
end

#sets=(attrs_array) ⇒ Object



71
72
73
74
# File 'app/models/supplejack_api/user.rb', line 71

def sets=(attrs_array)
  return false unless attrs_array.try(:any?)
  attrs_array.each { |attrs| user_sets.build(attrs) }
end

#split_on_at_symbol(value) ⇒ Object



180
181
182
183
184
185
186
# File 'app/models/supplejack_api/user.rb', line 180

def split_on_at_symbol(value)
  if value.present? && value.include?('@')
    value.split('@').first
  else
    value
  end
end

#update_daily_activity(request) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/supplejack_api/user.rb', line 121

def update_daily_activity(request)
  # Get the controller name and strips out the `supplejack_api/` namespace
  controller = request.params[:controller].to_s.gsub(%r{ supplejack_api/ }, '')
  action = request.params[:action].to_s

  if controller == 'records' && action == 'index'
    controller = 'search'
    action = 'records'
  # TODO: Custom Search is deprecated. Evaluate for removal
  elsif controller == 'custom_searches' && action == 'records'
    controller = 'search'
    action = 'custom_search'
  elsif controller == 'set_items'
    controller = 'user_sets'
    action = "#{action}_item"
  end

  self.daily_activity ||= {}
  self.daily_activity[controller] ||= {}

  current_value = self.daily_activity[controller][action].to_i
  self.daily_activity[controller][action] = current_value + 1

  self.daily_activity_stored = false
end

#update_tracked_fields(request) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/supplejack_api/user.rb', line 106

def update_tracked_fields(request)
  old_current = 
  new_current = Time.now.utc
  self.     = old_current || new_current
  self.  = new_current

  old_current = 
  new_current = request.ip
  self.     = old_current || new_current
  self.  = new_current

  self. ||= 0
  self. += 1
end

#updated_today?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'app/models/supplejack_api/user.rb', line 81

def updated_today?
  updated_at > Time.now.beginning_of_day
end