Class: Jimmy::Json::URI
- Inherits:
-
Object
show all
- Defined in:
- lib/jimmy/json/uri.rb
Overview
Wraps the URI class to provide additional functionality.
Constant Summary
collapse
- FRAGMENT_ESCAPING =
Take from the back of URI::RFC3986_Parser::RFC3986_URI
%r{[^!$&-.0-;=@-Z_a-z~/?]}.freeze
Instance Method Summary
collapse
Constructor Details
#initialize(uri, container: false) ⇒ URI
Returns a new instance of URI.
15
16
17
18
19
20
21
22
|
# 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
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
130
131
132
133
134
135
136
137
138
139
140
141
|
# 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
|
Instance Method Details
#/(other) ⇒ Object
Return a new URI with the given pointer appended.
65
66
67
|
# File 'lib/jimmy/json/uri.rb', line 65
def /(other)
dup.tap { |uri| uri.pointer += other }
end
|
#==(other) ⇒ Object
Also known as:
eql?
Returns true if other
represents the same URI as self.
50
51
52
|
# File 'lib/jimmy/json/uri.rb', line 50
def ==(other)
other.is_a?(self.class) && other.to_s == to_s
end
|
#absolute? ⇒ true, false
|
# File 'lib/jimmy/json/uri.rb', line 102
|
#as_json(id: nil) ⇒ Object
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/')
88
89
90
91
92
93
|
# 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
|
#dup ⇒ URI
71
72
73
|
# File 'lib/jimmy/json/uri.rb', line 71
def dup
self.class.new self
end
|
#fragment ⇒ String
|
# File 'lib/jimmy/json/uri.rb', line 102
|
#fragment=(value) ⇒ Object
|
# File 'lib/jimmy/json/uri.rb', line 102
|
#hash ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
76
77
78
|
# File 'lib/jimmy/json/uri.rb', line 76
def hash
[self.class, @uri].hash
end
|
#host ⇒ String
|
# File 'lib/jimmy/json/uri.rb', line 102
|
#inspect ⇒ Object
44
45
46
|
# File 'lib/jimmy/json/uri.rb', line 44
def inspect
"#<#{self.class} #{self}>"
end
|
#join(other) ⇒ Object
Also known as:
+
57
58
59
|
# File 'lib/jimmy/json/uri.rb', line 57
def join(other)
self.class.new(@uri + other.to_s)
end
|
#path ⇒ String
|
# File 'lib/jimmy/json/uri.rb', line 102
|
#path=(value) ⇒ Object
|
# File 'lib/jimmy/json/uri.rb', line 102
|
Get the fragment of this URI as a Pointer.
26
27
28
|
# File 'lib/jimmy/json/uri.rb', line 26
def pointer
Pointer.new ::URI.decode_www_form_component(fragment)
end
|
#pointer=(value) ⇒ Object
Set the fragment of this URI using a Pointer.
32
33
34
35
36
37
38
39
|
# File 'lib/jimmy/json/uri.rb', line 32
def pointer=(value)
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
|
#relative? ⇒ true, false
|
# File 'lib/jimmy/json/uri.rb', line 102
|
#respond_to_missing?(name) ⇒ Boolean
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
125
126
127
|
# File 'lib/jimmy/json/uri.rb', line 125
def respond_to_missing?(name, *)
@uri.respond_to? name or super
end
|
#route_to(other) ⇒ Json::URI
98
99
100
|
# File 'lib/jimmy/json/uri.rb', line 98
def route_to(other)
self.class.new(@uri.route_to other.to_s)
end
|