Class: SchwabRb::DataObjects::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/schwab_rb/data_objects/transaction.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(activity_id:, time:, type:, status:, sub_account:, trade_date:, position_id:, order_id:, net_amount:, transfer_items: []) ⇒ Transaction

Returns a new instance of Transaction.



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/schwab_rb/data_objects/transaction.rb', line 103

def initialize(activity_id:, time:, type:, status:, sub_account:, trade_date:, position_id:, order_id:,
               net_amount:, transfer_items: [])
  @activity_id = activity_id
  @time = time
  @type = type
  @status = status
   = 
  @trade_date = trade_date
  @position_id = position_id
  @order_id = order_id
  @net_amount = net_amount
  @transfer_items = transfer_items
end

Instance Attribute Details

#activity_idObject (readonly)

Returns the value of attribute activity_id.



117
118
119
# File 'lib/schwab_rb/data_objects/transaction.rb', line 117

def activity_id
  @activity_id
end

#net_amountObject (readonly)

Returns the value of attribute net_amount.



117
118
119
# File 'lib/schwab_rb/data_objects/transaction.rb', line 117

def net_amount
  @net_amount
end

#order_idObject (readonly)

Returns the value of attribute order_id.



117
118
119
# File 'lib/schwab_rb/data_objects/transaction.rb', line 117

def order_id
  @order_id
end

#position_idObject (readonly)

Returns the value of attribute position_id.



117
118
119
# File 'lib/schwab_rb/data_objects/transaction.rb', line 117

def position_id
  @position_id
end

#statusObject (readonly)

Returns the value of attribute status.



117
118
119
# File 'lib/schwab_rb/data_objects/transaction.rb', line 117

def status
  @status
end

#sub_accountObject (readonly)

Returns the value of attribute sub_account.



117
118
119
# File 'lib/schwab_rb/data_objects/transaction.rb', line 117

def 
  
end

#timeObject (readonly)

Returns the value of attribute time.



117
118
119
# File 'lib/schwab_rb/data_objects/transaction.rb', line 117

def time
  @time
end

#trade_dateObject (readonly)

Returns the value of attribute trade_date.



117
118
119
# File 'lib/schwab_rb/data_objects/transaction.rb', line 117

def trade_date
  @trade_date
end

#transfer_itemsObject (readonly)

Returns the value of attribute transfer_items.



117
118
119
# File 'lib/schwab_rb/data_objects/transaction.rb', line 117

def transfer_items
  @transfer_items
end

#typeObject (readonly)

Returns the value of attribute type.



117
118
119
# File 'lib/schwab_rb/data_objects/transaction.rb', line 117

def type
  @type
end

Class Method Details

.build(data) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/schwab_rb/data_objects/transaction.rb', line 87

def build(data)
  Transaction.new(
    activity_id: data.fetch(:activityId),
    time: data.fetch(:time),
    type: data.fetch(:type),
    status: data.fetch(:status),
    sub_account: data.fetch(:subAccount),
    trade_date: data.fetch(:tradeDate),
    position_id: data.fetch(:positionId, nil),
    order_id: data.fetch(:orderId, nil),
    net_amount: data.fetch(:netAmount, nil),
    transfer_items: data.fetch(:transferItems).map { |ti| TransferItem.build(ti) }
  )
end

Instance Method Details

#asset_instrument_idObject



144
145
146
# File 'lib/schwab_rb/data_objects/transaction.rb', line 144

def asset_instrument_id
  transfer_items.find { |ti| ti.asset? }.instrument_id
end

#asset_symbolObject



140
141
142
# File 'lib/schwab_rb/data_objects/transaction.rb', line 140

def asset_symbol
  transfer_items.find { |ti| ti.asset? }.symbol
end

#commissionsObject



132
133
134
# File 'lib/schwab_rb/data_objects/transaction.rb', line 132

def commissions
  transfer_items.select(&:commission?).map { |ti| ti.cost }
end

#credit_debitsObject



124
125
126
# File 'lib/schwab_rb/data_objects/transaction.rb', line 124

def credit_debits
  transfer_items.select(&:credit_debit?).map { |ti| ti.cost }
end

#feesObject



128
129
130
# File 'lib/schwab_rb/data_objects/transaction.rb', line 128

def fees
  transfer_items.select(&:fee?).map { |ti| ti.cost }
end

#symbolsObject



136
137
138
# File 'lib/schwab_rb/data_objects/transaction.rb', line 136

def symbols
  transfer_items.map { |ti| ti.symbol }
end

#to_hObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/schwab_rb/data_objects/transaction.rb', line 148

def to_h
  {
    activity_id: @activity_id,
    time: @time,
    type: @type,
    status: @status,
    sub_account: ,
    trade_date: @trade_date,
    position_id: @position_id,
    order_id: @order_id,
    net_amount: @net_amount,
    transfer_items: @transfer_items.map(&:to_h)
  }
end

#trade?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/schwab_rb/data_objects/transaction.rb', line 120

def trade?
  type == "TRADE"
end