Class: Beowulf::Type::Extension
Constant Summary
ExtensionTypes::TYPES
ExtensionIds::IDS
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Utils
#debug, #error, #extract_signatures, #hexlify, #pakArr, #pakC, #pakHash, #pakI, #pakL!, #pakPubKey, #pakQ, #pakS, #pakStr, #pakc, #pakq, #paks, #send_log, #unhexlify, #varint, #warning
#type
#id
Constructor Details
#initialize(options = {}) ⇒ Extension
Returns a new instance of Extension.
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/beowulf/type/extension.rb', line 10
def initialize(options = {})
opt = options.dup
@type = opt.delete(:type)
opt.each do |k, v|
instance_variable_set("@#{k}", type(@type, k, v))
end
unless Extension::known_extension_names.include? @type
raise ExtensionError, "Unsupported extension type: #{@type}"
end
end
|
Class Method Details
.known_extension_names ⇒ Object
86
87
88
|
# File 'lib/beowulf/type/extension.rb', line 86
def self.known_extension_names
list_extensions.map { |ex| ex["extension"].to_sym }
end
|
.list_extensions ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/beowulf/type/extension.rb', line 74
def self.list_extensions
data_exts = '[
{
"extension": "extension_json_type",
"params": [
"value"
]
}
]'
@list_extensions = JSON.parse(data_exts)
end
|
.param_names(type) ⇒ Object
90
91
92
93
94
95
96
|
# File 'lib/beowulf/type/extension.rb', line 90
def self.param_names(type)
list_extensions.each do |ex|
if ex['extension'].to_sym == type.to_sym
return ex['params'].map(&:to_sym)
end
end
end
|
Instance Method Details
#payload ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/beowulf/type/extension.rb', line 51
def payload
params = {}
params["type"] = @type.to_s
Extension::param_names(@type.to_sym).each do |p|
next unless defined? p
v = instance_variable_get("@#{p}")
next if v.nil?
next if v.class == Beowulf::Type::Future
params[p] = case v
when Beowulf::Type::ExtensionJson
v.to_s
else; v
end
end
params
end
|
#to_bytes ⇒ 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
|
# File 'lib/beowulf/type/extension.rb', line 23
def to_bytes
bytes = [id(@type.to_sym)].pack('C')
Extension::param_names(@type.to_sym).each do |p|
next unless defined? p
v = instance_variable_get("@#{p}")
bytes += v.to_bytes and next if v.respond_to? :to_bytes
bytes += case v
when Symbol then pakStr(v.to_s)
when String then pakStr(v)
when Integer then paks(v)
when TrueClass then pakC(1)
when FalseClass then pakC(0)
when ::Array then pakArr(v)
when ::Hash then pakHash(v)
when ExtensionJson then v.to_bytes
when NilClass then next
else
raise ExtensionError, "Unsupported type: #{v.class}"
end
end
bytes
end
|