Class: Aspire::Object::Base

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/aspire/object/base.rb

Overview

The base class for Aspire API objects

Direct Known Subclasses

Digitisation, ListBase, Module, Resource, TimePeriod, User

Constant Summary collapse

STRIP_HTML =

Aspire properties containing HTML markup will have the markup stripped

if STRIP_HTML = true"#{without format suffix (.html, .json etc.)}"
true

Constants included from Util

Util::LD_API_URI

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#child_url?, #duration, #id_from_uri, #item?, #linked_data, #linked_data_path, #list?, #list_url?, #module?, #parent_url?, #parse_url, #resource?, #section?, #url_for_comparison, #url_path, #user?

Constructor Details

#initialize(uri, factory) ⇒ void

Initialises a new Aspire::Object instance

Parameters:

  • uri (String)

    the URI of the object

  • factory (Aspire::Object::Factory)

    the factory for creating Aspire::Object instances



31
32
33
34
35
# File 'lib/aspire/object/base.rb', line 31

def initialize(uri, factory)
  self.factory = factory
  # Normalise the URL to the linked data form
  self.uri = factory ? factory.cache.linked_data_url(uri) : uri
end

Instance Attribute Details

#factoryAspire::Object::Factory

Returns the factory for creating Aspire::Object instances.

Returns:



20
21
22
# File 'lib/aspire/object/base.rb', line 20

def factory
  @factory
end

#uriString

Returns the URI of the object.

Returns:

  • (String)

    the URI of the object



24
25
26
# File 'lib/aspire/object/base.rb', line 24

def uri
  @uri
end

Instance Method Details

#get_boolean(property, data, single: true) ⇒ Boolean+

Returns a Boolean property value

Parameters:

  • property (String)

    the property name

  • data (Hash)

    the data hash containing the property (defaults to self.ld)

  • single (Boolean) (defaults to: true)

    if true, return a single value, otherwise return an array of values

Returns:

  • (Boolean, Array<Boolean>)

    the property value(s)



44
45
46
47
48
# File 'lib/aspire/object/base.rb', line 44

def get_boolean(property, data, single: true)
  get_property(property, data, single: single) do |value, _type|
    value ? true : false
  end
end

#get_date(property, data, single: true) ⇒ DateTime+

Returns a DateTime instance for a timestamp property

Parameters:

  • property (String)

    the property name

  • data (Hash)

    the data hash containing the property (defaults to self.ld)

  • single (Boolean) (defaults to: true)

    if true, return a single value, otherwise return an array of values

Returns:

  • (DateTime, Array<DateTime>)

    the property value(s)



57
58
59
60
61
# File 'lib/aspire/object/base.rb', line 57

def get_date(property, data, single: true)
  get_property(property, data, single: single) do |value, _type|
    DateTime.parse(value)
  end
end

#get_property(property, data, is_url: false, single: true) {|value, type| ... } ⇒ Object+

Returns the value of a property

Parameters:

  • property (String)

    the property name

  • data (Hash)

    the data hash containing the property (defaults to self.data)

  • is_url (Boolean) (defaults to: false)

    if true, the property value is a URL

  • single (Boolean) (defaults to: true)

    if true, return a single value, otherwise return an array of values

Yields:

  • (value, type)

    passes the value and type to the block

Yield Parameters:

  • value (Object)

    the property value

  • type (String)

    the type of the property value

Yield Returns:

  • (Object)

    the transformed property value

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/aspire/object/base.rb', line 75

def get_property(property, data, is_url: false, single: true, &block)
  values = data ? data[property] : nil
  if values.is_a?(Array)
    values = values.map do |value|
      get_property_value(value, is_url: is_url, &block)
    end
    single ? values[0] : values
  else
    value = get_property_value(values, is_url: is_url, &block)
    single ? value : [value]
  end
end

#to_sString

Returns a string representation of the APIObject instance (the URI)

Returns:

  • (String)

    the string representation of the APIObject instance



90
91
92
# File 'lib/aspire/object/base.rb', line 90

def to_s
  uri.to_s
end