Class: ThreeScaleToolbox::Entities::BackendMappingRule

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

Constant Summary collapse

VALID_PARAMS =
%w[metric_id pattern http_method delta position last].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CRD::BackendMappingRuleSerializer

#metric_method_ref, #to_cr

Constructor Details

#initialize(id:, backend:, attrs: nil) ⇒ BackendMappingRule

Returns a new instance of BackendMappingRule.



26
27
28
29
30
31
# File 'lib/3scale_toolbox/entities/backend_mapping_rule.rb', line 26

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

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



24
25
26
# File 'lib/3scale_toolbox/entities/backend_mapping_rule.rb', line 24

def backend
  @backend
end

#idObject (readonly)

Returns the value of attribute id.



24
25
26
# File 'lib/3scale_toolbox/entities/backend_mapping_rule.rb', line 24

def id
  @id
end

#remoteObject (readonly)

Returns the value of attribute remote.



24
25
26
# File 'lib/3scale_toolbox/entities/backend_mapping_rule.rb', line 24

def remote
  @remote
end

Class Method Details

.create(backend:, attrs:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/3scale_toolbox/entities/backend_mapping_rule.rb', line 10

def create(backend:, attrs:)
  mapping_rule = backend.remote.create_backend_mapping_rule(
    backend.id,
    Helper.filter_params(VALID_PARAMS, attrs)
  )
  if (errors = mapping_rule['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend mapping rule has not been created',
                                                    errors)
  end

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

Instance Method Details

#attrsObject



33
34
35
# File 'lib/3scale_toolbox/entities/backend_mapping_rule.rb', line 33

def attrs
  @attrs ||= mapping_rule_attrs
end

#deleteObject



84
85
86
# File 'lib/3scale_toolbox/entities/backend_mapping_rule.rb', line 84

def delete
  remote.delete_backend_mapping_rule backend.id, id
end

#deltaObject



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

def delta
  attrs['delta']
end

#http_methodObject



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

def http_method
  attrs['http_method']
end

#lastObject



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

def last
  attrs['last']
end

#metric_idObject



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

def metric_id
  attrs['metric_id']
end

#patternObject



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

def pattern
  attrs['pattern']
end

#update(mr_attrs) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/3scale_toolbox/entities/backend_mapping_rule.rb', line 69

def update(mr_attrs)
  new_attrs = remote.update_backend_mapping_rule(
    backend.id, id,
    Helper.filter_params(VALID_PARAMS, mr_attrs)
  )
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend mapping rule has not been updated', errors)
  end

  # update current attrs
  @attrs = new_attrs

  new_attrs
end