Class: ThreeScaleToolbox::Entities::Limit

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

Constant Summary collapse

LIMITS_BLACKLIST =
%w[id metric_id plan_id links created_at updated_at].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CRD::Limit

#metric_system_name, #to_cr

Constructor Details

#initialize(id:, plan:, metric_id:, attrs:) ⇒ Limit

Returns a new instance of Limit.



21
22
23
24
25
26
27
# File 'lib/3scale_toolbox/entities/limit.rb', line 21

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

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



19
20
21
# File 'lib/3scale_toolbox/entities/limit.rb', line 19

def attrs
  @attrs
end

#idObject (readonly)

Returns the value of attribute id.



19
20
21
# File 'lib/3scale_toolbox/entities/limit.rb', line 19

def id
  @id
end

#metric_idObject (readonly)

Returns the value of attribute metric_id.



19
20
21
# File 'lib/3scale_toolbox/entities/limit.rb', line 19

def metric_id
  @metric_id
end

#planObject (readonly)

Returns the value of attribute plan.



19
20
21
# File 'lib/3scale_toolbox/entities/limit.rb', line 19

def plan
  @plan
end

#remoteObject (readonly)

Returns the value of attribute remote.



19
20
21
# File 'lib/3scale_toolbox/entities/limit.rb', line 19

def remote
  @remote
end

Class Method Details

.create(plan:, metric_id:, attrs:) ⇒ Object



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

def create(plan:, metric_id:, attrs:)
  resp_attrs = plan.remote.create_application_plan_limit plan.id, metric_id, attrs
  if (errors = resp_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Limit has not been created', errors)
  end

  new(id: resp_attrs.fetch('id'), plan: plan, metric_id: metric_id, attrs: resp_attrs)
end

Instance Method Details

#backend_methodObject



75
76
77
78
79
# File 'lib/3scale_toolbox/entities/limit.rb', line 75

def backend_method
  if (backend = backend_from_metric_link)
    return backend.methods.find { |m| m.id == metric_id }
  end
end

#backend_metricObject



69
70
71
72
73
# File 'lib/3scale_toolbox/entities/limit.rb', line 69

def backend_metric
  if (backend = backend_from_metric_link)
    return backend.metrics.find { |m| m.id == metric_id }
  end
end

#deleteObject



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

def delete
  remote.delete_application_plan_limit plan.id, metric_id, id
end


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

def links
  attrs['links'] || []
end


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

def metric_link
  links.find { |link| link['rel'] == 'metric' } || {}
end

#periodObject



29
30
31
# File 'lib/3scale_toolbox/entities/limit.rb', line 29

def period
  attrs['period']
end

#product_methodObject



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

def product_method
  plan.service.methods.find { |m| m.id == metric_id }
end

#product_metricObject



61
62
63
# File 'lib/3scale_toolbox/entities/limit.rb', line 61

def product_metric
  plan.service.metrics.find { |m| m.id == metric_id }
end

#to_hashObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/3scale_toolbox/entities/limit.rb', line 81

def to_hash
  extra_attrs = {}

  if (metric = product_metric)
    extra_attrs['metric_system_name'] = metric.system_name
  elsif (method = product_method)
    extra_attrs['metric_system_name'] = method.system_name
  elsif (metric = backend_metric)
    extra_attrs['metric_system_name'] = metric.system_name
    extra_attrs['metric_backend_system_name'] = metric.backend.system_name
  elsif (method = backend_method)
    extra_attrs['metric_system_name'] = method.system_name
    extra_attrs['metric_backend_system_name'] = method.backend.system_name
  else
    raise_metric_not_found
  end

  attrs.merge(extra_attrs).reject { |key, _| LIMITS_BLACKLIST.include? key }
end

#update(new_limit_attrs) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/3scale_toolbox/entities/limit.rb', line 45

def update(new_limit_attrs)
  new_attrs = remote.update_application_plan_limit(plan.id, metric_id, id, new_limit_attrs)
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Limit has not been updated', errors)
  end

  # update current attrs
  @attrs = new_attrs

  new_attrs
end

#valueObject



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

def value
  attrs['value']
end