class BerkeleyLibrary::TIND::API::Collection

Constants

ENDPOINT

Attributes

children[R]
name[R]
nb_rec[R]
size[R]
translations[R]

Public Class Methods

all() click to toggle source
# 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) click to toggle source

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

@return [Array<Collection>] an array of top-level collections

# 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) click to toggle source
# 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
new(name, nb_rec, children, translations) click to toggle source
# 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

Private Class Methods

ensure_hash(json) click to toggle source
# File lib/berkeley_library/tind/api/collection.rb, line 68
def ensure_hash(json)
  return {} unless json
  return json if hash_like?(json)

  JSON.parse(json)
end
hash_like?(h) click to toggle source
# File lib/berkeley_library/tind/api/collection.rb, line 75
def hash_like?(h)
  h.respond_to?(:each_key) && h.respond_to?(:each_value)
end

Public Instance Methods

each_descendant(include_self: false) { |self| ... } click to toggle source
# 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_en() click to toggle source
# File lib/berkeley_library/tind/api/collection.rb, line 19
def name_en
  return unless (names = translations['name'])

  names['en']
end