Class: ThreeScaleToolbox::Entities::Method
- Inherits:
-
Object
- Object
- ThreeScaleToolbox::Entities::Method
- Includes:
- CRD::MethodSerializer
- Defined in:
- lib/3scale_toolbox/entities/method.rb
Constant Summary collapse
- METHOD_BLACKLIST =
%w[id links created_at updated_at parent_id].freeze
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 method_id.
- .find_by_system_name(service:, system_name:) ⇒ Object
Instance Method Summary collapse
- #attrs ⇒ Object
- #delete ⇒ Object
- #description ⇒ Object
- #disable ⇒ Object
- #enable ⇒ Object
-
#enriched_key ⇒ Object
enriched_key returns a metric key that will be unique for all metrics/methods from products and backends.
- #friendly_name ⇒ Object
-
#initialize(id:, service:, attrs: nil) ⇒ Method
constructor
A new instance of Method.
- #system_name ⇒ Object
- #to_hash ⇒ Object
- #update(m_attrs) ⇒ Object
Methods included from CRD::MethodSerializer
Constructor Details
#initialize(id:, service:, attrs: nil) ⇒ Method
Returns a new instance of Method.
32 33 34 35 36 37 |
# File 'lib/3scale_toolbox/entities/method.rb', line 32 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.
30 31 32 |
# File 'lib/3scale_toolbox/entities/method.rb', line 30 def id @id end |
#remote ⇒ Object (readonly)
Returns the value of attribute remote.
30 31 32 |
# File 'lib/3scale_toolbox/entities/method.rb', line 30 def remote @remote end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
30 31 32 |
# File 'lib/3scale_toolbox/entities/method.rb', line 30 def service @service end |
Class Method Details
.create(service:, attrs:) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/3scale_toolbox/entities/method.rb', line 9 def create(service:, attrs:) method_attrs = service.remote.create_method service.id, service.hits.id, attrs if (errors = method_attrs['errors']) raise ThreeScaleToolbox::ThreeScaleApiError.new('Method has not been created', errors) end new(id: method_attrs.fetch('id'), service: service, attrs: method_attrs) end |
.find(service:, ref:) ⇒ Object
ref can be system_name or method_id
19 20 21 22 23 |
# File 'lib/3scale_toolbox/entities/method.rb', line 19 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
25 26 27 |
# File 'lib/3scale_toolbox/entities/method.rb', line 25 def find_by_system_name(service:, system_name:) service.methods.find { |m| m.system_name == system_name } end |
Instance Method Details
#attrs ⇒ Object
39 40 41 |
# File 'lib/3scale_toolbox/entities/method.rb', line 39 def attrs @attrs ||= method_attrs end |
#delete ⇒ Object
75 76 77 |
# File 'lib/3scale_toolbox/entities/method.rb', line 75 def delete remote.delete_method service.id, hits_id, id end |
#description ⇒ Object
51 52 53 |
# File 'lib/3scale_toolbox/entities/method.rb', line 51 def description attrs['description'] end |
#disable ⇒ Object
55 56 57 |
# File 'lib/3scale_toolbox/entities/method.rb', line 55 def disable Metric.new(id: id, service: service).disable end |
#enable ⇒ Object
59 60 61 |
# File 'lib/3scale_toolbox/entities/method.rb', line 59 def enable Metric.new(id: id, service: service).enable end |
#enriched_key ⇒ Object
enriched_key returns a metric key that will be unique for all metrics/methods from products and backends
81 82 83 |
# File 'lib/3scale_toolbox/entities/method.rb', line 81 def enriched_key "product.#{service.id}.#{id}" end |
#friendly_name ⇒ Object
47 48 49 |
# File 'lib/3scale_toolbox/entities/method.rb', line 47 def friendly_name attrs['friendly_name'] end |
#system_name ⇒ Object
43 44 45 |
# File 'lib/3scale_toolbox/entities/method.rb', line 43 def system_name attrs['system_name'] end |
#to_hash ⇒ Object
85 86 87 |
# File 'lib/3scale_toolbox/entities/method.rb', line 85 def to_hash attrs.reject { |key, _| METHOD_BLACKLIST.include? key } end |
#update(m_attrs) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/3scale_toolbox/entities/method.rb', line 63 def update(m_attrs) new_attrs = remote.update_method(service.id, hits_id, id, m_attrs) if (errors = new_attrs['errors']) raise ThreeScaleToolbox::ThreeScaleApiError.new('Method has not been updated', errors) end # update current attrs @attrs = new_attrs new_attrs end |