Module: Elixir::Base

Defined in:
lib/elixir/base.rb

Constant Summary collapse

UPPERCASE =
/[[:upper:]]/.freeze
LOWERCASE =
/[[:lower:]]/.freeze

Class Method Summary collapse

Class Method Details

.decode16(string, char_case: :upper) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/elixir/base.rb', line 11

def decode16 string, char_case: :upper
  case char_case
  when :upper
    return :error if string =~ LOWERCASE
  when :lower
    return :error if string =~ UPPERCASE
  end

  [:ok, [string].pack('H*')] rescue :error
end

.decode16!(string, char_case: :upper) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/elixir/base.rb', line 22

def decode16! string, char_case: :upper
  case char_case
  when :upper
    raise ArgumentError if string =~ LOWERCASE
  when :lower
    raise ArgumentError if string =~ UPPERCASE
  end

  [string].pack 'H*'
end

.decode32(string, char_case: :upper) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/elixir/base.rb', line 33

def decode32 string, char_case: :upper
  case char_case
  when :upper
    return :error if string =~ LOWERCASE
  when :lower
    return :error if string =~ UPPERCASE
  end

  [:ok, Base32.decode(string)] rescue :error
end

.decode32!(string, char_case: :upper) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/elixir/base.rb', line 44

def decode32! string, char_case: :upper
  case char_case
  when :upper
    raise ArgumentError if string =~ LOWERCASE
  when :lower
    raise ArgumentError if string =~ UPPERCASE
  end

  Base32.decode string
end

.decode64(string) ⇒ Object



55
56
57
# File 'lib/elixir/base.rb', line 55

def decode64 string
  [:ok, Base64.strict_decode64(string)] rescue :error
end

.decode64!(string) ⇒ Object



59
60
61
# File 'lib/elixir/base.rb', line 59

def decode64! string
  Base64.strict_decode64 string
end

.encode16(data, char_case: :upper) ⇒ Object



63
64
65
66
67
# File 'lib/elixir/base.rb', line 63

def encode16 data, char_case: :upper
  encoded = data.unpack('H*').first

  char_case == :upper ? encoded.upcase : encoded
end

.encode32(data, char_case: :upper) ⇒ Object



69
70
71
72
73
# File 'lib/elixir/base.rb', line 69

def encode32 data, char_case: :upper
  encoded = Base32.encode data

  char_case == :upper ? encoded.upcase : encoded
end

.encode64(data) ⇒ Object



75
76
77
# File 'lib/elixir/base.rb', line 75

def encode64 data
  Base64.strict_encode64(data)
end

.hex_decode32(string, char_case: :upper) ⇒ Object



83
84
85
# File 'lib/elixir/base.rb', line 83

def hex_decode32 string, char_case: :upper
  # TODO
end

.hex_decode32!(string, char_case: :upper) ⇒ Object



87
88
89
# File 'lib/elixir/base.rb', line 87

def hex_decode32! string, char_case: :upper
  # TODO
end

.hex_encode32(string, char_case: :upper) ⇒ Object



79
80
81
# File 'lib/elixir/base.rb', line 79

def hex_encode32 string, char_case: :upper
  # TODO
end

.url_decode64(string) ⇒ Object



91
92
93
# File 'lib/elixir/base.rb', line 91

def url_decode64 string
  # TODO
end

.url_decode64!(string) ⇒ Object



95
96
97
# File 'lib/elixir/base.rb', line 95

def url_decode64! string
  # TODO
end