Class: ThreeScaleToolbox::Entities::BackendMetric
- Inherits:
-
Object
- Object
- ThreeScaleToolbox::Entities::BackendMetric
- 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
- METRIC_BLACKLIST =
%w[id links created_at updated_at].freeze
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#remote ⇒ Object
readonly
Returns the value of attribute remote.
Class Method Summary collapse
- .create(backend:, attrs:) ⇒ Object
-
.find(backend:, ref:) ⇒ Object
ref can be system_name or metric_id.
- .find_by_system_name(backend:, system_name:) ⇒ Object
Instance Method Summary collapse
- #attrs ⇒ Object
- #delete ⇒ Object
- #description ⇒ Object
-
#enriched_key ⇒ Object
enriched_key returns a metric key that will be unique for all metrics from products and backends.
- #friendly_name ⇒ Object
-
#initialize(id:, backend:, attrs: nil) ⇒ BackendMetric
constructor
A new instance of BackendMetric.
- #system_name ⇒ Object
- #to_hash ⇒ Object
- #unit ⇒ Object
- #update(m_attrs) ⇒ Object
Methods included from CRD::BackendMetricSerializer
Constructor Details
#initialize(id:, backend:, attrs: nil) ⇒ BackendMetric
Returns a new instance of BackendMetric.
37 38 39 40 41 42 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 37 def initialize(id:, backend:, attrs: nil) @id = id.to_i @backend = backend @remote = backend.remote @attrs = process_attrs(attrs) end |
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
35 36 37 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 35 def backend @backend end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
35 36 37 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 35 def id @id end |
#remote ⇒ Object (readonly)
Returns the value of attribute remote.
35 36 37 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 35 def remote @remote end |
Class Method Details
.create(backend:, attrs:) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 12 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
24 25 26 27 28 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 24 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
30 31 32 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 30 def find_by_system_name(backend:, system_name:) backend.metrics.find { |m| m.system_name == system_name } end |
Instance Method Details
#attrs ⇒ Object
44 45 46 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 44 def attrs @attrs ||= process_attrs(metric_attrs) end |
#delete ⇒ Object
75 76 77 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 75 def delete remote.delete_backend_metric backend.id, id end |
#description ⇒ Object
60 61 62 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 60 def description attrs['description'] end |
#enriched_key ⇒ Object
enriched_key returns a metric key that will be unique for all metrics from products and backends
81 82 83 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 81 def enriched_key "backend.#{backend.id}.#{id}" end |
#friendly_name ⇒ Object
52 53 54 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 52 def friendly_name attrs['friendly_name'] end |
#system_name ⇒ Object
48 49 50 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 48 def system_name attrs['system_name'] end |
#to_hash ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 85 def to_hash extra_attrs = { 'backend_system_name' => backend.system_name } attrs.merge(extra_attrs).reject { |key, _| METRIC_BLACKLIST.include? key } end |
#unit ⇒ Object
56 57 58 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 56 def unit attrs['unit'] end |
#update(m_attrs) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/3scale_toolbox/entities/backend_metric.rb', line 64 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 |