Class: ThreeScaleToolbox::Entities::BackendMetric

Inherits:
Object
  • Object
show all
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

Constructor Details

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

Returns a new instance of BackendMetric.



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

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.



31
32
33
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 31

def backend
  @backend
end

#idObject (readonly)

Returns the value of attribute id.



31
32
33
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 31

def id
  @id
end

#remoteObject (readonly)

Returns the value of attribute remote.



31
32
33
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 31

def remote
  @remote
end

Class Method Details

.create(backend:, attrs:) ⇒ Object



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

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



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

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



26
27
28
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 26

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

Instance Method Details

#attrsObject



40
41
42
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 40

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

#deleteObject



63
64
65
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 63

def delete
  remote.delete_backend_metric backend.id, id
end

#friendly_nameObject



48
49
50
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 48

def friendly_name
  @attrs['friendly_name']
end

#system_nameObject



44
45
46
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 44

def system_name
  @attrs['system_name']
end

#update(m_attrs) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 52

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