Class: Code::Object::Url
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
Class Method Summary collapse
- .call(**args) ⇒ Object
- .code_decode(string = nil) ⇒ Object
- .code_encode(string = nil) ⇒ Object
- .code_parse(string = nil) ⇒ Object
- .function_documentation(scope) ⇒ Object
Instance Method Summary collapse
-
#initialize(*args, **_kargs, &_block) ⇒ Url
constructor
A new instance of Url.
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 |