Class: MetricsApi::V3::Presenters::ExtendedMetadata

Inherits:
Object
  • Object
show all
Defined in:
app/services/metrics_api/v3/presenters/extended_metadata.rb

Constant Summary collapse

PRESENTERS_BASE =
'MetricsApi::V3::Presenters::'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metrics, start_date, end_date) ⇒ ExtendedMetadata

Returns a new instance of ExtendedMetadata.



10
11
12
13
14
# File 'app/services/metrics_api/v3/presenters/extended_metadata.rb', line 10

def initialize(metrics, start_date, end_date)
  @metrics = metrics
  @start_date = start_date
  @end_date = end_date
end

Instance Attribute Details

#end_dateObject (readonly)

Returns the value of attribute end_date.



8
9
10
# File 'app/services/metrics_api/v3/presenters/extended_metadata.rb', line 8

def end_date
  @end_date
end

#metricsObject (readonly)

Returns the value of attribute metrics.



8
9
10
# File 'app/services/metrics_api/v3/presenters/extended_metadata.rb', line 8

def metrics
  @metrics
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



8
9
10
# File 'app/services/metrics_api/v3/presenters/extended_metadata.rb', line 8

def start_date
  @start_date
end

Class Method Details

.to_procObject



35
36
37
# File 'app/services/metrics_api/v3/presenters/extended_metadata.rb', line 35

def self.to_proc
  ->(models) { new(models).to_json }
end

Instance Method Details

#to_jsonObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/metrics_api/v3/presenters/extended_metadata.rb', line 16

def to_json
  (start_date..end_date).map do |date|
    base = { date: date }

    todays_metrics = metrics.map do |metric|
      relavent_models = metric[:models].select { |key| key == date }.values.first
      presenter = (PRESENTERS_BASE + metric[:metric].camelize).constantize

      next { metric[:metric] => [] } unless relavent_models.present?

      { metric[:metric] => relavent_models.map(&presenter) }
    end

    todays_metrics.each { |x| base.merge!(x) }

    base
  end
end