Class: ThreeScale::Backend::UsageLimit

Inherits:
Object
  • Object
show all
Extended by:
Memoizer::Decorator
Includes:
Storable
Defined in:
lib/3scale/backend/usage_limit.rb

Constant Summary collapse

PERIODS =
(Period::ALL_DESC - [Period::Second]).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Memoizer::Decorator

included

Methods included from Storable

included, #initialize, #storage

Methods included from StorageKeyHelpers

#encode_key

Instance Attribute Details

#metric_idObject

Returns the value of attribute metric_id.



8
9
10
# File 'lib/3scale/backend/usage_limit.rb', line 8

def metric_id
  @metric_id
end

#periodObject

Returns the value of attribute period.



8
9
10
# File 'lib/3scale/backend/usage_limit.rb', line 8

def period
  @period
end

#plan_idObject

Returns the value of attribute plan_id.



8
9
10
# File 'lib/3scale/backend/usage_limit.rb', line 8

def plan_id
  @plan_id
end

#service_idObject

Returns the value of attribute service_id.



8
9
10
# File 'lib/3scale/backend/usage_limit.rb', line 8

def service_id
  @service_id
end

#valueObject

Returns the value of attribute value.



8
9
10
# File 'lib/3scale/backend/usage_limit.rb', line 8

def value
  @value
end

Class Method Details

.delete(service_id, plan_id, metric_id, period) ⇒ Object



52
53
54
55
# File 'lib/3scale/backend/usage_limit.rb', line 52

def delete(service_id, plan_id, metric_id, period)
  storage.del(key(service_id, plan_id, metric_id, period))
  clear_cache(service_id, plan_id)
end

.load_all(service_id, plan_id) ⇒ Object



23
24
25
26
# File 'lib/3scale/backend/usage_limit.rb', line 23

def load_all(service_id, plan_id)
  metric_ids = Metric.load_all_ids(service_id)
  generate_for_metrics(service_id, plan_id, metric_ids)
end

.load_for_affecting_metrics(service_id, plan_id, metric_ids) ⇒ Object



29
30
31
# File 'lib/3scale/backend/usage_limit.rb', line 29

def load_for_affecting_metrics(service_id, plan_id, metric_ids)
  generate_for_metrics(service_id, plan_id, metric_ids)
end

.load_value(service_id, plan_id, metric_id, period) ⇒ Object



34
35
36
37
# File 'lib/3scale/backend/usage_limit.rb', line 34

def load_value(service_id, plan_id, metric_id, period)
  raw_value = storage.get(key(service_id, plan_id, metric_id, period))
  raw_value and raw_value.to_i
end

.save(attributes) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/3scale/backend/usage_limit.rb', line 39

def save(attributes)
  service_id = attributes[:service_id]
  plan_id = attributes[:plan_id]
  prefix = key_prefix(service_id, plan_id, attributes[:metric_id])
  storage.pipelined do
    PERIODS.each do |period|
      p_val = attributes[period.to_sym]
      p_val and storage.set(key_for_period(prefix, period), p_val)
    end
  end
  clear_cache(service_id, plan_id)
end

Instance Method Details

#metric_nameObject



10
11
12
# File 'lib/3scale/backend/usage_limit.rb', line 10

def metric_name
  Metric.load_name(service_id, metric_id)
end

#validate(usage_data) ⇒ Object

NOTE: validate can ONLY be called with the guarantee that usage_data will have a matching period key.



16
17
18
# File 'lib/3scale/backend/usage_limit.rb', line 16

def validate(usage_data)
  usage_data[period][metric_id].to_i <= value
end