Class: ErrorRadar::Api::ErrorsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/error_radar/api/errors_controller.rb

Constant Summary collapse

PER_PAGE =
50

Instance Method Summary collapse

Instance Method Details

#indexObject

GET /api/errors



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/error_radar/api/errors_controller.rb', line 9

def index
  scope  = build_scope
  total  = scope.count
  page   = [params[:page].to_i, 1].max
  total_pages = [(total.to_f / PER_PAGE).ceil, 1].max
  page   = [page, total_pages].min

  errors = scope.limit(PER_PAGE).offset((page - 1) * PER_PAGE)

  render json: {
    data: errors.map { |e| serialize(e) },
    meta: { total: total, page: page, per_page: PER_PAGE, total_pages: total_pages }
  }
end

#showObject

GET /api/errors/:id



25
26
27
28
29
30
# File 'app/controllers/error_radar/api/errors_controller.rb', line 25

def show
  log = ErrorLog.find(params[:id])
  render json: { data: serialize(log, detail: true) }
rescue ActiveRecord::RecordNotFound
  render json: { error: 'Not found' }, status: :not_found
end

#updateObject

PATCH /api/errors/:id



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/error_radar/api/errors_controller.rb', line 33

def update
  log        = ErrorLog.find(params[:id])
  new_status = params[:status].to_s

  unless ErrorLog.statuses.key?(new_status)
    return render json: { error: 'invalid status' }, status: :unprocessable_entity
  end

  if new_status == 'resolved'
    log.resolve!(by: params[:resolved_by].presence, note: params[:note].presence)
  else
    log.update!(status: new_status, resolved_at: nil)
  end

  render json: { data: serialize(log) }
rescue ActiveRecord::RecordNotFound
  render json: { error: 'Not found' }, status: :not_found
end