Class: ThreeScaleToolbox::Entities::Metric

Inherits:
Object
  • Object
show all
Defined in:
lib/3scale_toolbox/entities/metric.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, service:, attrs: nil) ⇒ Metric

Returns a new instance of Metric.



31
32
33
34
35
36
# File 'lib/3scale_toolbox/entities/metric.rb', line 31

def initialize(id:, service:, attrs: nil)
  @id = id.to_i
  @service = service
  @remote = service.remote
  @attrs = attrs
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



29
30
31
# File 'lib/3scale_toolbox/entities/metric.rb', line 29

def id
  @id
end

#remoteObject (readonly)

Returns the value of attribute remote.



29
30
31
# File 'lib/3scale_toolbox/entities/metric.rb', line 29

def remote
  @remote
end

#serviceObject (readonly)

Returns the value of attribute service.



29
30
31
# File 'lib/3scale_toolbox/entities/metric.rb', line 29

def service
  @service
end

Class Method Details

.create(service:, attrs:) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/3scale_toolbox/entities/metric.rb', line 5

def create(service:, attrs:)
  metric = service.remote.create_metric service.id, attrs
  if (errors = metric['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Metric has not been created', errors)
  end

  new(id: metric.fetch('id'), service: service, attrs: metric)
end

.find(service:, ref:) ⇒ Object

ref can be system_name or metric_id



15
16
17
18
19
# File 'lib/3scale_toolbox/entities/metric.rb', line 15

def find(service:, ref:)
  new(id: ref, service: service).tap(&:attrs)
rescue ThreeScale::API::HttpClient::NotFoundError
  find_by_system_name(service: service, system_name: ref)
end

.find_by_system_name(service:, system_name:) ⇒ Object



21
22
23
24
25
26
# File 'lib/3scale_toolbox/entities/metric.rb', line 21

def find_by_system_name(service:, system_name:)
  metric = service.metrics.find { |m| m['system_name'] == system_name }
  return if metric.nil?

  new(id: metric.fetch('id'), service: service, attrs: metric)
end

Instance Method Details

#attrsObject



38
39
40
# File 'lib/3scale_toolbox/entities/metric.rb', line 38

def attrs
  @attrs ||= metric_attrs
end

#deleteObject



76
77
78
# File 'lib/3scale_toolbox/entities/metric.rb', line 76

def delete
  remote.delete_metric service.id, id
end

#disableObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/3scale_toolbox/entities/metric.rb', line 42

def disable
  # For each plan, get limits for the current metric
  # if already disabled -> NOOP
  # if non zero eternity limit exist, update
  # if non eternity limit exist, create
  service_plans.each do |plan|
    eternity_limit = plan_eternity_limit(plan)
    if eternity_limit.nil?
      plan.create_limit(id, zero_eternity_limit_attrs)
    elsif !eternity_limit.fetch('value').zero?
      plan.update_limit(id, eternity_limit.fetch('id'), zero_eternity_limit_attrs)
    end
  end
end

#enableObject



57
58
59
60
61
62
# File 'lib/3scale_toolbox/entities/metric.rb', line 57

def enable
  service_plans.each do |plan|
    limit = plan_zero_eternity_limit(plan)
    plan.delete_limit(id, limit.fetch('id')) unless limit.nil?
  end
end

#update(new_metric_attrs) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/3scale_toolbox/entities/metric.rb', line 64

def update(new_metric_attrs)
  new_attrs = remote.update_metric(service.id, id, new_metric_attrs)
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Metric has not been updated', errors)
  end

  # update current attrs
  @attrs = new_attrs

  new_attrs
end