Class: Universa::HashId
- Inherits:
-
RemoteAdapter
- Object
- Delegator
- RemoteAdapter
- Universa::HashId
- Defined in:
- lib/universa/contract.rb
Overview
Adapter for Universa HashId
class, helps to avoid confusion when using different representations of the ID.
Class Method Summary collapse
-
.from_digest(digest_bytes) ⇒ HashId
Construct from binary representation, not to confuse with binary one.
-
.from_string(string_id) ⇒ Object
Construct from string representation of the ID, not to confuse with binary one.
Instance Method Summary collapse
-
#bytes ⇒ String
Get binary representation.
-
#eql?(other) ⇒ Boolean
To use it as a hash key_address.
-
#hash ⇒ Object
To use it as a hash key_address.
-
#to_s ⇒ String
Get string representation.
-
#to_url_safe_string ⇒ String
Converts to URL-safe varianot of base64, as RFC 3548 suggests: the 63:nd / character with the underscore _ the 62:nd + character with the minus -.
Methods inherited from RemoteAdapter
#__getobj__, #__setobj__, #initialize, #inspect, invoke_static, remote_class, remote_class_name
Constructor Details
This class inherits a constructor from Universa::RemoteAdapter
Class Method Details
.from_digest(digest_bytes) ⇒ HashId
Construct from binary representation, not to confuse with binary one.
32 33 34 35 |
# File 'lib/universa/contract.rb', line 32 def self.from_digest(digest_bytes) digest_bytes.force_encoding 'binary' invoke_static 'with_digest', digest_bytes end |
.from_string(string_id) ⇒ Object
Construct from string representation of the ID, not to confuse with binary one. This method takes both regular base64 representation and RFC3548 url-safe modification, as from #to_url_safe_string.
41 42 43 44 |
# File 'lib/universa/contract.rb', line 41 def self.from_string(string_id) string_id.force_encoding('utf-8').gsub('-','+').gsub('_','/') invoke_static 'with_digest', string_id end |
Instance Method Details
#bytes ⇒ String
Get binary representation. It is shorter than string representation but contain non-printable characters and can cause problems if treated like a string. Use #to_s to get string representation instead.
50 51 52 |
# File 'lib/universa/contract.rb', line 50 def bytes get_digest end |
#eql?(other) ⇒ Boolean
To use it as a hash key_address. Same as this == other.
79 80 81 |
# File 'lib/universa/contract.rb', line 79 def eql? other self == other end |
#hash ⇒ Object
To use it as a hash key_address.
74 75 76 |
# File 'lib/universa/contract.rb', line 74 def hash bytes.hash end |
#to_s ⇒ String
Get string representation. It is, actually, base64 encoded string representation. Longer, but could easily be transferred with text protocols.
58 59 60 |
# File 'lib/universa/contract.rb', line 58 def to_s Base64.encode64(get_digest).gsub(/\s/, '') end |
#to_url_safe_string ⇒ String
Converts to URL-safe varianot of base64, as RFC 3548 suggests:
the 63:nd / character with the underscore _
the 62:nd + character with the minus -
Could be decoded safely back with from_string but not (most likely) with JAVA API itself
68 69 70 |
# File 'lib/universa/contract.rb', line 68 def to_url_safe_string Base64.encode64(get_digest).gsub(/\s/, '').gsub('/','_').gsub('+', '-') end |