class ThreeScaleToolbox::Entities::BackendMetric

Constants

VALID_PARAMS

Attributes

backend[R]
id[R]
remote[R]

Public Class Methods

create(backend:, attrs:) click to toggle source
# 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:) click to toggle source

ref can be system_name or metric_id

# 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:) click to toggle source
# 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
new(id:, backend:, attrs: nil) click to toggle source
# 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

Public Instance Methods

attrs() click to toggle source
# File lib/3scale_toolbox/entities/backend_metric.rb, line 42
def attrs
  @attrs ||= process_attrs(metric_attrs)
end
delete() click to toggle source
# File lib/3scale_toolbox/entities/backend_metric.rb, line 73
def delete
  remote.delete_backend_metric backend.id, id
end
description() click to toggle source
# File lib/3scale_toolbox/entities/backend_metric.rb, line 58
def description
  attrs['description']
end
friendly_name() click to toggle source
# File lib/3scale_toolbox/entities/backend_metric.rb, line 50
def friendly_name
  attrs['friendly_name']
end
system_name() click to toggle source
# File lib/3scale_toolbox/entities/backend_metric.rb, line 46
def system_name
  attrs['system_name']
end
unit() click to toggle source
# File lib/3scale_toolbox/entities/backend_metric.rb, line 54
def unit
  attrs['unit']
end
update(m_attrs) click to toggle source
# 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

Private Instance Methods

metric_attrs() click to toggle source
# File lib/3scale_toolbox/entities/backend_metric.rb, line 86
def metric_attrs
  raise ThreeScaleToolbox::InvalidIdError if id.zero?

  metric = remote.backend_metric backend.id, id
  if (errors = metric['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend metric not read', errors)
  end

  metric
end
process_attrs(metric_attrs) click to toggle source
# File lib/3scale_toolbox/entities/backend_metric.rb, line 79
def process_attrs(metric_attrs)
  return if metric_attrs.nil?

  # system_name: my_metric_02.45498 -> system_name: my_metric_02
  metric_attrs.merge('system_name' => metric_attrs.fetch('system_name', '').partition('.').first)
end