Class: Code::Object::Base64

Inherits:
Object show all
Defined in:
lib/code/object/base_64.rb

Constant Summary collapse

CLASS_DOCUMENTATION =
{
  name: "Base64",
  description: "encodes and decodes strings with base64 text formats.",
  examples: [
    "Base64.encode(:hello)",
    "Base64.decode(Base64.encode(:hello))",
    "Base64.urlsafe_decode(Base64.urlsafe_encode(\"???\"))"
  ]
}.freeze
CLASS_FUNCTIONS =
{
  "encode" => {
    name: "encode",
    description: "returns base64 text with line breaks for a string.",
    examples: [
      "Base64.encode(:hello)",
      "Base64.encode(:hello_world)",
      "Base64.encode(\"123\")"
    ]
  },
  "encode_64" => {
    name: "encode_64",
    description: "returns base64 text with line breaks for a string.",
    examples: [
      "Base64.encode_64(:hello)",
      "Base64.encode_64(:hello_world)",
      "Base64.encode_64(\"123\")"
    ]
  },
  "decode" => {
    name: "decode",
    description: "returns a string decoded from base64 text.",
    examples: [
      "Base64.decode(\"aGVsbG8=\")",
      "Base64.decode(Base64.encode(:hello))",
      "Base64.decode(\"MTIz\")"
    ]
  },
  "decode_64" => {
    name: "decode_64",
    description: "returns a string decoded from base64 text.",
    examples: [
      "Base64.decode_64(\"aGVsbG8=\")",
      "Base64.decode_64(Base64.encode(:hello))",
      "Base64.decode_64(\"MTIz\")"
    ]
  },
  "strict_encode" => {
    name: "strict_encode",
    description: "returns base64 text without line breaks for a string.",
    examples: [
      "Base64.strict_encode(:hello)",
      "Base64.strict_encode(:hello_world)",
      "Base64.strict_encode(\"123\")"
    ]
  },
  "strict_encode_64" => {
    name: "strict_encode_64",
    description: "returns base64 text without line breaks for a string.",
    examples: [
      "Base64.strict_encode_64(:hello)",
      "Base64.strict_encode_64(:hello_world)",
      "Base64.strict_encode_64(\"123\")"
    ]
  },
  "strict_decode" => {
    name: "strict_decode",
    description: "returns a string decoded from strict base64 text.",
    examples: [
      "Base64.strict_decode(\"aGVsbG8=\")",
      "Base64.strict_decode(Base64.strict_encode(:hello))",
      "Base64.strict_decode(\"MTIz\")"
    ]
  },
  "strict_decode_64" => {
    name: "strict_decode_64",
    description: "returns a string decoded from strict base64 text.",
    examples: [
      "Base64.strict_decode_64(\"aGVsbG8=\")",
      "Base64.strict_decode_64(Base64.strict_encode(:hello))",
      "Base64.strict_decode_64(\"MTIz\")"
    ]
  },
  "urlsafe_encode" => {
    name: "urlsafe_encode",
    description: "returns url-safe base64 text for a string.",
    examples: [
      "Base64.urlsafe_encode(:hello)",
      "Base64.urlsafe_encode(\">>>\")",
      "Base64.urlsafe_encode(\"???\")"
    ]
  },
  "url_safe_encode_64" => {
    name: "url_safe_encode_64",
    description: "returns url-safe base64 text for a string.",
    examples: [
      "Base64.url_safe_encode_64(:hello)",
      "Base64.url_safe_encode_64(\">>>\")",
      "Base64.url_safe_encode_64(\"???\")"
    ]
  },
  "urlsafe_decode" => {
    name: "urlsafe_decode",
    description: "returns a string decoded from url-safe base64 text.",
    examples: [
      "Base64.urlsafe_decode(\"aGVsbG8=\")",
      "Base64.urlsafe_decode(\"Pj4-\")",
      "Base64.urlsafe_decode(\"Pz8_\")"
    ]
  },
  "url_safe_decode_64" => {
    name: "url_safe_decode_64",
    description: "returns a string decoded from url-safe base64 text.",
    examples: [
      "Base64.url_safe_decode_64(\"aGVsbG8=\")",
      "Base64.url_safe_decode_64(\"Pj4-\")",
      "Base64.url_safe_decode_64(\"Pz8_\")"
    ]
  }
}.freeze

Class Method Summary collapse

Class Method Details

.call(**args) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/code/object/base_64.rb', line 132

def self.call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  code_value = code_arguments.code_first

  case code_operator.to_s
  when "encode", "encode_64"
    sig(args) { String }
    code_encode(code_value)
  when "decode", "decode_64"
    sig(args) { String }
    code_decode(code_value)
  when "strict_encode", "strict_encode_64"
    sig(args) { String }
    code_strict_encode(code_value)
  when "strict_decode", "strict_decode_64"
    sig(args) { String }
    code_strict_decode(code_value)
  when "urlsafe_encode", "url_safe_encode_64"
    sig(args) { String }
    code_urlsafe_encode(code_value)
  when "urlsafe_decode", "url_safe_decode_64"
    sig(args) { String }
    code_urlsafe_decode(code_value)
  else
    super
  end
end

.code_decode(string) ⇒ Object



167
168
169
170
171
# File 'lib/code/object/base_64.rb', line 167

def self.code_decode(string)
  code_string = string.to_code

  String.new(::Base64.decode64(code_string.to_s))
end

.code_encode(string) ⇒ Object



161
162
163
164
165
# File 'lib/code/object/base_64.rb', line 161

def self.code_encode(string)
  code_string = string.to_code

  String.new(::Base64.encode64(code_string.to_s))
end

.code_strict_decode(string) ⇒ Object



179
180
181
182
183
# File 'lib/code/object/base_64.rb', line 179

def self.code_strict_decode(string)
  code_string = string.to_code

  String.new(::Base64.strict_decode64(code_string.to_s))
end

.code_strict_encode(string) ⇒ Object



173
174
175
176
177
# File 'lib/code/object/base_64.rb', line 173

def self.code_strict_encode(string)
  code_string = string.to_code

  String.new(::Base64.strict_encode64(code_string.to_s))
end

.code_urlsafe_decode(string) ⇒ Object



191
192
193
194
195
# File 'lib/code/object/base_64.rb', line 191

def self.code_urlsafe_decode(string)
  code_string = string.to_code

  String.new(::Base64.urlsafe_decode64(code_string.to_s))
end

.code_urlsafe_encode(string) ⇒ Object



185
186
187
188
189
# File 'lib/code/object/base_64.rb', line 185

def self.code_urlsafe_encode(string)
  code_string = string.to_code

  String.new(::Base64.urlsafe_encode64(code_string.to_s))
end

.function_documentation(scope) ⇒ Object



126
127
128
129
130
# File 'lib/code/object/base_64.rb', line 126

def self.function_documentation(scope)
  return CLASS_FUNCTIONS if scope == :class

  {}
end