class Aspire::UserLookup

Implements a hash of User instances indexed by URI The hash can be populated from an Aspire All User Profiles report CSV file

Attributes

store[RW]

@!attribute [rw] store

@return [Object] a hash-like object mapping user URIs to their JSON data

Public Class Methods

new(filename: nil, store: nil) click to toggle source

Initialises a new UserLookup instance @see (Hash#initialize) @param filename [String] the filename of the CSV file to populate the hash @return [void]

# File lib/aspire/user_lookup.rb, line 18
def initialize(filename: nil, store: nil)
  self.store = store || {}
  load(filename) if filename
end

Public Instance Methods

[](uri, factory = nil) click to toggle source

Returns an Aspire::Object::User instance for a URI @param uri [String] the URI of the user @param factory [Aspire::Object::Factory] the data object factory @return [Aspire::Object::User] the user

# File lib/aspire/user_lookup.rb, line 27
def [](uri, factory = nil)
  data = store[uri]
  data.nil? ? nil : Aspire::Object::User.new(uri, factory, json: data)
end
load(filename = nil) click to toggle source

Populates the store from an All User Profiles report CSV file @param filename [String] the filename of the CSV file @return [void]

# File lib/aspire/user_lookup.rb, line 35
def load(filename = nil)
  delim = /\s*;\s*/ # The delimiter for email and role lists
  enum = Aspire::Enumerator::ReportEnumerator.new(filename).enumerator
  enum.each do |row|
    # Construct a JSON data structure for the user
    uri = row[3]
    data = csv_to_json_api(row, email_delim: delim, role_delim: delim)
    csv_to_json_other(row, data)
    # Store the JSON data in the lookup table
    store[uri] = data
  end
end
method_missing(method, *args, &block) click to toggle source

Proxies missing methods to the store @param method [Symbol] the method name @param args [Array] the method arguments @param block [Proc] the code block @return [Object] the store method result

Calls superclass method
# File lib/aspire/user_lookup.rb, line 53
def method_missing(method, *args, &block)
  super unless store.respond_to?(method)
  store.public_send(method, *args, &block)
end
respond_to_missing?(method, include_private = false) click to toggle source

Proxies missing method respond_to? to the store @param method [Symbol] the method name @param include_private [Boolean] if true, include private methods,

otherwise include only public methods

@return [Boolean] true if the store supports the method, false otherwise

# File lib/aspire/user_lookup.rb, line 63
def respond_to_missing?(method, include_private = false)
  store.respond_to?(method, include_private)
end

Private Instance Methods

csv_to_json(row) click to toggle source
# File lib/aspire/user_lookup.rb, line 69
def csv_to_json(row)
  # Recreate the Aspire user profile JSON API response from the CSV record
  data = csv_to_json_api(row)
  # Add other report fields which aren't part of the JSON API response
  csv_to_json_other(row, data)
end
csv_to_json_api(row, data = {}, email_delim: nil, role_delim: nil) click to toggle source

Adds CSV fields which mirror the Aspire user profile JSON API fields @param row [Array] the fields from the All User Profiles report CSV @param data [Hash] the JSON representation of the user profile @return [Hash] the JSON data hash

# File lib/aspire/user_lookup.rb, line 80
def csv_to_json_api(row, data = {}, email_delim: nil, role_delim: nil)
  data['email'] = (row[4] || '').split(email_delim)
  data['firstName'] = row[0]
  data['role'] = (row[7] || '').split(role_delim)
  data['surname'] = row[1]
  data['uri'] = row[3]
  data
end
csv_to_json_other(row, data = {}) click to toggle source

Adds CSV fields which aren’t part of the Aspire user profile JSON API @param row [Array] the fields from the All User Profiles report CSV @param data [Hash] the JSON representation of the user profile @return [Hash] the JSON data hash

# File lib/aspire/user_lookup.rb, line 93
def csv_to_json_other(row, data = {})
  # The following fields are not present in the JSON API response but are in
  # the All User Profiles report - they are included for completeness.
  data['jobRole'] = row[5] || ''
  data['lastLogin'] = row[8]
  data['name'] = row[2] || ''
  data['visibility'] = row[6] || ''
  data
end