Method: UniformResourceIdentifier::Authority#initialize

Defined in:
lib/uniform_resource_identifier/authority.rb

#initialize(authority = nil) ⇒ Authority

Returns a new instance of Authority.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/uniform_resource_identifier/authority.rb', line 11

def initialize(authority=nil)
  if authority.respond_to?(:to_str)
    authority = Parser.parse(authority)
    
    port = authority[:port].blank? ? nil : authority[:port].to_i if authority.has_key?(:port)
    
    @user_info = UserInfo.parse(authority[:user_info]) if authority.has_key?(:user_info)
    @host = Host.parse(authority[:host]) if authority.has_key?(:host)
    @port = port
  elsif authority.respond_to?(:to_hash)
    authority = authority.to_hash.symbolize_keys
    
    port = authority[:port].blank? ? nil : authority[:port].to_i if authority.has_key?(:port)
    
    @user_info = UserInfo.parse(authority[:user_info]) if authority.has_key?(:user_info)
    @host = Host.parse(authority[:host]) if authority.has_key?(:host)
    
    @port = port
  else
    raise(TypeError, "authority must either be a String or a Hash") unless authority.nil?
  end
end