Module: UsageCredits

Defined in:
lib/usage_credits.rb,
lib/usage_credits.rb,
lib/usage_credits/engine.rb,
lib/usage_credits/railtie.rb,
lib/usage_credits/version.rb,
lib/usage_credits/callbacks.rb,
lib/usage_credits/cost/base.rb,
lib/usage_credits/cost/fixed.rb,
lib/usage_credits/configuration.rb,
lib/usage_credits/cost/compound.rb,
lib/usage_credits/cost/variable.rb,
lib/usage_credits/models/wallet.rb,
lib/usage_credits/callback_context.rb,
lib/usage_credits/models/operation.rb,
lib/usage_credits/models/allocation.rb,
lib/usage_credits/models/credit_pack.rb,
lib/usage_credits/models/fulfillment.rb,
lib/usage_credits/models/transaction.rb,
lib/usage_credits/jobs/fulfillment_job.rb,
lib/usage_credits/helpers/period_parser.rb,
lib/usage_credits/helpers/credits_helper.rb,
lib/usage_credits/helpers/credit_calculator.rb,
lib/usage_credits/models/concerns/has_wallet.rb,
lib/generators/usage_credits/install_generator.rb,
lib/usage_credits/services/fulfillment_service.rb,
lib/usage_credits/models/credit_subscription_plan.rb,
lib/usage_credits/models/concerns/pay_charge_extension.rb,
lib/usage_credits/models/concerns/pay_subscription_extension.rb

Overview

lib/usage_credits/services/fulfillment_service.rb

Defined Under Namespace

Modules: Callbacks, Cost, CreditCalculator, CreditsHelper, Generators, HasWallet, PayChargeExtension, PaySubscriptionExtension, PeriodParser Classes: Allocation, ApplicationJob, ApplicationRecord, CallbackContext, Configuration, CreditPack, CreditSubscriptionPlan, Engine, Error, Fulfillment, FulfillmentJob, FulfillmentService, InsufficientCredits, InvalidOperation, Operation, Railtie, Transaction, Wallet

Constant Summary collapse

VERSION =
"0.5.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



69
70
71
# File 'lib/usage_credits.rb', line 69

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.available_credit_packsObject Also known as: available_packs



109
110
111
# File 'lib/usage_credits.rb', line 109

def available_credit_packs
  credit_packs.values.uniq
end

.configure {|configuration| ... } ⇒ Object

Configure the gem with a block (main entry point)

Yields:



74
75
76
# File 'lib/usage_credits.rb', line 74

def configure
  yield(configuration)
end

.credit_pack(name, &block) ⇒ Object



95
96
97
# File 'lib/usage_credits.rb', line 95

def credit_pack(name, &block)
  configuration.credit_pack(name, &block)
end

.credit_packsObject Also known as: packs



99
100
101
# File 'lib/usage_credits.rb', line 99

def credit_packs
  configuration.credit_packs
end

.credit_subscription_plansObject Also known as: subscription_plans, plans



118
119
120
# File 'lib/usage_credits.rb', line 118

def credit_subscription_plans
  configuration.credit_subscription_plans
end

.find_credit_pack(name) ⇒ Object Also known as: find_pack



104
105
106
# File 'lib/usage_credits.rb', line 104

def find_credit_pack(name)
  credit_packs[name.to_sym]
end

.find_subscription_plan(name) ⇒ Object Also known as: find_plan



124
125
126
# File 'lib/usage_credits.rb', line 124

def find_subscription_plan(name)
  credit_subscription_plans[name.to_sym]
end

.find_subscription_plan_by_processor_id(processor_id) ⇒ Object Also known as: find_plan_by_id



129
130
131
# File 'lib/usage_credits.rb', line 129

def find_subscription_plan_by_processor_id(processor_id)
  configuration.find_subscription_plan_by_processor_id(processor_id)
end

.handle_event(event, **params) ⇒ Object

DEPRECATED: Events are now dispatched through Callbacks module This method shows a deprecation warning only once to avoid log spam



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/usage_credits.rb', line 149

def handle_event(event, **params)
  @deprecation_warnings ||= {}
  unless @deprecation_warnings[:handle_event]
    warn "[DEPRECATION] UsageCredits.handle_event is deprecated. Events are now dispatched through the Callbacks module."
    @deprecation_warnings[:handle_event] = true
  end
  case event
  when :low_balance_reached
    notify_low_balance(params[:wallet]&.owner)
  end
end

.notify_low_balance(owner) ⇒ Object

DEPRECATED: Event handling for low balance notifications Use on_low_balance_reached callback instead This method shows a deprecation warning only once to avoid log spam



137
138
139
140
141
142
143
144
145
# File 'lib/usage_credits.rb', line 137

def notify_low_balance(owner)
  @deprecation_warnings ||= {}
  unless @deprecation_warnings[:notify_low_balance]
    warn "[DEPRECATION] UsageCredits.notify_low_balance is deprecated. Use on_low_balance_reached callback instead."
    @deprecation_warnings[:notify_low_balance] = true
  end
  return unless configuration.low_balance_callback
  configuration.low_balance_callback.call(owner)
end

.operation(name, &block) ⇒ Object

DSL methods - all delegate to configuration These enable things like both UsageCredits.credit_pack and bare credit_pack usage



87
88
89
# File 'lib/usage_credits.rb', line 87

def operation(name, &block)
  configuration.operation(name, &block)
end

.operationsObject



91
92
93
# File 'lib/usage_credits.rb', line 91

def operations
  configuration.operations
end

.reset!Object

Reset configuration to defaults (mainly for testing)



79
80
81
82
# File 'lib/usage_credits.rb', line 79

def reset!
  @configuration = nil
  @deprecation_warnings = {}
end

.subscription_plan(name, &block) ⇒ Object



114
115
116
# File 'lib/usage_credits.rb', line 114

def subscription_plan(name, &block)
  configuration.subscription_plan(name, &block)
end