Class: ThreeScaleToolbox::Entities::Method

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CRD::MethodSerializer

#to_cr

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

#idObject (readonly)

Returns the value of attribute id.



30
31
32
# File 'lib/3scale_toolbox/entities/method.rb', line 30

def id
  @id
end

#remoteObject (readonly)

Returns the value of attribute remote.



30
31
32
# File 'lib/3scale_toolbox/entities/method.rb', line 30

def remote
  @remote
end

#serviceObject (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

#attrsObject



39
40
41
# File 'lib/3scale_toolbox/entities/method.rb', line 39

def attrs
  @attrs ||= method_attrs
end

#deleteObject



75
76
77
# File 'lib/3scale_toolbox/entities/method.rb', line 75

def delete
  remote.delete_method service.id, hits_id, id
end

#descriptionObject



51
52
53
# File 'lib/3scale_toolbox/entities/method.rb', line 51

def description
  attrs['description']
end

#disableObject



55
56
57
# File 'lib/3scale_toolbox/entities/method.rb', line 55

def disable
  Metric.new(id: id, service: service).disable
end

#enableObject



59
60
61
# File 'lib/3scale_toolbox/entities/method.rb', line 59

def enable
  Metric.new(id: id, service: service).enable
end

#enriched_keyObject

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_nameObject



47
48
49
# File 'lib/3scale_toolbox/entities/method.rb', line 47

def friendly_name
  attrs['friendly_name']
end

#system_nameObject



43
44
45
# File 'lib/3scale_toolbox/entities/method.rb', line 43

def system_name
  attrs['system_name']
end

#to_hashObject



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