Class: ThreeScaleToolbox::Entities::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/3scale_toolbox/entities/method.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, parent_id:, service:, attrs: nil) ⇒ Method

Returns a new instance of Method.



32
33
34
35
36
37
38
# File 'lib/3scale_toolbox/entities/method.rb', line 32

def initialize(id:, parent_id:, service:, attrs: nil)
  @id = id.to_i
  @service = service
  @parent_id = parent_id
  @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

#parent_idObject (readonly)

Returns the value of attribute parent_id.



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

def parent_id
  @parent_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:, parent_id:, attrs:) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/3scale_toolbox/entities/method.rb', line 5

def create(service:, parent_id:, attrs:)
  method = service.remote.create_method service.id, parent_id, attrs
  if (errors = method['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Method has not been created', errors)

  end

  new(id: method.fetch('id'), parent_id: parent_id, service: service, attrs: method)
end

.find(service:, parent_id:, ref:) ⇒ Object

ref can be system_name or method_id



16
17
18
19
20
# File 'lib/3scale_toolbox/entities/method.rb', line 16

def find(service:, parent_id:, ref:)
  new(id: ref, parent_id: parent_id, service: service).tap(&:attrs)
rescue ThreeScale::API::HttpClient::NotFoundError
  find_by_system_name(service: service, parent_id: parent_id, system_name: ref)
end

.find_by_system_name(service:, parent_id:, system_name:) ⇒ Object



22
23
24
25
26
27
# File 'lib/3scale_toolbox/entities/method.rb', line 22

def find_by_system_name(service:, parent_id:, system_name:)
  method = service.methods(parent_id).find { |m| m['system_name'] == system_name }
  return if method.nil?

  new(id: method.fetch('id'), parent_id: parent_id, service: service, attrs: method)
end

Instance Method Details

#attrsObject



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

def attrs
  @attrs ||= method_attrs
end

#deleteObject



64
65
66
# File 'lib/3scale_toolbox/entities/method.rb', line 64

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

#disableObject



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

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

#enableObject



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

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

#update(m_attrs) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/3scale_toolbox/entities/method.rb', line 52

def update(m_attrs)
  new_attrs = remote.update_method(service.id, parent_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