Class: Coinbase::User

Inherits:
Object
  • Object
show all
Defined in:
lib/coinbase/user.rb

Overview

A representation of a User. Users have Wallets, which can hold balances of Assets. Access the default User through Coinbase#default_user.

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ User

Returns a new User object. Do not use this method directly. Instead, use Coinbase#default_user.



12
13
14
# File 'lib/coinbase/user.rb', line 12

def initialize(model)
  @model = model
end

Instance Method Details

#create_wallet(create_wallet_options = {}) ⇒ Coinbase::Wallet

Creates a new Wallet belonging to the User.



25
26
27
28
29
30
31
# File 'lib/coinbase/user.rb', line 25

def create_wallet(create_wallet_options = {})
  # For ruby 2.7 compatibility we cannot pass in keyword args when the create wallet
  # options is empty
  return Wallet.create if create_wallet_options.empty?

  Wallet.create(**create_wallet_options)
end

#idString

Returns the User ID.



18
19
20
# File 'lib/coinbase/user.rb', line 18

def id
  @model.id
end

#import_wallet(data) ⇒ Coinbase::Wallet

Imports a Wallet belonging to the User.



36
37
38
# File 'lib/coinbase/user.rb', line 36

def import_wallet(data)
  Wallet.import(data)
end

#inspectString

Same as to_s.



97
98
99
# File 'lib/coinbase/user.rb', line 97

def inspect
  to_s
end

#to_sString

Returns a string representation of the User.



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

def to_s
  "Coinbase::User{user_id: '#{id}'}"
end

#wallet(wallet_id) ⇒ Coinbase::Wallet

Returns the Wallet with the given ID.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/coinbase/user.rb', line 77

def wallet(wallet_id)
  wallet_model = Coinbase.call_api do
    wallets_api.get_wallet(wallet_id)
  end

  addresses_list = Coinbase.call_api do
    addresses_api.list_addresses(wallet_model.id, { limit: Coinbase::Wallet::MAX_ADDRESSES })
  end

  Wallet.new(wallet_model, seed: '', address_models: addresses_list.data)
end

#wallets(page_size: 10, next_page_token: nil) ⇒ Array<Coinbase::Wallet, String>

Lists the Wallets belonging to the User.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/coinbase/user.rb', line 45

def wallets(page_size: 10, next_page_token: nil)
  opts = {
    limit: page_size
  }

  opts[:page] = next_page_token unless next_page_token.nil?

  wallet_list = Coinbase.call_api do
    wallets_api.list_wallets(opts)
  end

  # A map from wallet_id to address models.
  address_model_map = {}

  wallet_list.data.each do |wallet_model|
    addresses_list = Coinbase.call_api do
      addresses_api.list_addresses(wallet_model.id, { limit: Coinbase::Wallet::MAX_ADDRESSES })
    end

    address_model_map[wallet_model.id] = addresses_list.data
  end

  wallets = wallet_list.data.map do |wallet_model|
    Wallet.new(wallet_model, seed: '', address_models: address_model_map[wallet_model.id])
  end

  [wallets, wallet_list.next_page]
end