Class: ThreeScaleToolbox::Entities::MappingRule

Inherits:
Object
  • Object
show all
Includes:
CRD::MappingRuleSerializer
Defined in:
lib/3scale_toolbox/entities/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::MappingRuleSerializer

#metric_method_ref, #to_cr

Constructor Details

#initialize(id:, service:, attrs: nil) ⇒ MappingRule

Returns a new instance of MappingRule.



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

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.



24
25
26
# File 'lib/3scale_toolbox/entities/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/mapping_rule.rb', line 24

def remote
  @remote
end

#serviceObject (readonly)

Returns the value of attribute service.



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

def service
  @service
end

Class Method Details

.create(service:, attrs:) ⇒ Object



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

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

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

Instance Method Details

#attrsObject



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

def attrs
  @attrs ||= mapping_rule_attrs
end

#deleteObject



72
73
74
# File 'lib/3scale_toolbox/entities/mapping_rule.rb', line 72

def delete
  remote.delete_mapping_rule service.id, id
end

#deltaObject



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

def delta
  attrs['delta']
end

#http_methodObject



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

def http_method
  attrs['http_method']
end

#lastObject



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

def last
  attrs['last']
end

#metric_idObject



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

def metric_id
  attrs['metric_id']
end

#patternObject



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

def pattern
  attrs['pattern']
end

#update(mr_attrs) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/3scale_toolbox/entities/mapping_rule.rb', line 57

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

  # update current attrs
  @attrs = new_attrs

  new_attrs
end