Class: MetricsApi::V3::Endpoints::Root

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
app/services/metrics_api/v3/endpoints/root.rb

Constant Summary collapse

METRICS_TO_MODEL =

Mapping of metric names to the model that represents that metric

{
  'record' => SupplejackApi::FacetedMetrics,
  'view' => SupplejackApi::UsageMetrics
}.freeze
METRIC_TO_MODEL_KEY =

Mapping of metrics to the field on it’s respective model that contains the value to filter against

{
  'record' => :name,
  'view' => :record_field_value
}.freeze
MAX_FACETS =

Facet limit to return in response

10

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#parse_date_param

Constructor Details

#initialize(params) ⇒ Root

Returns a new instance of Root.



24
25
26
27
28
29
# File 'app/services/metrics_api/v3/endpoints/root.rb', line 24

def initialize(params)
  @facets = parse_csv_param(params[:facets])
  @start_date = parse_date_param(params[:start_date]) || Date.yesterday
  @end_date = parse_date_param(params[:end_date]) || Date.yesterday
  @metrics = parse_csv_param(params[:metrics]) || %w(record view)
end

Instance Attribute Details

#end_dateObject

Returns the value of attribute end_date.



8
9
10
# File 'app/services/metrics_api/v3/endpoints/root.rb', line 8

def end_date
  @end_date
end

#facetsObject

Returns the value of attribute facets.



8
9
10
# File 'app/services/metrics_api/v3/endpoints/root.rb', line 8

def facets
  @facets
end

#metricsObject

Returns the value of attribute metrics.



8
9
10
# File 'app/services/metrics_api/v3/endpoints/root.rb', line 8

def metrics
  @metrics
end

#start_dateObject

Returns the value of attribute start_date.



8
9
10
# File 'app/services/metrics_api/v3/endpoints/root.rb', line 8

def start_date
  @start_date
end

Instance Method Details

#callObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/metrics_api/v3/endpoints/root.rb', line 31

def call
  # TODO: Figure out a nicer way of handling error responses
  unless facets.present?
    return {
      exception: {
        status: 400,
        message: 'facets parameter is required'
      }
    }
  end
  if facets.size > MAX_FACETS
    return {
      exception: {
        status: 400,
        message: "Only up to #{MAX_FACETS} may be requested at once"
      }
    }
  end

  metrics_models = metrics.map(&method(:metric_to_model_bundle))
  filtered_models = metrics_models.map(&method(:filter_model_bundle))
  models_grouped_by_date = filtered_models.map(&method(:group_models_in_bundle_by_date))

  MetricsApi::V3::Presenters::ExtendedMetadata.new(models_grouped_by_date, start_date, end_date).to_json
end