Class: Istox::BlockchainReceiptService

Inherits:
Object
  • Object
show all
Defined in:
lib/istox/helpers/blockchain_receipt_service.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.blockchain_receipt_classObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/istox/helpers/blockchain_receipt_service.rb', line 58

def self.blockchain_receipt_class
    raise "Have you forgetten to init blockchain receipt service?" if ::Istox::BlockchainReceiptService.get_blockchain_receipt_klass == nil

    blockchain_receipt_klass = ::Istox::BlockchainReceiptService.get_blockchain_receipt_klass
    klass = class_eval("::#{blockchain_receipt_klass.name}")
    unless klass <= ::Istox::BlockchainReceipt
        raise RuntimeError, "#{blockchain_receipt_klass.name} does not inherit istox blockchain receipt"
    end

    return klass
end

.generate_uuidObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/istox/helpers/blockchain_receipt_service.rb', line 12

def self.generate_uuid
    klass = ::Istox::BlockchainReceiptService.blockchain_receipt_class
    uuid = SecureRandom.uuid

    klass.create!({
        txid: uuid
    })

    uuid
end

.get_blockchain_receipt_klassObject



8
9
10
# File 'lib/istox/helpers/blockchain_receipt_service.rb', line 8

def self.get_blockchain_receipt_klass
    @@blockchain_receipt_klass
end

.init(blockchain_receipt_klass) ⇒ Object



4
5
6
# File 'lib/istox/helpers/blockchain_receipt_service.rb', line 4

def self.init(blockchain_receipt_klass)
    @@blockchain_receipt_klass = blockchain_receipt_klass
end

Instance Method Details

#create!(tx_message, model, sid, action, is_request: false) ⇒ Object



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
50
51
52
53
54
55
56
# File 'lib/istox/helpers/blockchain_receipt_service.rb', line 23

def create!(tx_message, model, sid, action, is_request: false)
    klass = ::Istox::BlockchainReceiptService.blockchain_receipt_class

    # delete the previous existing blockchain receipts
    klass.where(resource_name: model.class.name,
                            resource_id: model.id).destroy_all

    (0...tx_message.txnCount).each do |i|
        if i == 0
            blockchain_receipt = klass.find_by_txid(tx_message.uuid)

            raise "Unable to find blockchain receipt with uuid #{tx_message.uuid}, have you forgetten to generate uuid?" if blockchain_receipt.blank?

            blockchain_receipt.update!({
                sid: sid,
                txid: tx_message.uuid,
                resource_name: model.class.name,
                resource_id: model.id,
                resource_action: action,
                is_request: is_request
            })
        else
            blockchain_receipt = klass.new(
                sid: sid,
                txid: tx_message.uuid,
                resource_name: model.class.name,
                resource_id: model.id,
                resource_action: action,
                is_request: is_request
            )
            blockchain_receipt.save!
        end
    end
end