Class: Ask::TokenUsage::Configuration
- Inherits:
-
Object
- Object
- Ask::TokenUsage::Configuration
- Defined in:
- lib/ask/token_usage/configuration.rb
Overview
Global configuration for ask-token-usage.
Ask::TokenUsage.configure do |config|
config.store = Ask::TokenUsage::Stores::Memory.new
config.price_per_token = 0.0001 # $0.0001 per token
config.rounding = :ceil
config.negatives = false
config.currency = "USD"
end
Instance Attribute Summary collapse
-
#currency ⇒ Object
ISO currency code used by pricing.
-
#negatives ⇒ Object
Allow balances to go below zero.
- #on_adjust(&block) ⇒ Object
- #on_grant(&block) ⇒ Object
- #on_insufficient(&block) ⇒ Object
- #on_spend(&block) ⇒ Object
-
#rounding ⇒ Object
Token-cost rounding: :ceil (default), :floor, or :round.
-
#store ⇒ Object
The store backing every wallet.
- #time(&block) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#price_per_token ⇒ Object
The configured Money value of one billing token.
-
#price_per_token=(value) ⇒ Object
Set the price of a single billing token in USD (or your currency).
- #round(amount) ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ask/token_usage/configuration.rb', line 47 def initialize @store = Stores::Memory.new @rounding = :ceil @negatives = false @currency = "USD" @on_grant = nil @on_spend = nil @on_adjust = nil @on_insufficient = nil @time = -> { Time.now } end |
Instance Attribute Details
#currency ⇒ Object
ISO currency code used by pricing. Defaults to "USD".
29 30 31 |
# File 'lib/ask/token_usage/configuration.rb', line 29 def currency @currency end |
#negatives ⇒ Object
Allow balances to go below zero. Defaults to false.
26 27 28 |
# File 'lib/ask/token_usage/configuration.rb', line 26 def negatives @negatives end |
#on_adjust(&block) ⇒ Object
91 92 93 94 |
# File 'lib/ask/token_usage/configuration.rb', line 91 def on_adjust(&block) @on_adjust = block if block @on_adjust end |
#on_grant(&block) ⇒ Object
81 82 83 84 |
# File 'lib/ask/token_usage/configuration.rb', line 81 def on_grant(&block) @on_grant = block if block @on_grant end |
#on_insufficient(&block) ⇒ Object
96 97 98 99 |
# File 'lib/ask/token_usage/configuration.rb', line 96 def on_insufficient(&block) @on_insufficient = block if block @on_insufficient end |
#on_spend(&block) ⇒ Object
86 87 88 89 |
# File 'lib/ask/token_usage/configuration.rb', line 86 def on_spend(&block) @on_spend = block if block @on_spend end |
#rounding ⇒ Object
Token-cost rounding: :ceil (default), :floor, or :round.
23 24 25 |
# File 'lib/ask/token_usage/configuration.rb', line 23 def rounding @rounding end |
#store ⇒ Object
The store backing every wallet. Defaults to a process-wide in-memory store; swap for an ActiveRecord-backed store via ask-token-usage-rails.
20 21 22 |
# File 'lib/ask/token_usage/configuration.rb', line 20 def store @store end |
#time(&block) ⇒ Object
76 77 78 79 |
# File 'lib/ask/token_usage/configuration.rb', line 76 def time(&block) @time = block if block @time end |
Instance Method Details
#price_per_token ⇒ Object
The configured Money value of one billing token. Raises when unset.
70 71 72 73 74 |
# File 'lib/ask/token_usage/configuration.rb', line 70 def price_per_token return @price_per_token if defined?(@price_per_token) && @price_per_token raise Error, "Ask::TokenUsage.configure { |c| c.price_per_token = ... } is not set; pricing needs it" end |
#price_per_token=(value) ⇒ Object
Set the price of a single billing token in USD (or your currency). Provide a numeric value or a Money object.
config.price_per_token = 0.0001 # $0.0001 / token
config.price_per_token = Money.from_amount(0.0001, "USD")
65 66 67 |
# File 'lib/ask/token_usage/configuration.rb', line 65 def price_per_token=(value) @price_per_token = value.is_a?(Money) ? value : Money.from_amount(value.to_d, currency) end |
#round(amount) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/ask/token_usage/configuration.rb', line 101 def round(amount) number = amount.to_d case rounding when :ceil then number.ceil when :floor then number.floor when :round then number.round else number.to_i end end |