Class: ThreeScaleToolbox::Entities::Method

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

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.



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

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.



28
29
30
# File 'lib/3scale_toolbox/entities/method.rb', line 28

def id
  @id
end

#remoteObject (readonly)

Returns the value of attribute remote.



28
29
30
# File 'lib/3scale_toolbox/entities/method.rb', line 28

def remote
  @remote
end

#serviceObject (readonly)

Returns the value of attribute service.



28
29
30
# File 'lib/3scale_toolbox/entities/method.rb', line 28

def service
  @service
end

Class Method Details

.create(service:, attrs:) ⇒ Object



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

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



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

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



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

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

Instance Method Details

#attrsObject



37
38
39
# File 'lib/3scale_toolbox/entities/method.rb', line 37

def attrs
  @attrs ||= method_attrs
end

#deleteObject



73
74
75
# File 'lib/3scale_toolbox/entities/method.rb', line 73

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

#descriptionObject



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

def description
  attrs['description']
end

#disableObject



53
54
55
# File 'lib/3scale_toolbox/entities/method.rb', line 53

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

#enableObject



57
58
59
# File 'lib/3scale_toolbox/entities/method.rb', line 57

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

#friendly_nameObject



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

def friendly_name
  attrs['friendly_name']
end

#system_nameObject



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

def system_name
  attrs['system_name']
end

#update(m_attrs) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/3scale_toolbox/entities/method.rb', line 61

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