Class: Virgil::SDK::HighLevel::VirgilBuffer
- Inherits:
-
Struct
- Object
- Struct
- Virgil::SDK::HighLevel::VirgilBuffer
- Defined in:
- lib/virgil/sdk/high_level/virgil_buffer.rb
Overview
This class provides a list of methods that simplify the work with an array of bytes.
Instance Attribute Summary collapse
-
#bytes ⇒ Object
Returns the value of attribute bytes.
Class Method Summary collapse
-
.from_base64(str) ⇒ Object
Initializes a new buffer from specified string, which encodes binary data as base-64 digits.
-
.from_bytes(bytes) ⇒ Object
Initializes a new buffer from array of bytes.
-
.from_file(key_file_path) ⇒ Object
Initializes a new buffer from file.
-
.from_hex(str) ⇒ Object
Initializes a new buffer from specified string, which encodes binary data as hexadecimal digits.
- .from_string(str, encoding = VirgilStringEncoding::UTF8) ⇒ Object
-
.from_utf8(str) ⇒ Object
Initializes a new buffer from specified string, which encodes binary data as utf-8.
Instance Method Summary collapse
-
#initialize(bytes) ⇒ VirgilBuffer
constructor
A new instance of VirgilBuffer.
-
#to_base64 ⇒ Object
Converts all the bytes in current buffer to its equivalent string representation that is encoded with base-64 digits.
-
#to_hex ⇒ Object
Converts the numeric value of each element of a current buffer bytes to its equivalent hexadecimal string representation.
- #to_s ⇒ Object
- #to_string(encoding = VirgilStringEncoding::UTF8) ⇒ Object
-
#to_utf8 ⇒ Object
Decodes all the bytes in current buffer into a string.
Constructor Details
#initialize(bytes) ⇒ VirgilBuffer
Returns a new instance of VirgilBuffer.
44 45 46 47 48 49 |
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 44 def initialize(bytes) self.class.validate_bytes_param(bytes) super end |
Instance Attribute Details
#bytes ⇒ Object
Returns the value of attribute bytes
42 43 44 |
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 42 def bytes @bytes end |
Class Method Details
.from_base64(str) ⇒ Object
Initializes a new buffer from specified string, which encodes binary data as base-64 digits.
105 106 107 |
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 105 def self.from_base64(str) new(Base64.decode64(str).bytes) end |
.from_bytes(bytes) ⇒ Object
Initializes a new buffer from array of bytes
53 54 55 56 57 58 59 |
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 53 def self.from_bytes(bytes) self.validate_bytes_param(bytes) new(bytes) end |
.from_file(key_file_path) ⇒ Object
Initializes a new buffer from file.
97 98 99 100 101 |
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 97 def self.from_file(key_file_path) raise ArgumentError.new("file_path is not valide") unless (File.exist?(key_file_path) && File.readable?(key_file_path)) str = File.read(key_file_path) from_string(str) end |
.from_hex(str) ⇒ Object
Initializes a new buffer from specified string, which encodes binary data as hexadecimal digits.
117 118 119 |
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 117 def self.from_hex(str) new(str.scan(/../).map { |x| x.hex }) end |
.from_string(str, encoding = VirgilStringEncoding::UTF8) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 62 def self.from_string(str, encoding=VirgilStringEncoding::UTF8) case encoding when VirgilStringEncoding::BASE64 return self.from_base64(str) when VirgilStringEncoding::HEX return self.from_hex(str) when VirgilStringEncoding::UTF8 return self.from_utf8(str) else raise ArgumentError.new("encoding is undefined") end end |
.from_utf8(str) ⇒ Object
Initializes a new buffer from specified string, which encodes binary data as utf-8.
111 112 113 |
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 111 def self.from_utf8(str) new(str.bytes) end |
Instance Method Details
#to_base64 ⇒ Object
Converts all the bytes in current buffer to its equivalent string representation that is encoded with base-64 digits.
124 125 126 |
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 124 def to_base64 Base64.strict_encode64(to_s) end |
#to_hex ⇒ Object
Converts the numeric value of each element of a current buffer bytes to its equivalent hexadecimal string representation.
137 138 139 |
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 137 def to_hex to_s.each_byte.map { |b| b.to_s(16) }.join end |
#to_s ⇒ Object
92 93 94 |
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 92 def to_s bytes.pack('c*') end |
#to_string(encoding = VirgilStringEncoding::UTF8) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 78 def to_string(encoding=VirgilStringEncoding::UTF8) case encoding when VirgilStringEncoding::BASE64 return self.to_base64 when VirgilStringEncoding::HEX return self.to_hex when VirgilStringEncoding::UTF8 return to_s else raise ArgumentError.new("encoding is undefined") end end |
#to_utf8 ⇒ Object
Decodes all the bytes in current buffer into a string.
130 131 132 |
# File 'lib/virgil/sdk/high_level/virgil_buffer.rb', line 130 def to_utf8 to_s end |