Class: BerkeleyLibrary::TIND::API::Collection

Inherits:
Object
  • Object
show all
Extended by:
Logging
Defined in:
lib/berkeley_library/tind/api/collection.rb

Constant Summary collapse

ENDPOINT =
'collections'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, nb_rec, children, translations) ⇒ Collection

Returns a new instance of Collection.



12
13
14
15
16
17
# File 'lib/berkeley_library/tind/api/collection.rb', line 12

def initialize(name, nb_rec, children, translations)
  @name = name
  @nb_rec = nb_rec
  @children = children
  @translations = translations
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



9
10
11
# File 'lib/berkeley_library/tind/api/collection.rb', line 9

def children
  @children
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/berkeley_library/tind/api/collection.rb', line 9

def name
  @name
end

#nb_recObject (readonly) Also known as: size

Returns the value of attribute nb_rec.



9
10
11
# File 'lib/berkeley_library/tind/api/collection.rb', line 9

def nb_rec
  @nb_rec
end

#translationsObject (readonly)

Returns the value of attribute translations.



9
10
11
# File 'lib/berkeley_library/tind/api/collection.rb', line 9

def translations
  @translations
end

Class Method Details

.allObject



36
37
38
39
40
41
42
# File 'lib/berkeley_library/tind/api/collection.rb', line 36

def all
  json = API.get(ENDPOINT, depth: 100)
  all_from_json(json)
rescue API::APIException => e
  logger.error(e)
  []
end

.all_from_json(json) ⇒ Array<Collection>

Returns an array of collection tree roots, which can be traversed with #each_descendant.

Returns:

  • (Array<Collection>)

    an array of top-level collections



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/berkeley_library/tind/api/collection.rb', line 54

def all_from_json(json)
  ensure_hash(json).map do |name, attrs|
    translations = attrs['translations']
    Collection.new(
      name,
      attrs['nb_rec'],
      all_from_json(attrs['children']),
      translations
    )
  end
end

.each_collection(&block) ⇒ Object



44
45
46
47
48
# File 'lib/berkeley_library/tind/api/collection.rb', line 44

def each_collection(&block)
  return to_enum(:each_collection) unless block_given?

  all.each { |c| c.each_descendant(include_self: true, &block) }
end

Instance Method Details

#each_descendant(include_self: false) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



25
26
27
28
29
# File 'lib/berkeley_library/tind/api/collection.rb', line 25

def each_descendant(include_self: false, &block)
  yield self if include_self

  children.each { |c| c.each_descendant(include_self: include_self, &block) }
end

#name_enObject



19
20
21
22
23
# File 'lib/berkeley_library/tind/api/collection.rb', line 19

def name_en
  return unless (names = translations['name'])

  names['en']
end