Class: ThreeScaleToolbox::Entities::BackendMetric

Inherits:
Object
  • Object
show all
Includes:
CRD::BackendMetricSerializer
Defined in:
lib/3scale_toolbox/entities/backend_metric.rb

Constant Summary collapse

VALID_PARAMS =
%w[friendly_name system_name unit description].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CRD::BackendMetricSerializer

#to_cr

Constructor Details

#initialize(id:, backend:, attrs: nil) ⇒ BackendMetric

Returns a new instance of BackendMetric.



35
36
37
38
39
40
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 35

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

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



33
34
35
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 33

def backend
  @backend
end

#idObject (readonly)

Returns the value of attribute id.



33
34
35
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 33

def id
  @id
end

#remoteObject (readonly)

Returns the value of attribute remote.



33
34
35
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 33

def remote
  @remote
end

Class Method Details

.create(backend:, attrs:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 10

def create(backend:, attrs:)
  metric = backend.remote.create_backend_metric(backend.id,
                                                Helper.filter_params(VALID_PARAMS, attrs))
  if (errors = metric['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend metric has not been created',
                                                    errors)
  end

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

.find(backend:, ref:) ⇒ Object

ref can be system_name or metric_id



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

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

.find_by_system_name(backend:, system_name:) ⇒ Object



28
29
30
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 28

def find_by_system_name(backend:, system_name:)
  backend.metrics.find { |m| m.system_name == system_name }
end

Instance Method Details

#attrsObject



42
43
44
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 42

def attrs
  @attrs ||= process_attrs(metric_attrs)
end

#deleteObject



73
74
75
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 73

def delete
  remote.delete_backend_metric backend.id, id
end

#descriptionObject



58
59
60
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 58

def description
  attrs['description']
end

#friendly_nameObject



50
51
52
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 50

def friendly_name
  attrs['friendly_name']
end

#system_nameObject



46
47
48
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 46

def system_name
  attrs['system_name']
end

#unitObject



54
55
56
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 54

def unit
  attrs['unit']
end

#update(m_attrs) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 62

def update(m_attrs)
  new_attrs = remote.update_backend_metric(backend.id, id,
                                           Helper.filter_params(VALID_PARAMS, m_attrs))
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend metric has not been updated', errors)
  end

  # update current attrs
  @attrs = process_attrs(new_attrs)
end