Class: Net::Ping::LDAP

Inherits:
Net::Ping show all
Defined in:
lib/net/ping/ldap.rb

Overview

The Ping::LDAP class encapsulates methods for LDAP pings.

Constant Summary

Constant Summary

Constants inherited from Net::Ping

VERSION

Instance Attribute Summary (collapse)

Attributes inherited from Net::Ping

#duration, #exception, #host, #port, #timeout, #warning

Instance Method Summary (collapse)

Constructor Details

- (LDAP) initialize(uri = nil, timeout = 5)

Creates and returns a new Ping::LDAP object. The default timeout is 5 seconds.

uri string is expected to be a full URI with scheme (ldap/ldaps) and optionally the port (else default port is assumed) e.g.

ldap://my.ldap.host.com
ldap://my.ldap.host.com:1389
ldaps://my.ldap.host.com
ldaps://my.ldap.host.com:6636

If a plain hostname is provided as the uri, a default port of 389 is assumed



42
43
44
45
# File 'lib/net/ping/ldap.rb', line 42

def initialize(uri=nil, timeout=5)
  host, port = decode_uri(uri)
  super(host, port, timeout)
end

Instance Attribute Details

- (Object) encryption

set/get the encryption method. By default nil, but may be set to :simple_tls



25
26
27
# File 'lib/net/ping/ldap.rb', line 25

def encryption
  @encryption
end

- (Object) password

Returns the value of attribute password



20
21
22
# File 'lib/net/ping/ldap.rb', line 20

def password
  @password
end

- (Object) uri

uri contains the URI object for the request



14
15
16
# File 'lib/net/ping/ldap.rb', line 14

def uri
  @uri
end

- (Object) username

username and password may be set for ping using an authenticated LDAP bind



19
20
21
# File 'lib/net/ping/ldap.rb', line 19

def username
  @username
end

Instance Method Details

- (Object) config

constructs the LDAP configuration structure



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/net/ping/ldap.rb', line 64

def config
  {
    :host => uri.host,
    :port => uri.port,
    :encryption => encryption
  }.merge(
    (username && password) ?
    { :auth => {:method => :simple, :username => username, :password => password} } :
    { :auth => {:method => :anonymous} }
  )
end

- (Object) decode_uri(value)

method used to decode uri string



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/net/ping/ldap.rb', line 49

def decode_uri(value)
  @uri = URI.parse(value)
  if uri.scheme =~ /ldap/
    p = @port = uri.port
    h = @host = uri.host
    @encryption = uri.scheme=='ldaps' ? :simple_tls : nil
  else
    h = value
    p = 389
  end
  [h, p]
end

- (Object) ping(host = nil) Also known as: ping?, pingecho

perform ping, optionally providing the ping destination uri



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/net/ping/ldap.rb', line 78

def ping(host = nil)
  decode_uri(host) if host
  super(@host)
  
  bool = false

  start_time = Time.now

  begin
    Timeout.timeout(@timeout) do
      Net::LDAP.new( config  ).bind
    end
  rescue Net::LDAP::LdapError => e
    @exception = e.message
  rescue Exception => e
    @exception = e.message
  else
    bool = true
  end

  # There is no duration if the ping failed
  @duration = Time.now - start_time if bool

  bool
end