Sha256: 0818a23b7dda89cb852162e293f4ff9159d670c97a7c41e7d7c1cf3eb67103ce

Contents?: true

Size: 1.49 KB

Versions: 6

Compression:

Stored size: 1.49 KB

Contents

module ThreeScaleToolbox
  module Entities
    class PricingRule
      include CRD::PricingRuleSerializer

      class << self
        def create(plan:, metric_id:, attrs:)
          resp_attrs = plan.remote.create_pricingrule plan.id, metric_id, attrs
          if (errors = resp_attrs['errors'])
            raise ThreeScaleToolbox::ThreeScaleApiError.new('Pricing rule has not been created', errors)
          end

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

      attr_reader :id, :plan, :remote, :attrs, :metric_id

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

      def cost_per_unit
        attrs['cost_per_unit'].to_f
      end

      def min
        attrs['min']
      end

      def max
        attrs['max']
      end

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

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

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

      private

      # Used by CRD::PricingRule
      # Returns the backend hosting the metric
      def backend_from_metric
        backend_id = Helper.backend_metric_link_parser(metric_link['href'] || '')
        return if backend_id.nil?

        Backend.new(id: backend_id.to_i, remote: remote)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
3scale_toolbox-0.19.2 lib/3scale_toolbox/entities/pricing_rule.rb
3scale_toolbox-0.19.1 lib/3scale_toolbox/entities/pricing_rule.rb
3scale_toolbox-0.19.0 lib/3scale_toolbox/entities/pricing_rule.rb
3scale_toolbox-0.18.3 lib/3scale_toolbox/entities/pricing_rule.rb
3scale_toolbox-0.18.2 lib/3scale_toolbox/entities/pricing_rule.rb
3scale_toolbox-0.18.0 lib/3scale_toolbox/entities/pricing_rule.rb