Class: Diva::URI

Inherits:
Object
  • Object
show all
Defined in:
lib/diva/uri.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ URI



34
35
36
37
38
39
40
41
42
43
# File 'lib/diva/uri.rb', line 34

def initialize(uri)
  case uri.freeze
  when URI, Addressable::URI
    @uri = uri
  when String
    @uri_string = uri
  when Hash
    @uri_hash = uri
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *rest, &block) ⇒ Object



103
104
105
# File 'lib/diva/uri.rb', line 103

def method_missing(method, *rest, &block)
  to_uri.__send__(method, *rest, &block)
end

Instance Method Details

#==(other) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/diva/uri.rb', line 45

def ==(other)
  case other
  when URI, Addressable::URI
    other == to_uri
  when Diva::URI
    if has_string? or other.has_string?
      to_s == other.to_s
    else
      other.to_uri == to_uri
    end
  end
end

#freezeObject



91
92
93
94
95
96
97
# File 'lib/diva/uri.rb', line 91

def freeze
  unless frozen?
    to_uri
    to_s
  end
  super
end

#has_string?Boolean



62
63
64
# File 'lib/diva/uri.rb', line 62

def has_string?
  !!@uri_string
end

#has_uri?Boolean



66
67
68
# File 'lib/diva/uri.rb', line 66

def has_uri?
  !!@uri
end

#hashObject



58
59
60
# File 'lib/diva/uri.rb', line 58

def hash
  to_s.hash ^ self.class.hash
end

#respond_to?(method) ⇒ Boolean



99
100
101
# File 'lib/diva/uri.rb', line 99

def respond_to?(method)
  super or to_uri.respond_to?(method)
end

#schemeObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/diva/uri.rb', line 78

def scheme
  if has_string? and !has_uri?
    match = @uri_string.match(%r<\A(\w+):>)
    if match
      match[1]
    else
      to_uri.scheme
    end
  else
    to_uri.scheme
  end
end

#to_sObject



70
71
72
# File 'lib/diva/uri.rb', line 70

def to_s
  @uri_string ||= to_uri.to_s.freeze
end

#to_uriObject



74
75
76
# File 'lib/diva/uri.rb', line 74

def to_uri
  @uri ||= generate_uri.freeze
end