Class: Myrr::Budget

Inherits:
Object
  • Object
show all
Defined in:
lib/myrr/budget.rb

Overview

Budget represents spending limit rules for an agent.

Examples:

Myrr::Budget.set(
  agent_did: "did:myrr:abc123",
  daily_max_cents: 100_00,
  monthly_max_cents: 500_00,
  allowed_merchant_categories: ["data_services", "api_services"]
)

budget = Myrr::Budget.for_agent("did:myrr:abc123")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Budget

Returns a new instance of Budget.

Parameters:

  • attrs (Hash)

    Budget attributes from the API



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/myrr/budget.rb', line 21

def initialize(attrs)
  @id = attrs["id"]
  @agent_did = attrs["agent_did"]
  @per_transaction_max = attrs["per_transaction_max"]
  @topup_per_transaction_max = attrs["topup_per_transaction_max"]
  @daily_max = attrs["daily_max"]
  @monthly_max = attrs["monthly_max"]
  @allowed_merchant_categories = attrs["allowed_merchant_categories"]
  @created_at = attrs["created_at"]
  @updated_at = attrs["updated_at"]
end

Instance Attribute Details

#agent_didObject (readonly)

Returns the value of attribute agent_did.



16
17
18
# File 'lib/myrr/budget.rb', line 16

def agent_did
  @agent_did
end

#allowed_merchant_categoriesObject (readonly)

Returns the value of attribute allowed_merchant_categories.



16
17
18
# File 'lib/myrr/budget.rb', line 16

def allowed_merchant_categories
  @allowed_merchant_categories
end

#created_atObject (readonly)

Returns the value of attribute created_at.



16
17
18
# File 'lib/myrr/budget.rb', line 16

def created_at
  @created_at
end

#daily_maxObject (readonly)

Returns the value of attribute daily_max.



16
17
18
# File 'lib/myrr/budget.rb', line 16

def daily_max
  @daily_max
end

#idObject (readonly)

Returns the value of attribute id.



16
17
18
# File 'lib/myrr/budget.rb', line 16

def id
  @id
end

#monthly_maxObject (readonly)

Returns the value of attribute monthly_max.



16
17
18
# File 'lib/myrr/budget.rb', line 16

def monthly_max
  @monthly_max
end

#per_transaction_maxObject (readonly)

Returns the value of attribute per_transaction_max.



16
17
18
# File 'lib/myrr/budget.rb', line 16

def per_transaction_max
  @per_transaction_max
end

#topup_per_transaction_maxObject (readonly)

Returns the value of attribute topup_per_transaction_max.



16
17
18
# File 'lib/myrr/budget.rb', line 16

def topup_per_transaction_max
  @topup_per_transaction_max
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



16
17
18
# File 'lib/myrr/budget.rb', line 16

def updated_at
  @updated_at
end

Class Method Details

.for_agent(agent_did) ⇒ Myrr::Budget?

Get budget for an agent.

Parameters:

  • agent_did (String)

    The did:myrr: identifier

Returns:

  • (Myrr::Budget, nil)

    Returns nil if no budget has been set



59
60
61
62
63
64
65
# File 'lib/myrr/budget.rb', line 59

def self.for_agent(agent_did)
  resp = Myrr.client.send(:get, "/v1/budgets/agent/#{agent_did}")
  new(resp)
rescue Myrr::Error => e
  raise unless e.message.include?("agent_not_found")
  nil
end

.set(agent_did:, per_transaction_max_cents: nil, topup_per_transaction_max_cents: nil, daily_max_cents: nil, monthly_max_cents: nil, allowed_merchant_categories: nil) ⇒ Myrr::Budget

Create or update budget for an agent.

Parameters:

  • agent_did (String)

    The did:myrr: identifier

  • per_transaction_max_cents (Integer, nil) (defaults to: nil)

    Max per transaction

  • topup_per_transaction_max_cents (Integer, nil) (defaults to: nil)

    Max per top-up

  • daily_max_cents (Integer, nil) (defaults to: nil)

    Max per day

  • monthly_max_cents (Integer, nil) (defaults to: nil)

    Max per month

  • allowed_merchant_categories (Array<String>, nil) (defaults to: nil)

    Allowed MCCs

Returns:



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/myrr/budget.rb', line 42

def self.set(agent_did:, per_transaction_max_cents: nil, topup_per_transaction_max_cents: nil,
             daily_max_cents: nil, monthly_max_cents: nil, allowed_merchant_categories: nil)
  body = { agent_did: agent_did }
  body[:per_transaction_max] = per_transaction_max_cents if per_transaction_max_cents
  body[:topup_per_transaction_max] = topup_per_transaction_max_cents if topup_per_transaction_max_cents
  body[:daily_max] = daily_max_cents if daily_max_cents
  body[:monthly_max] = monthly_max_cents if monthly_max_cents
  body[:allowed_merchant_categories] = allowed_merchant_categories if allowed_merchant_categories

  resp = Myrr.client.send(:post, "/v1/budgets", body)
  new(resp)
end