Class: DataObjects::URI

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

Overview

A DataObjects URI is of the form scheme://user:password@host:port/path#fragment

The elements are all optional except scheme and path:

scheme

The name of a DBMS for which you have a do_<scheme> adapter gem installed. If scheme is jdbc, the actual DBMS is in the path followed by a colon.

user

The name of the user to authenticate to the database

password

The password to use in authentication

host

The domain name (defaulting to localhost) where the database is available

port

The TCP/IP port number to use for the connection

path

The name or path to the database

query

Parameters for the connection, for example encoding=utf8

fragment

Not currently known to be in use, but available to the adapters

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ URI

Returns a new instance of URI.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/data_objects/uri.rb', line 49

def initialize(*args)
  if (component = args.first).is_a?(Hash)
    @scheme     = component[:scheme]
    @subscheme  = component[:subscheme]
    @user       = component[:user]
    @password   = component[:password]
    @host       = component[:host]
    @port       = component[:port]
    @path       = component[:path]
    @query      = component[:query]
    @fragment   = component[:fragment]
    @relative   = component[:relative]
  elsif args.size > 1
    warn "DataObjects::URI.new with arguments is deprecated, use a Hash of URI components (#{caller.first})"
    @scheme, @user, @password, @host, @port, @path, @query, @fragment = *args
  else
    raise ArgumentError, "argument should be a Hash of URI components, was: #{args.inspect}"
  end
end

Instance Attribute Details

#fragmentObject (readonly)

Returns the value of attribute fragment.



17
18
19
# File 'lib/data_objects/uri.rb', line 17

def fragment
  @fragment
end

#hostObject (readonly)

Returns the value of attribute host.



17
18
19
# File 'lib/data_objects/uri.rb', line 17

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



17
18
19
# File 'lib/data_objects/uri.rb', line 17

def password
  @password
end

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/data_objects/uri.rb', line 17

def path
  @path
end

#portObject (readonly)

Returns the value of attribute port.



17
18
19
# File 'lib/data_objects/uri.rb', line 17

def port
  @port
end

#queryObject (readonly)

Returns the value of attribute query.



17
18
19
# File 'lib/data_objects/uri.rb', line 17

def query
  @query
end

#schemeObject (readonly)

Returns the value of attribute scheme.



17
18
19
# File 'lib/data_objects/uri.rb', line 17

def scheme
  @scheme
end

#subschemeObject (readonly)

Returns the value of attribute subscheme.



17
18
19
# File 'lib/data_objects/uri.rb', line 17

def subscheme
  @subscheme
end

#userObject (readonly)

Returns the value of attribute user.



17
18
19
# File 'lib/data_objects/uri.rb', line 17

def user
  @user
end

Class Method Details

.parse(uri) ⇒ Object

Make a DataObjects::URI object by parsing a string. Simply delegates to Addressable::URI::parse.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/data_objects/uri.rb', line 20

def self.parse(uri)
  return uri if uri.is_a?(self)

  if uri.is_a?(Addressable::URI)
    scheme = uri.scheme
  elsif uri[0, 4] == 'jdbc'
    scheme = uri[0, 4]
    uri       = Addressable::URI.parse(uri[5, uri.length])
    subscheme = uri&.scheme
  else
    uri       = Addressable::URI.parse(uri)
    scheme    = uri&.scheme
    subscheme = nil
  end

  new(
    scheme: scheme,
    subscheme: subscheme,
    user: uri&.user,
    password: uri&.password,
    host: uri&.host,
    port: uri&.port,
    path: uri&.path,
    query: uri&.query_values,
    fragment: uri&.fragment,
    relative: !uri.to_s.index('//').nil? # basic (naive) check for relativity / opaqueness
  )
end

Instance Method Details

#eql?(other) ⇒ Boolean

Compare this URI to another for hashing

Returns:

  • (Boolean)


100
101
102
# File 'lib/data_objects/uri.rb', line 100

def eql?(other)
  to_s.eql?(other.to_s)
end

#hashObject

Hash this URI



105
106
107
# File 'lib/data_objects/uri.rb', line 105

def hash
  to_s.hash
end

#opaque?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/data_objects/uri.rb', line 69

def opaque?
  !@relative
end

#relative?Boolean

Returns:

  • (Boolean)


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

def relative?
  @relative
end

#to_sObject

Display this URI object as a string



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/data_objects/uri.rb', line 78

def to_s
  string = ''
  string << "#{scheme}:"     if scheme
  string << "#{subscheme}:"  if subscheme
  string << '//'             if relative?
  if user
    string << user.to_s
    string << '@'
  end
  string << host.to_s        if host
  string << ":#{port}"       if port
  string << path.to_s
  if query
    string << '?' << query.map do |key, value|
      "#{key}=#{value}"
    end.join('&')
  end
  string << "##{fragment}" if fragment
  string
end