Class: Code::Object::Base64

Inherits:
Code::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

Constants inherited from Code::Object

INSTANCE_FUNCTIONS, NUMBER_CLASSES

Constants included from Concerns::Shared

Concerns::Shared::COMPOUND_ASSIGNMENT_OPERATORS, Concerns::Shared::OPERATOR_METHOD_ALIASES, Concerns::Shared::SHARED_OPERATORS

Instance Attribute Summary

Attributes included from Concerns::Shared

#functions, #raw

Class Method Summary collapse

Methods inherited from Code::Object

class_documentation, class_functions, code_new, #code_new, documentation, documentation_for, documented_functions_for, function_documentation_for, function_documentation_registry_for, functions, inherited_function_documentation_for, #initialize, instance_functions, maybe, #name, repeat, sorted_dictionary, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #blank?, #call, #code_and, #code_as_json, #code_blank?, #code_class_functions, #code_compare, #code_deep_duplicate, #code_different, #code_documentable_functions, #code_documentation, #code_duplicate, #code_dynamic_call, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_false?, #code_falsy?, code_fetch, #code_fetch, #code_functions, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_has_key?, #code_inclusive_range, #code_inspect, #code_instance_functions, #code_instance_of?, #code_is_a?, #code_itself, #code_less, #code_less_or_equal, #code_name, #code_nothing?, #code_or, #code_presence, #code_presence_in, #code_present?, #code_respond_to?, #code_same_object?, #code_self, #code_send, code_set, #code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_tap, #code_then, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_true?, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #present?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?

Constructor Details

This class inherits a constructor from Code::Object

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