Module: Revolut::Api::Private::Transactions

Included in:
Client
Defined in:
lib/revolut/api/private/transactions.rb

Instance Method Summary collapse

Instance Method Details

#request_transactions(params: {}, retries: 3) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/revolut/api/private/transactions.rb', line 51

def request_transactions(params: {}, retries: 3)
  data                        =   nil
      
  begin
    data                      =   get("user/current/transactions", params: params)
  rescue Faraday::ParsingError => e
    retries        -=   1
    retry if retries > 0
  end
      
  return data
end

#transaction(id) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/revolut/api/private/transactions.rb', line 64

def transaction(id)
  response          =   get("transaction/#{id}")
      
  if response.is_a?(Hash)
    log "#{response["message"]}"
  elsif response.is_a?(Array)
    return ::Revolut::Api::Response::Transaction.new(response&.first)
  end
end

#transactions(from: Time.new(Time.now.year, Time.now.month, 1, 0, 0, 0, 0), type: nil, phrase: nil, completed: nil, pending: nil, fetch_all: true, memoize: true) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/revolut/api/private/transactions.rb', line 6

def transactions(from: Time.new(Time.now.year, Time.now.month, 1, 0, 0, 0, 0), type: nil, phrase: nil, completed: nil, pending: nil, fetch_all: true, memoize: true)
  current                     =   Time.now.utc
  txs                         =   {}
      
  if fetch_all && memoize && memoized.fetch(:transactions, []).any?
    txs                       =   memoized.fetch(:transactions, [])
  else
    while from < current
      params                  =   {from: ::Revolut::Api::Utilities.utc_to_epoch(from)}
      data                    =   request_transactions(params: params)

      data.each do |item|
        tx                    =   ::Revolut::Api::Response::Transaction.new(item)
        txs[tx.id]            =   tx if tx && !tx.id.to_s.empty?
      end

      from                    =   from + ::Revolut::Api::Constants::TIME[:one_month]
      break unless fetch_all
    end
      
    txs                       =   txs.values
    memoized[:transactions]   =   txs if fetch_all
  end
      
  if !type.to_s.empty?
    txs                       =   txs.select { |tx| tx.type.to_s.upcase.strip == type.to_s.upcase.strip }
  end
      
  if completed.eql?(true)
    txs                       =   txs.select { |tx| tx.completed? }
  end
  
  if pending.eql?(true)
    txs                       =   txs.select { |tx| tx.pending? }
  end
      
  if !phrase.to_s.empty?
    txs                       =   txs.select { |tx| !(tx.description =~ /#{phrase}/i).nil? }
  end
      
  txs                         =   txs.sort_by { |tx| tx.started_date }
      
  return txs
end