Module: CWID

Defined in:
lib/cwid.rb,
lib/cwid/person.rb,
lib/cwid/configuration.rb

Defined Under Namespace

Classes: Configuration, Person

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration


10
11
12
# File 'lib/cwid.rb', line 10

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ CWID::Configuration

Configure the Gem's credentials.

Parameters:

  • (Proc)

Yields:

Returns:


16
17
18
19
20
21
22
23
# File 'lib/cwid.rb', line 16

def configure
  # Create a new configuration and yield it to a block
  self.configuration ||= Configuration.new

  yield(configuration)

  return configuration
end

.connected?Boolean

Check and see if the Gem can communicate with the LDAP server.

Returns:

  • (Boolean)

    True for connected, false for not.


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cwid.rb', line 103

def connected?
  result = true

  begin
    req = self.connection.get do |r|
      r.url self.configuration.search_path
      r.options.timeout = 2
      r.options.open_timeout = 1
    end
  rescue Faraday::ConnectionFailed
    result = false
  end

  result
end

.connectionFaraday::Connection

The Faraday connection being used by the Gem.

Returns:

  • (Faraday::Connection)

    The Faraday connection.


92
93
94
95
96
97
98
# File 'lib/cwid.rb', line 92

def connection
  Faraday.new(url: self.configuration.base_url) do |f|
    f.response :xml, :content_type => /\bxml$/
    f.adapter :net_http
    f.basic_auth self.configuration.username, self.configuration.password
  end
end

.format(cwid) ⇒ String

Formats a CWID to remove special characters and extra numbers.

Parameters:

  • cwid (Integer, String)

    The CWID to be formatted

Returns:

  • (String)

    The formatted CWID


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cwid.rb', line 71

def format(cwid)
  # Convert the input to a string
  cwid = cwid.to_s

  # Remove anything other than the numbers
  cwid = cwid.gsub(/[^0-9 ]/, '')

  # Remove spaces
  cwid = cwid.gsub(/\s+/, '')

  # If it's more than 8 characters, it has the card code as well
  if cwid.length > 8
    cwid = cwid[0..7]
  end

  cwid
end

.lookup(terms = {}) ⇒ Person

Perform a lookup using specific terms.

Parameters:

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

    The search terms in a hash (`cwid: 20045405`, `name: “Douglas Adams”`).

Returns:

  • (Person)

    The person that belongs to that CWID.


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cwid.rb', line 29

def lookup(terms = {})
  # Format the CWID if that's what we're getting.
  if terms[:cwid]
    terms = format(terms.values.first)
  else
    terms = terms.values.first
  end

  # Make the request to the LDAP server
  req = self.connection.get do |r|
    r.url self.configuration.search_path + terms
  end

  # Return nil if there's a nil directory return
  if req.body['directory'].nil?
    return []
  end

  # Return nil if there aren't any results
  if req.body['directory'] && (req.body['directory'].empty? || req.body['directory'].nil?)
    return []

  # Otherwise, create a new Person out of it
  else
    results = []

    if req.body['directory']['person'].is_a?(Hash)
      results << Person.new(req.body['directory']['person'])
    else
      req.body['directory']['person'].each do |r|
        results << Person.new(r)
      end
    end

    return results
  end
end