Class: ThreeScaleToolbox::Entities::Backend

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

Constant Summary collapse

VALID_PARAMS =
%w[name description system_name private_endpoint].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, remote:, attrs: nil) ⇒ Backend

Returns a new instance of Backend.



40
41
42
43
44
# File 'lib/3scale_toolbox/entities/backend.rb', line 40

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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



38
39
40
# File 'lib/3scale_toolbox/entities/backend.rb', line 38

def id
  @id
end

#remoteObject (readonly)

Returns the value of attribute remote.



38
39
40
# File 'lib/3scale_toolbox/entities/backend.rb', line 38

def remote
  @remote
end

Class Method Details

.create(remote:, attrs:) ⇒ Object



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

def create(remote:, attrs:)
  b_attrs = remote.create_backend Helper.filter_params(VALID_PARAMS, attrs)
  if (errors = b_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend has not been created', errors)
  end

  new(id: b_attrs.fetch('id'), remote: remote, attrs: b_attrs)
end

.find(remote:, ref:) ⇒ Object

ref can be system_name or backend_id



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

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

.find_by_system_name(remote:, system_name:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/3scale_toolbox/entities/backend.rb', line 24

def find_by_system_name(remote:, system_name:)
  list = remote.list_backends

  if list.respond_to?(:has_key?) && (errors = list['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend list not read', errors)
  end

  attrs = list.find { |backend| backend['system_name'] == system_name }
  return if attrs.nil?

  new(id: attrs.fetch('id'), remote: remote, attrs: attrs)
end

Instance Method Details

#==(other) ⇒ Object



118
119
120
# File 'lib/3scale_toolbox/entities/backend.rb', line 118

def ==(other)
  remote.http_client.endpoint == other.remote.http_client.endpoint && id == other.id
end

#attrsObject



46
47
48
# File 'lib/3scale_toolbox/entities/backend.rb', line 46

def attrs
  @attrs ||= fetch_backend_attrs
end

#deleteObject



114
115
116
# File 'lib/3scale_toolbox/entities/backend.rb', line 114

def delete
  remote.delete_backend id
end

#hitsObject



68
69
70
# File 'lib/3scale_toolbox/entities/backend.rb', line 68

def hits
  hits_metric(metrics_and_methods)
end

#mapping_rulesObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/3scale_toolbox/entities/backend.rb', line 91

def mapping_rules
  m_r = remote.list_backend_mapping_rules id
  if m_r.respond_to?(:has_key?) && (errors = m_r['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend mapping rules not read', errors)
  end

  m_r.map do |mr_attrs|
    BackendMappingRule.new(id: mr_attrs.fetch('id'), backend: self, attrs: mr_attrs)
  end
end

#methods(parent_metric_id) ⇒ List

Parameters:

  • parent_metric_id (Object)

    BackendMetric hits object

Returns:

  • (List)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/3scale_toolbox/entities/backend.rb', line 75

def methods(parent_metric_id)
  return [] if parent_metric_id.nil?

  method_attr_list = remote.list_backend_methods id, parent_metric_id.id
  if method_attr_list.respond_to?(:has_key?) && (errors = method_attr_list['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend methods not read', errors)
  end

  method_attr_list.map do |method_attrs|
    BackendMethod.new(id: method_attrs.fetch('id'),
                      backend: self,
                      parent_id: parent_metric_id.id,
                      attrs: method_attrs)
  end
end

#metricsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/3scale_toolbox/entities/backend.rb', line 54

def metrics
  # cache result to reuse
  metric_and_method_list = metrics_and_methods
  hits_metric_obj = hits_metric(metric_and_method_list)

  metric_attr_list = ThreeScaleToolbox::Helper.array_difference(metric_and_method_list, methods(hits_metric_obj)) do |item, method|
    method.id == item.fetch('id', nil)
  end

  metric_attr_list.map do |metric_attrs|
    BackendMetric.new(id: metric_attrs.fetch('id'), backend: self, attrs: metric_attrs)
  end
end

#system_nameObject



50
51
52
# File 'lib/3scale_toolbox/entities/backend.rb', line 50

def system_name
  attrs['system_name']
end

#update(b_attrs) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/3scale_toolbox/entities/backend.rb', line 102

def update(b_attrs)
  new_attrs = remote.update_backend id, Helper.filter_params(VALID_PARAMS, b_attrs)
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend not updated', errors)
  end

  # update current attrs
  @attrs = new_attrs

  new_attrs
end