Class: UniformResourceIdentifier

Inherits:
Object
  • Object
show all
Extended by:
Parsable
Defined in:
lib/uniform_resource_identifier.rb,
lib/uniform_resource_identifier/host.rb,
lib/uniform_resource_identifier/path.rb,
lib/uniform_resource_identifier/query.rb,
lib/uniform_resource_identifier/domain.rb,
lib/uniform_resource_identifier/parser.rb,
lib/uniform_resource_identifier/parsable.rb,
lib/uniform_resource_identifier/relative.rb,
lib/uniform_resource_identifier/authority.rb,
lib/uniform_resource_identifier/user_info.rb

Defined Under Namespace

Modules: Parsable, Parser Classes: Authority, Domain, Host, Path, Query, Relative, UserInfo

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parsable

parse

Constructor Details

#initialize(uri = nil) ⇒ UniformResourceIdentifier

Returns a new instance of UniformResourceIdentifier.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/uniform_resource_identifier.rb', line 11

def initialize(uri=nil)
  if uri.respond_to?(:to_str)
    parsed_uri = Parser.parse(uri)
    
    @protocol = parsed_uri[:protocol]
    
    @authority = Authority.new(
      :user_info => {
        :username => parsed_uri[:username],
        :password => parsed_uri[:password]
      },
      :host => parsed_uri[:host],
      :port => parsed_uri[:port]
    )
    
    @relative = Relative.new(
      :path => {
        :directory => parsed_uri[:directory],
        :file => parsed_uri[:file]
      },
      :query => parsed_uri[:query],
      :anchor => parsed_uri[:anchor]
    )
  elsif uri.respond_to?(:to_hash)
    uri = uri.to_hash.symbolize_keys
    
    @protocol = uri[:protocol].to_s if uri.has_key?(:protocol)
    @authority = Authority.parse(uri[:authority]) if uri.has_key?(:authority)
    @relative = Relative.parse(uri[:relative]) if uri.has_key?(:relative)
  else
    raise(TypeError, "uri must either be a String or a Hash") unless uri.nil?
  end
end

Instance Attribute Details

#protocolObject Also known as: scheme

#

Attributes = #

#


73
74
75
# File 'lib/uniform_resource_identifier.rb', line 73

def protocol
  @protocol
end

Instance Method Details

#==(url) ⇒ Object



45
46
47
48
49
50
# File 'lib/uniform_resource_identifier.rb', line 45

def ==(url)
  return false unless url.respond_to?(:to_s)
  url = self.class.new(url.to_str) if url.respond_to?(:to_str)
  
  url.to_s == self.to_s
end

#absolute?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/uniform_resource_identifier.rb', line 222

def absolute?
  !relative?
end

#anchorObject



206
207
208
# File 'lib/uniform_resource_identifier.rb', line 206

def anchor
  self.relative.anchor
end

#anchor=(anchor) ⇒ Object



210
211
212
# File 'lib/uniform_resource_identifier.rb', line 210

def anchor=(anchor)
  self.relative.anchor = anchor
end

#authorityObject



82
83
84
# File 'lib/uniform_resource_identifier.rb', line 82

def authority
  @authority ||= Authority.new
end

#authority=(authority) ⇒ Object



86
87
88
# File 'lib/uniform_resource_identifier.rb', line 86

def authority=(authority)
  @authority = Authority.parse(authority)
end

#directoryObject



182
183
184
# File 'lib/uniform_resource_identifier.rb', line 182

def directory
  self.relative.path.directory
end

#directory=(directory) ⇒ Object



186
187
188
# File 'lib/uniform_resource_identifier.rb', line 186

def directory=(directory)
  self.relative.path.directory = directory
end

#domainObject



142
143
144
# File 'lib/uniform_resource_identifier.rb', line 142

def domain
  self.authority.host.domain ||= Domain.new
end

#domain=(domain) ⇒ Object



146
147
148
# File 'lib/uniform_resource_identifier.rb', line 146

def domain=(domain)
  self.authority.host.domain = Domain.parse(domain)
end

#fileObject



190
191
192
# File 'lib/uniform_resource_identifier.rb', line 190

def file
  self.relative.path.file
end

#file=(file) ⇒ Object



194
195
196
# File 'lib/uniform_resource_identifier.rb', line 194

def file=(file)
  self.relative.path.file = file
end

#hostObject



126
127
128
# File 'lib/uniform_resource_identifier.rb', line 126

def host
  self.authority.host
end

#host=(host) ⇒ Object



130
131
132
# File 'lib/uniform_resource_identifier.rb', line 130

def host=(host)
  self.authority.host = host
end

#localhost?Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/uniform_resource_identifier.rb', line 226

def localhost?
  !!(self.host =~ /^localhost$/i)
end

#passwordObject



118
119
120
# File 'lib/uniform_resource_identifier.rb', line 118

def password
  self.authority..password
end

#password=(password) ⇒ Object



122
123
124
# File 'lib/uniform_resource_identifier.rb', line 122

def password=(password)
  self.authority..password = password
end

#pathObject



174
175
176
# File 'lib/uniform_resource_identifier.rb', line 174

def path
  self.relative.path
end

#path=(path) ⇒ Object



178
179
180
# File 'lib/uniform_resource_identifier.rb', line 178

def path=(path)
  self.relative.path = path
end

#portObject



166
167
168
# File 'lib/uniform_resource_identifier.rb', line 166

def port
  self.authority.port
end

#port=(port) ⇒ Object



170
171
172
# File 'lib/uniform_resource_identifier.rb', line 170

def port=(port)
  self.authority.port = port
end

#queryObject



198
199
200
# File 'lib/uniform_resource_identifier.rb', line 198

def query
  self.relative.query
end

#query=(query) ⇒ Object



202
203
204
# File 'lib/uniform_resource_identifier.rb', line 202

def query=(query)
  self.relative.query = query
end

#relativeObject



90
91
92
# File 'lib/uniform_resource_identifier.rb', line 90

def relative
  @relative ||= Relative.new
end

#relative=(relative) ⇒ Object



94
95
96
# File 'lib/uniform_resource_identifier.rb', line 94

def relative=(relative)
  @relative = Relative.parse(relative)
end

#relative?Boolean

#

Extras = #

#

Returns:

  • (Boolean)


218
219
220
# File 'lib/uniform_resource_identifier.rb', line 218

def relative?
  self.host.nil?
end

#sldObject



150
151
152
# File 'lib/uniform_resource_identifier.rb', line 150

def sld
  self.authority.host.domain.sld
end

#sld=(sld) ⇒ Object



154
155
156
# File 'lib/uniform_resource_identifier.rb', line 154

def sld=(sld)
  self.authority.host.domain.sld = sld
end

#subdomainObject



134
135
136
# File 'lib/uniform_resource_identifier.rb', line 134

def subdomain
  self.authority.host.subdomain
end

#subdomain=(subdomain) ⇒ Object



138
139
140
# File 'lib/uniform_resource_identifier.rb', line 138

def subdomain=(subdomain)
  self.authority.host.subdomain = subdomain
end

#tldObject



158
159
160
# File 'lib/uniform_resource_identifier.rb', line 158

def tld
  self.authority.host.domain.tld
end

#tld=(tld) ⇒ Object



162
163
164
# File 'lib/uniform_resource_identifier.rb', line 162

def tld=(tld)
  self.authority.host.domain.tld = tld
end

#to_hObject



61
62
63
64
65
66
67
# File 'lib/uniform_resource_identifier.rb', line 61

def to_h
  {
    :protocol => @protocol,
    :authority => @authority.nil? ? nil : @authority.to_h,
    :relative => @relative.nil? ? nil : @relative.to_h
  }
end

#to_sObject

#

Serialization = #

#


56
57
58
59
# File 'lib/uniform_resource_identifier.rb', line 56

def to_s
  protocol = "#{@protocol}://" unless @protocol.nil?
  "#{protocol}#{@authority}#{@relative}"
end

#user_infoObject

#

Delegates = #

#


102
103
104
# File 'lib/uniform_resource_identifier.rb', line 102

def 
  self.authority.
end

#user_info=(user_info) ⇒ Object



106
107
108
# File 'lib/uniform_resource_identifier.rb', line 106

def user_info=()
  self.authority. = 
end

#usernameObject



110
111
112
# File 'lib/uniform_resource_identifier.rb', line 110

def username
  self.authority..username
end

#username=(username) ⇒ Object



114
115
116
# File 'lib/uniform_resource_identifier.rb', line 114

def username=(username)
  self.authority..username = username
end