lib/protocol/http/reference.rb in protocol-http-0.36.0 vs lib/protocol/http/reference.rb in protocol-http-0.37.0
- old
+ new
@@ -1,27 +1,27 @@
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2018-2023, by Samuel Williams.
-require_relative 'url'
+require_relative "url"
module Protocol
module HTTP
# A relative reference, excluding any authority. The path part of an HTTP request.
class Reference
include Comparable
# Generate a reference from a path and user parameters. The path may contain a `#fragment` or `?query=parameters`.
- def self.parse(path = '/', parameters = nil)
- base, fragment = path.split('#', 2)
- path, query = base.split('?', 2)
+ def self.parse(path = "/", parameters = nil)
+ base, fragment = path.split("#", 2)
+ path, query = base.split("?", 2)
self.new(path, query, fragment, parameters)
end
- def initialize(path = '/', query = nil, fragment = nil, parameters = nil)
+ def initialize(path = "/", query = nil, fragment = nil, parameters = nil)
@path = path
@query = query
@fragment = fragment
@parameters = parameters
end
@@ -77,19 +77,19 @@
@fragment and !@fragment.empty?
end
def append(buffer)
if query?
- buffer << URL.escape_path(@path) << '?' << @query
- buffer << '&' << URL.encode(@parameters) if parameters?
+ buffer << URL.escape_path(@path) << "?" << @query
+ buffer << "&" << URL.encode(@parameters) if parameters?
else
buffer << URL.escape_path(@path)
- buffer << '?' << URL.encode(@parameters) if parameters?
+ buffer << "?" << URL.encode(@parameters) if parameters?
end
if fragment?
- buffer << '#' << URL.escape(@fragment)
+ buffer << "#" << URL.escape(@fragment)
end
return buffer
end
@@ -148,65 +148,65 @@
def split(path)
if path.empty?
[path]
else
- path.split('/', -1)
+ path.split("/", -1)
end
end
def expand_absolute_path(path, parts)
parts.each do |part|
- if part == '..'
+ if part == ".."
path.pop
- elsif part == '.'
+ elsif part == "."
# Do nothing.
else
path << part
end
end
- if path.first != ''
- path.unshift('')
+ if path.first != ""
+ path.unshift("")
end
end
def expand_relative_path(path, parts)
parts.each do |part|
- if part == '..' and path.any?
+ if part == ".." and path.any?
path.pop
- elsif part == '.'
+ elsif part == "."
# Do nothing.
else
path << part
end
end
end
# @param pop [Boolean] whether to remove the last path component of the base path, to conform to URI merging behaviour, as defined by RFC2396.
def expand_path(base, relative, pop = true)
- if relative.start_with? '/'
+ if relative.start_with? "/"
return relative
end
path = split(base)
# RFC2396 Section 5.2:
# 6) a) All but the last segment of the base URI's path component is
# copied to the buffer. In other words, any characters after the
# last (right-most) slash character, if any, are excluded.
- path.pop if pop or path.last == ''
+ path.pop if pop or path.last == ""
parts = split(relative)
# Absolute path:
- if path.first == ''
+ if path.first == ""
expand_absolute_path(path, parts)
else
expand_relative_path(path, parts)
end
- return path.join('/')
+ return path.join("/")
end
end
end
end