Class: ThreeScaleToolbox::Entities::Metric
- Inherits:
-
Object
- Object
- ThreeScaleToolbox::Entities::Metric
- Defined in:
- lib/3scale_toolbox/entities/metric.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#remote ⇒ Object
readonly
Returns the value of attribute remote.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Class Method Summary collapse
- .create(service:, attrs:) ⇒ Object
-
.find(service:, ref:) ⇒ Object
ref can be system_name or metric_id.
- .find_by_system_name(service:, system_name:) ⇒ Object
Instance Method Summary collapse
- #attrs ⇒ Object
- #delete ⇒ Object
- #disable ⇒ Object
- #enable ⇒ Object
-
#initialize(id:, service:, attrs: nil) ⇒ Metric
constructor
A new instance of Metric.
- #update(new_metric_attrs) ⇒ Object
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
#id ⇒ Object (readonly)
Returns the value of attribute id.
29 30 31 |
# File 'lib/3scale_toolbox/entities/metric.rb', line 29 def id @id end |
#remote ⇒ Object (readonly)
Returns the value of attribute remote.
29 30 31 |
# File 'lib/3scale_toolbox/entities/metric.rb', line 29 def remote @remote end |
#service ⇒ Object (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
#attrs ⇒ Object
38 39 40 |
# File 'lib/3scale_toolbox/entities/metric.rb', line 38 def attrs @attrs ||= metric_attrs end |
#delete ⇒ Object
76 77 78 |
# File 'lib/3scale_toolbox/entities/metric.rb', line 76 def delete remote.delete_metric service.id, id end |
#disable ⇒ Object
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 |
#enable ⇒ Object
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 |