Module: Multibases

Defined in:
lib/multibases.rb,
lib/multibases/bare.rb,
lib/multibases/base2.rb,
lib/multibases/error.rb,
lib/multibases/base16.rb,
lib/multibases/base32.rb,
lib/multibases/base64.rb,
lib/multibases/base_x.rb,
lib/multibases/version.rb,
lib/multibases/registry.rb,
lib/multibases/ord_table.rb,
lib/multibases/byte_array.rb

Defined Under Namespace

Classes: AlphabetEncodingInvalid, AlphabetOutOfRange, Base16, Base2, Base32, Base64, BaseX, ByteArray, DecodedByteArray, Encoded, EncodedByteArray, Error, Identity, IndexedOrdTable, MissingEncoding, NoEngine, OrdTable, Registration

Constant Summary collapse

VERSION =
'0.3.2'
IMPLEMENTATIONS =

rubocop:disable Style/MutableConstant

{}

Class Method Summary collapse

Class Method Details

.[](entry) ⇒ Object



26
27
28
# File 'lib/multibases/registry.rb', line 26

def [](entry)
  find_by(code: entry, encoding: entry)
end

.code(encoding) ⇒ Object



123
124
125
# File 'lib/multibases/bare.rb', line 123

def code(encoding)
  fetch_by!(encoding: encoding).code
end

.codesObject



52
53
54
# File 'lib/multibases/registry.rb', line 52

def codes
  Multibases::IMPLEMENTATIONS.values.map(&:code)
end

.decode(data, *args) ⇒ Object



114
115
116
117
# File 'lib/multibases/bare.rb', line 114

def decode(data, *args)
  encoded = Multibases.unpack(data)
  encoded.decode(*args)
end

.decorate(encoded, encoding = nil) ⇒ Object

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/multibases/bare.rb', line 95

def decorate(encoded, encoding = nil)
  return encoded.pack if encoded.is_a?(Encoded)
  raise ArgumentError, 'Missing encoding of encoded data' unless encoding

  encoded = encoded.bytes unless encoded.is_a?(Array)

  Encoded.new(
    Multibases.code(encoding),
    encoding,
    encoded.length,
    EncodedByteArray.new(encoded)
  ).pack
end

.encode(encoding, data, engine = Multibases.engine(encoding)) ⇒ Object

Raises:



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/multibases/bare.rb', line 69

def encode(encoding, data, engine = Multibases.engine(encoding))
  raise NoEngine, encoding unless engine

  encoded_data = engine.encode(data)

  Encoded.new(
    Multibases.code(encoding),
    encoding,
    encoded_data.length,
    encoded_data
  )
end

.encoding(code) ⇒ Object



119
120
121
# File 'lib/multibases/bare.rb', line 119

def encoding(code)
  fetch_by!(code: code).encoding
end

.engine(lookup) ⇒ Object

Raises:



127
128
129
130
131
132
# File 'lib/multibases/bare.rb', line 127

def engine(lookup)
  registration = find_by(code: lookup, encoding: lookup)
  raise NoEngine, lookup unless registration

  registration.engine
end

.fetch_by!(code: nil, encoding: nil) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/multibases/registry.rb', line 38

def fetch_by!(code: nil, encoding: nil)
  return Multibases::IMPLEMENTATIONS.fetch(encoding) if encoding

  Multibases.find_by(code: code).tap do |found|
    raise KeyError, "No implementation has code #{code}" unless found
  end
end

.find_by(code: nil, encoding: nil) ⇒ Object



46
47
48
49
50
# File 'lib/multibases/registry.rb', line 46

def find_by(code: nil, encoding: nil)
  Multibases::IMPLEMENTATIONS.values.find do |v|
    v == code || v == encoding
  end
end

.implement(encoding, code, implementation = nil, alphabet = nil, alphabet_encoding = nil) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/multibases/registry.rb', line 30

def implement(encoding, code, implementation = nil, alphabet = nil, alphabet_encoding = nil)
  Multibases::IMPLEMENTATIONS[encoding] = Registration.new(
    code,
    encoding,
    implementation&.new(alphabet, encoding: alphabet_encoding)
  )
end

.multibase_version(multibase_semver = nil) ⇒ Object



60
61
62
63
64
# File 'lib/multibases/registry.rb', line 60

def multibase_version(multibase_semver = nil)
  return @multibase_version if multibase_semver.nil?

  @multibase_version = multibase_semver
end

.namesObject



56
57
58
# File 'lib/multibases/registry.rb', line 56

def names
  Multibases::IMPLEMENTATIONS.keys
end

.pack(*args) ⇒ Object



109
110
111
112
# File 'lib/multibases/bare.rb', line 109

def pack(*args)
  encoded = Multibases.encode(*args)
  encoded.pack
end

.unpack(decorated) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/multibases/bare.rb', line 82

def unpack(decorated)
  decorated = decorated.pack('c*') if decorated.is_a?(Array)
  code = decorated[0]
  encoded_data = decorated[1..-1]

  Encoded.new(
    code,
    Multibases.encoding(code),
    encoded_data.length,
    EncodedByteArray.new(encoded_data.bytes)
  )
end