Class: BerkeleyLibrary::TIND::API::Collection
- Inherits:
-
Object
- Object
- BerkeleyLibrary::TIND::API::Collection
- Extended by:
- Logging
- Defined in:
- lib/berkeley_library/tind/api/collection.rb
Constant Summary collapse
- ENDPOINT =
'collections'.freeze
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#nb_rec ⇒ Object
(also: #size)
readonly
Returns the value of attribute nb_rec.
-
#translations ⇒ Object
readonly
Returns the value of attribute translations.
Class Method Summary collapse
- .all ⇒ Object
-
.all_from_json(json) ⇒ Array<Collection>
Returns an array of collection tree roots, which can be traversed with #each_descendant.
- .each_collection(&block) ⇒ Object
Instance Method Summary collapse
- #each_descendant(include_self: false) {|_self| ... } ⇒ Object
-
#initialize(name, nb_rec, children, translations) ⇒ Collection
constructor
A new instance of Collection.
- #name_en ⇒ Object
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
#children ⇒ Object (readonly)
Returns the value of attribute children.
9 10 11 |
# File 'lib/berkeley_library/tind/api/collection.rb', line 9 def children @children end |
#name ⇒ Object (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_rec ⇒ Object (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 |
#translations ⇒ Object (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
.all ⇒ Object
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.
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
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_en ⇒ Object
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 |