class Jimmy::Json::URI
Wraps the URI
class to provide additional functionality.
Constants
- FRAGMENT_ESCAPING
Take from the back of URI::RFC3986_Parser::RFC3986_URI
Public Class Methods
@param [String, URI
, ::URI] uri @param [true, false] container If true, a /
will be appended to the
given +uri+ if it was omitted. Otherwise, a +#+ will be appended.
# File lib/jimmy/json/uri.rb, line 15 def initialize(uri, container: false) @uri = ::URI.parse(uri.to_s) if container @uri.path += '/' unless @uri.path.end_with? '/' else @uri.fragment ||= '' end end
Public Instance Methods
Return a new URI
with the given pointer appended. @param [Pointer, String, ::Array<String>] other
# File lib/jimmy/json/uri.rb, line 65 def /(other) dup.tap { |uri| uri.pointer += other } end
Returns true if other
represents the same URI
as self. @param [URI] other
# File lib/jimmy/json/uri.rb, line 50 def ==(other) other.is_a?(self.class) && other.to_s == to_s end
Get this URI
as a string. If id
is given, the string will be this URI
relative to the given URI
.
uri = Jimmy::Json::URI.new('http://example.com/foo/bar#') uri.as_json(id: 'http://example.com/foo/') # => "bar#"
@param [URI, ::URI, String] id If not nil, the URI
will be represented
relative to +id+.
# File lib/jimmy/json/uri.rb, line 88 def as_json(id: nil, **) return to_s unless id id = URI.new(id) id.absolute? ? id.route_to(id + self).to_s : to_s end
@see ::Object#dup @return [URI]
# File lib/jimmy/json/uri.rb, line 71 def dup self.class.new self end
@api private
# File lib/jimmy/json/uri.rb, line 76 def hash [self.class, @uri].hash end
@see ::Object#inspect
# File lib/jimmy/json/uri.rb, line 44 def inspect "#<#{self.class} #{self}>" end
@see ::URI#join
# File lib/jimmy/json/uri.rb, line 57 def join(other) self.class.new(@uri + other.to_s) end
@api private
# File lib/jimmy/json/uri.rb, line 130 def method_missing(symbol, *args, &block) if @uri.respond_to? symbol self.class.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{symbol}(*args, &block) @uri.__send__ :#{symbol}, *args, &block end RUBY return __send__ symbol, *args, &block end super end
Get the fragment of this URI
as a {Pointer}. @return [Pointer]
# File lib/jimmy/json/uri.rb, line 26 def pointer Pointer.new ::URI.decode_www_form_component(fragment) end
Set the fragment of this URI
using a {Pointer}. @param [String, Pointer
, ::Array<String>] value
# File lib/jimmy/json/uri.rb, line 32 def pointer=(value) # Loosely based on URI.encode_www_form_component fragment = Pointer.new(value).to_s.dup fragment.force_encoding Encoding::ASCII_8BIT fragment.gsub!(FRAGMENT_ESCAPING) { |chr| '%%%02X' % chr.ord } fragment.force_encoding Encoding::US_ASCII self.fragment = fragment end
@api private
# File lib/jimmy/json/uri.rb, line 125 def respond_to_missing?(name, *) @uri.respond_to? name or super end
@see URI::Generic#route_to @param [Json::URI, URI
, String] other @return [Json::URI]
# File lib/jimmy/json/uri.rb, line 98 def route_to(other) self.class.new(@uri.route_to other.to_s) end