Class: CLURef
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#decode_base_16, #encode_base_16, #from_bytes
Constructor Details
#initialize(value = nil, access_rights = nil) ⇒ CLURef
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
|
# File 'lib/types/cl_uref.rb', line 16
def initialize(value = nil, access_rights = nil)
super()
begin
raise ArgumentError.new('The length of URefAddr should be 32') unless value.length == 32
rescue ArgumentError => e
e.message
end
begin
raise ArgumentError.new('Unsupported AccessRights') unless AccessRights.has_value?(access_rights)
rescue ArgumentError => e
e.message
end
@value = value
@access_rights = access_rights
end
|
Instance Attribute Details
#err ⇒ Object
Returns the value of attribute err.
13
14
15
|
# File 'lib/types/cl_uref.rb', line 13
def err
@err
end
|
Class Method Details
.from_json(json) ⇒ Object
112
113
114
115
116
117
118
|
# File 'lib/types/cl_uref.rb', line 112
def self.from_json(json)
parsed = JSON.parse(json).deep_symbolize_keys
bytes = CLValueBytesParsers::CLURefBytesParser.decode_base_16(parsed[:bytes])
decoded = bytes[0...-1]
access_rights = bytes.pop
CLURef.new(decoded, access_rights)
end
|
.parse_uref_address(str) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/types/cl_uref.rb', line 67
def self.parse_uref_address(str)
raise ArgumentError.new("Expected a string value of \'uref-\' ") unless str.start_with?("uref-")
raise ArgumentError.new("Expected a value of 3") unless str[0..str.length-1].split('-', 3).size == 3
raise ArgumentError.new("Expected a value of 32") unless str[0..str.length-1].split('-', 3)[1].length/2 == 32
arr = str[0..str.length-1].split('-', 3)
prefix = arr[0]
uref_addr = arr[1]
suffix = arr[2]
uref_byte_length = uref_addr.length / 2
decoded_addr = CLValueBytesParsers::CLURefBytesParser.decode_base_16(uref_addr)
access_rights = suffix.to_i(8)
raise ArgumentError.new("The value of \'access_rights\' is out of range. It must be >= 0 and <= 7. Received #{access_rights}") unless suffix.to_i(10).between?(0, 7)
CLURef.new(decoded_addr, access_rights)
end
|
.to_json(uref) ⇒ Object
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/types/cl_uref.rb', line 101
def self.to_json(uref)
decoded_addr = uref.get_value
access_rights = uref.get_access_rights
decoded_addr_with_access_rights = CLValueBytesParsers::CLURefBytesParser.to_bytes(uref)
encoded_addr_with_access_rights = CLValueBytesParsers::CLURefBytesParser.encode_base_16(decoded_addr_with_access_rights)
json = {"bytes": encoded_addr_with_access_rights, "cl_type": uref.get_cl_type}.to_json
end
|
Instance Method Details
#get_access_rights ⇒ Object
59
60
61
|
# File 'lib/types/cl_uref.rb', line 59
def get_access_rights
@access_rights
end
|
#get_cl_type ⇒ Object
50
51
52
53
|
# File 'lib/types/cl_uref.rb', line 50
def get_cl_type
@cl_type = CLURefType.new
@cl_type.to_string
end
|
#get_size ⇒ Object
63
64
65
|
# File 'lib/types/cl_uref.rb', line 63
def get_size
@value.size
end
|
#get_value ⇒ Object
55
56
57
|
# File 'lib/types/cl_uref.rb', line 55
def get_value
@value
end
|
#to_bytes(str) ⇒ Object
Example: str = “uref-000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f-007”;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/types/cl_uref.rb', line 86
def to_bytes(str)
raise ArgumentError.new("Expected a string value of \'uref-\' ") unless str.start_with?("uref-")
raise ArgumentError.new("Expected a value of 3") unless str[0..str.length-1].split('-', 3).size == 3
raise ArgumentError.new("Expected a value of 32") unless str[0..str.length-1].split('-', 3)[1].length/2 == 32
arr = str[0..str.length-1].split('-', 3)
prefix = arr[0]
uref_addr = arr[1]
suffix = arr[2]
uref_byte_length = uref_addr.length / 2
access_rights = suffix.to_i(8)
raise ArgumentError.new("The value of \'access_rights\' is out of range. It must be >= 0 and <= 7. Received #{access_rights}") unless suffix.to_i(10).between?(0, 7)
uref_addr + [access_rights].pack("C*").unpack1("H*")
end
|