Class: ThreeScaleToolbox::Entities::BackendMethod

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

Constant Summary collapse

VALID_PARAMS =
%w[friendly_name system_name description].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, parent_id:, backend:, attrs: nil) ⇒ BackendMethod

Returns a new instance of BackendMethod.



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

def initialize(id:, parent_id:, backend:, attrs: nil)
  @id = id.to_i
  @backend = backend
  @parent_id = parent_id
  @remote = backend.remote
  @attrs = process_attrs(attrs)
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



31
32
33
# File 'lib/3scale_toolbox/entities/backend_method.rb', line 31

def backend
  @backend
end

#idObject (readonly)

Returns the value of attribute id.



31
32
33
# File 'lib/3scale_toolbox/entities/backend_method.rb', line 31

def id
  @id
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



31
32
33
# File 'lib/3scale_toolbox/entities/backend_method.rb', line 31

def parent_id
  @parent_id
end

#remoteObject (readonly)

Returns the value of attribute remote.



31
32
33
# File 'lib/3scale_toolbox/entities/backend_method.rb', line 31

def remote
  @remote
end

Class Method Details

.create(backend:, parent_id:, attrs:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/3scale_toolbox/entities/backend_method.rb', line 8

def create(backend:, parent_id:, attrs:)
  method = backend.remote.create_backend_method(backend.id, parent_id,
                                                Helper.filter_params(VALID_PARAMS, attrs))
  if (errors = method['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend Method has not been created',
                                                    errors)
  end

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

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

ref can be system_name or method_id



20
21
22
23
24
# File 'lib/3scale_toolbox/entities/backend_method.rb', line 20

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

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



26
27
28
# File 'lib/3scale_toolbox/entities/backend_method.rb', line 26

def find_by_system_name(backend:, parent_id:, system_name:)
  backend.methods(parent_id).find { |m| m.system_name == system_name }
end

Instance Method Details

#attrsObject



41
42
43
# File 'lib/3scale_toolbox/entities/backend_method.rb', line 41

def attrs
  @attrs ||= process_attrs(method_attrs)
end

#deleteObject



65
66
67
# File 'lib/3scale_toolbox/entities/backend_method.rb', line 65

def delete
  remote.delete_backend_method backend.id, parent_id, id
end

#friendly_nameObject



49
50
51
# File 'lib/3scale_toolbox/entities/backend_method.rb', line 49

def friendly_name
  @attrs['friendly_name']
end

#system_nameObject



45
46
47
# File 'lib/3scale_toolbox/entities/backend_method.rb', line 45

def system_name
  @attrs['system_name']
end

#update(m_attrs) ⇒ Object



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

def update(m_attrs)
  new_attrs = remote.update_backend_method(backend.id, parent_id, id,
                                           Helper.filter_params(VALID_PARAMS, m_attrs))
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend Method has not been updated',
                                                    errors)
  end

  # update current attrs
  @attrs = process_attrs(new_attrs)
end