class Aspire::Object::Factory

A factory returning reading list objects given the object’s URI

Attributes

cache[RW]

@!attribute [rw] cache

@return [Aspire::Caching::Cache] the cache for retrieving data
users[RW]

@!attribute [rw] users

@return [Hash<String, Aspire::Object::User>] a hash of user profiles
  indexed by URI

Public Class Methods

new(cache, users = nil) click to toggle source

Initialises a new Factory instance @param cache [Aspire::Caching::Cache] the cache for retrieving data @param users [Hash<String, Aspire::Object::User>] a hash of user

profiles indexed by URI

@return [void]

# File lib/aspire/object/factory.rb, line 26
def initialize(cache, users = nil)
  self.cache = cache
  self.users = users || {}
end

Public Instance Methods

get(uri = nil, parent = nil, json: nil, ld: nil) click to toggle source

Returns a new API list object (Aspire::Object::ListBase subclass) given its URI @param uri [String] the URI of the object @param parent [Aspire::Object::ListBase] this object’s parent object @param json [Hash] the parsed JSON API data for the object @param ld [Hash] the parsed linked data API data for the object @return [Aspire::Object::ListBase] the list object

# File lib/aspire/object/factory.rb, line 38
def get(uri = nil, parent = nil, json: nil, ld: nil)
  return nil if uri.nil? || uri.empty?
  get_exceptions(uri, parent, json: json, ld: ld) ||
    get_linked_data(uri, parent, json: json, ld: ld)
end

Private Instance Methods

get_exceptions(uri = nil, parent = nil, json: nil, ld: nil) click to toggle source

Returns a new object from sources other than the linked data API @param uri [String] the URI of the object @param parent [Aspire::Object::ListBase] this object’s parent object @param json [Hash] the parsed JSON API data for the object @param ld [Hash] the parsed linked data API data for the object @return [Aspire::Object::ListBase, nil] the list object or nil if not

available
# File lib/aspire/object/factory.rb, line 53
def get_exceptions(uri = nil, parent = nil, json: nil, ld: nil)
  # Get item data from the parent list's JSON API data
  return ListItem.new(uri, self, parent) if item?(uri)
  # Get resource data from the JSON API
  if resource?(uri) && !json.nil?
    return Resource.new(uri, self, json: json, ld: ld)
  end
  # Get user data from the users lookup table
  # - normalise the URI to the form used by the linked data API
  return users[cache.linked_data_url(uri), self] if user?(uri)
  # Otherwise no exceptions
  nil
end
get_linked_data(uri, parent = nil, json: nil, ld: nil) click to toggle source

Returns a new object from the linked data API @param uri [String] the URI of the object @param parent [Aspire::Object::ListBase] this object’s parent object @param json [Hash] the parsed JSON API data for the object @param ld [Hash] the parsed linked data API data for the object @return [Aspire::Object::ListBase, nil] the list object or nil if not

available
# File lib/aspire/object/factory.rb, line 74
def get_linked_data(uri, parent = nil, json: nil, ld: nil)
  # Call #linked_data to determine whether uri is present in ld
  ld = linked_data(uri, ld) || cache.read(uri)
  return List.new(uri, self, parent, json: json, ld: ld) if list?(uri)
  return Module.new(uri, self, json: json, ld: ld) if module?(uri)
  return Resource.new(uri, self, json: json, ld: ld) if resource?(uri)
  if section?(uri)
    return ListSection.new(uri, self, parent, json: json, ld: ld)
  end
  nil
end