Class: DN

Inherits:
Object show all
Defined in:
lib/dnc/dn.rb

Overview

rubocop:disable ClassLength Accepts various DN strings and returns a DN object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ DN

Initialize the instance

NOTE: opts[transformation] defaults to "upcase"; use "to_s" for no change. NOTE: opts[:string_order] is a last resort config, defaults to RFC4514 spec.

Parameters:

  • opts (Hash) (defaults to: {})

    Options hash for new DN instance attribute values

  • opts (:dn_string) (defaults to: {})

    [String] The DN string you want to parse into a DN

  • opts (:logger) (defaults to: {})

    User provided logger vs Rails / Logging default logger

  • opts (:transformation) (defaults to: {})

    [String] String method for changing DN.

  • opts (:delimiter) (defaults to: {})

    [String] Specify a custom delimiter for dn_string

  • opts (:string_order) (defaults to: {})

    [Array] Specify the order of RDNs for .to_s



27
28
29
30
31
32
33
34
35
36
# File 'lib/dnc/dn.rb', line 27

def initialize(opts = {})
  @dn_string = opts[:dn_string]
  raise 'dnc: dn_string parameter is **required**' if dn_string.nil?
  @original_dn    = dn_string
  @logger         = opts[:logger] || logger
  @transformation = opts[:transformation] || 'upcase'
  @string_order   = opts[:string_order] || %w(cn l st o ou c street dc uid)
  @delimiter      = opts[:delimiter] || identify_delimiter
  format_dn
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object (private)



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/dnc/dn.rb', line 149

def method_missing(method_name)
  # Catch methods that end with _string
  method_match = method_name.to_s.match(/(.+)_string\z/)
  unless method_match.blank?
    method = method_match[1]
    method_class = send(method.to_sym).class
    return send(:dynamic_strings, method.to_s, method_class)
  end

  super
end

Instance Attribute Details

#cObject

Returns the value of attribute c.



13
14
15
# File 'lib/dnc/dn.rb', line 13

def c
  @c
end

#cnObject

Returns the value of attribute cn.



13
14
15
# File 'lib/dnc/dn.rb', line 13

def cn
  @cn
end

#dcObject

Returns the value of attribute dc.



13
14
15
# File 'lib/dnc/dn.rb', line 13

def dc
  @dc
end

#delimiterObject

Returns the value of attribute delimiter.



13
14
15
# File 'lib/dnc/dn.rb', line 13

def delimiter
  @delimiter
end

#dn_stringObject

Returns the value of attribute dn_string.



13
14
15
# File 'lib/dnc/dn.rb', line 13

def dn_string
  @dn_string
end

#lObject

Returns the value of attribute l.



13
14
15
# File 'lib/dnc/dn.rb', line 13

def l
  @l
end

#oObject

Returns the value of attribute o.



13
14
15
# File 'lib/dnc/dn.rb', line 13

def o
  @o
end

#original_dnObject

Returns the value of attribute original_dn.



13
14
15
# File 'lib/dnc/dn.rb', line 13

def original_dn
  @original_dn
end

#ouObject

Returns the value of attribute ou.



13
14
15
# File 'lib/dnc/dn.rb', line 13

def ou
  @ou
end

#stObject

Returns the value of attribute st.



13
14
15
# File 'lib/dnc/dn.rb', line 13

def st
  @st
end

#streetObject

Returns the value of attribute street.



13
14
15
# File 'lib/dnc/dn.rb', line 13

def street
  @street
end

#string_orderObject

Returns the value of attribute string_order.



13
14
15
# File 'lib/dnc/dn.rb', line 13

def string_order
  @string_order
end

#transformationObject

Returns the value of attribute transformation.



13
14
15
# File 'lib/dnc/dn.rb', line 13

def transformation
  @transformation
end

#uidObject

Returns the value of attribute uid.



13
14
15
# File 'lib/dnc/dn.rb', line 13

def uid
  @uid
end

Instance Method Details

#loggerObject

logger method to return Rails logger if defined, else logging logger



39
40
41
42
43
44
45
# File 'lib/dnc/dn.rb', line 39

def logger
  unless defined? @logger
    logger = Logging.logger[self]
    @logger = Kernel.const_defined?('Rails') ? Rails.logger : logger
  end
  @logger
end

#split_by_delimiterObject

Split passed DN by identified delimiter



61
62
63
# File 'lib/dnc/dn.rb', line 61

def split_by_delimiter
  dn_string.split(delimiter).reject(&:empty?)
end

#to_sObject

Convert DN object into a string (order follows RFC4514 LDAP specifications)



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dnc/dn.rb', line 48

def to_s
  return_string = ''
  @string_order.each do |string_name|
    unless send(string_name.to_sym).blank?
      return_string += ',' unless return_string.empty?
      return_string += send("#{string_name}_string".to_sym)
    end
  end

  return_string
end