Class: ForestLiana::StatsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/forest_liana/stats_controller.rb

Constant Summary collapse

CHART_TYPE_VALUE =
'Value'
CHART_TYPE_PIE =
'Pie'
CHART_TYPE_LINE =
'Line'
CHART_TYPE_LEADERBOARD =
'Leaderboard'
CHART_TYPE_OBJECTIVE =
'Objective'

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_user_from_jwt, #forest_user, #internal_server_error, papertrail?, #serialize_model, #serialize_models

Methods inherited from BaseController

#route_not_found

Instance Method Details

#getObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/forest_liana/stats_controller.rb', line 27

def get
  case params[:type]
  when CHART_TYPE_VALUE
    stat = ValueStatGetter.new(@resource, params, forest_user)
  when CHART_TYPE_PIE
    stat = PieStatGetter.new(@resource, params, forest_user)
  when CHART_TYPE_LINE
    stat = LineStatGetter.new(@resource, params, forest_user)
  when CHART_TYPE_OBJECTIVE
    stat = ObjectiveStatGetter.new(@resource, params, forest_user)
  when CHART_TYPE_LEADERBOARD
    stat = LeaderboardStatGetter.new(@resource, params, forest_user)
  end

  stat.perform
  if stat.record
    render json: serialize_model(stat.record), serializer: nil
  else
    render json: {status: 404}, status: :not_found, serializer: nil
  end
end

#get_with_live_queryObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/forest_liana/stats_controller.rb', line 49

def get_with_live_query
  begin
    stat = QueryStatGetter.new(params)
    stat.perform

    if stat.record
      render json: serialize_model(stat.record), serializer: nil
    else
      render json: {status: 404}, status: :not_found, serializer: nil
    end
  rescue ForestLiana::Errors::LiveQueryError => error
    render json: { errors: [{ status: 422, detail: error.message }] },
      status: :unprocessable_entity, serializer: nil
  rescue => error
    FOREST_LOGGER.error "Live Query error: #{error.message}"
    render json: { errors: [{ status: 422, detail: error.message }] },
      status: :unprocessable_entity, serializer: nil
  end
end