stats_controller.rb
969 Bytes
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
class Api::V1::StatsController < ApplicationController
respond_to :json, :xml
# The stats API only requires an api_key for the given app.
skip_before_action :authenticate_user!
before_action :require_api_key_or_authenticate_user!
def app
if (problem = @app.problems.order_by(:last_notice_at.desc).first)
@last_error_time = problem.last_notice_at
end
stats = {
:name => @app.name,
:id => @app.id,
:last_error_time => @last_error_time,
:unresolved_errors => @app.unresolved_count
}
respond_to do |format|
format.any(:html, :json) { render :json => JSON.dump(stats) } # render JSON if no extension specified on path
format.xml { render :xml => stats }
end
end
protected
def require_api_key_or_authenticate_user!
if params[:api_key].present?
if (@app = App.where(:api_key => params[:api_key]).first)
return true
end
end
authenticate_user!
end
end