Class: Code::Object::Url

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

Constant Summary collapse

CLASS_DOCUMENTATION =
{
  name: "Url",
  description:
    "encodes text for urls, decodes url-escaped text, and parses strings as urls.",
  examples: [
    "Url.encode(\"hello world\")",
    "Url.decode(\"a%2Fb%3Fx%3D1\")",
    "Url.parse(\"https://example.com/a?b=1\")"
  ]
}.freeze
CLASS_FUNCTIONS =
{
  "encode" => {
    name: "encode",
    description: "returns url-escaped text for a value.",
    examples: [
      "Url.encode(\"hello world\")",
      "Url.encode(\"a/b?x=1\")",
      "Url.encode(:hello)"
    ]
  },
  "decode" => {
    name: "decode",
    description: "returns text decoded from url-escaped text.",
    examples: [
      "Url.decode(\"hello+world\")",
      "Url.decode(\"a%2Fb%3Fx%3D1\")",
      "Url.decode(Url.encode(\"a+b\"))"
    ]
  },
  "parse" => {
    name: "parse",
    description:
      "returns a url parsed from a value, or an empty url when parsing fails.",
    examples: [
      "Url.parse(\"https://example.com/a?b=1\")",
      "Url.parse(\"/path\")",
      "Url.parse(\"not a url\")"
    ]
  }
}.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

Instance 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, 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

#initialize(*args, **_kargs, &_block) ⇒ Url

Returns a new instance of Url.



53
54
55
56
57
# File 'lib/code/object/url.rb', line 53

def initialize(*args, **_kargs, &_block)
  self.raw = ::URI.parse(args.first.to_s)
rescue ::URI::InvalidURIError
  self.raw = ::URI.parse("")
end

Class Method Details

.call(**args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/code/object/url.rb', line 59

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

  case code_operator.to_s
  when "encode"
    sig(args) { Object.maybe }
    code_encode(*code_arguments.raw)
  when "decode"
    sig(args) { Object.maybe }
    code_decode(*code_arguments.raw)
  when "parse"
    sig(args) { Object.maybe }
    code_parse(*code_arguments.raw)
  else
    super
  end
end

.code_decode(string = nil) ⇒ Object



84
85
86
# File 'lib/code/object/url.rb', line 84

def self.code_decode(string = nil)
  String.new(CGI.unescape(string.to_s))
end

.code_encode(string = nil) ⇒ Object



78
79
80
81
82
# File 'lib/code/object/url.rb', line 78

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

  String.new(CGI.escape(string.to_s))
end

.code_parse(string = nil) ⇒ Object



88
89
90
# File 'lib/code/object/url.rb', line 88

def self.code_parse(string = nil)
  new(string)
end

.function_documentation(scope) ⇒ Object



47
48
49
50
51
# File 'lib/code/object/url.rb', line 47

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

  {}
end