Class: Jimmy::Json::URI

Inherits:
Object
  • 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.

Parameters:

  • uri (String, URI, ::URI)
  • container (true, false) (defaults to: false)

    If true, a / will be appended to the given uri if it was omitted. Otherwise, a # will be appended.



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.

Parameters:

  • other (Pointer, String, ::Array<String>)


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.

Parameters:



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

Returns:

  • (true, false)

See Also:

  • URI::Generic#absolute?


# 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/')
# => "bar#"

Parameters:

  • id (URI, ::URI, String) (defaults to: nil)

    If not nil, the URI will be represented relative to id.



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

#dupURI

Returns:

See Also:

  • Object#dup


71
72
73
# File 'lib/jimmy/json/uri.rb', line 71

def dup
  self.class.new self
end

#fragmentString

Returns:

  • (String)

See Also:

  • URI::Generic#fragment


# File 'lib/jimmy/json/uri.rb', line 102

#fragment=(value) ⇒ Object

Parameters:

  • value (String)

See Also:

  • URI::Generic#fragment=


# File 'lib/jimmy/json/uri.rb', line 102

#hashObject

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

#hostString

Returns:

  • (String)

See Also:

  • URI::Generic#host


# File 'lib/jimmy/json/uri.rb', line 102

#inspectObject

See Also:

  • Object#inspect


44
45
46
# File 'lib/jimmy/json/uri.rb', line 44

def inspect
  "#<#{self.class} #{self}>"
end

#join(other) ⇒ Object Also known as: +

See Also:

  • URI#join


57
58
59
# File 'lib/jimmy/json/uri.rb', line 57

def join(other)
  self.class.new(@uri + other.to_s)
end

#pathString

Returns:

  • (String)

See Also:

  • URI::Generic#path


# File 'lib/jimmy/json/uri.rb', line 102

#path=(value) ⇒ Object

Parameters:

  • value (String)

See Also:

  • URI::Generic#path=


# File 'lib/jimmy/json/uri.rb', line 102

#pointerPointer

Get the fragment of this URI as a Pointer.

Returns:



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.

Parameters:

  • value (String, Pointer, ::Array<String>)


32
33
34
35
36
37
38
39
# 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

#relative?true, false

Returns:

  • (true, false)

See Also:

  • URI::Generic#relative?


# 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.

Returns:

  • (Boolean)


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

Parameters:

Returns:

See Also:

  • URI::Generic#route_to


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