Class: OpenStax::Content::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/content/book.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(archive_version:, uuid: nil, version: nil, hash: nil, title: nil, tree: nil, root_book_part: nil) ⇒ Book

Returns a new instance of Book.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/openstax/content/book.rb', line 5

def initialize(
  archive_version:, uuid: nil, version: nil, hash: nil, title: nil, tree: nil, root_book_part: nil
)
  @uuid            = uuid || (hash || {})['id']
  raise ArgumentError, 'Either uuid or hash with id key is required' if @uuid.nil?

  @version         = version || (hash || {})['version']
  raise ArgumentError, 'Either version or hash with version key is required' if @version.nil?

  @archive_version = archive_version
  @hash            = hash
  @title           = title
  @tree            = tree
  @root_book_part  = root_book_part
end

Instance Attribute Details

#archive_versionObject (readonly)

Returns the value of attribute archive_version.



21
22
23
# File 'lib/openstax/content/book.rb', line 21

def archive_version
  @archive_version
end

#uuidObject (readonly)

Returns the value of attribute uuid.



21
22
23
# File 'lib/openstax/content/book.rb', line 21

def uuid
  @uuid
end

#versionObject (readonly)

Returns the value of attribute version.



21
22
23
# File 'lib/openstax/content/book.rb', line 21

def version
  @version
end

Instance Method Details

#archiveObject



23
24
25
# File 'lib/openstax/content/book.rb', line 23

def archive
  @archive ||= OpenStax::Content::Archive.new archive_version
end

#bakedObject



35
36
37
# File 'lib/openstax/content/book.rb', line 35

def baked
  @baked ||= hash['baked']
end

#collatedObject



39
40
41
# File 'lib/openstax/content/book.rb', line 39

def collated
  @collated ||= hash.fetch('collated', false)
end

#hashObject



43
44
45
# File 'lib/openstax/content/book.rb', line 43

def hash
  @hash ||= archive.json url
end

#root_book_partObject



67
68
69
# File 'lib/openstax/content/book.rb', line 67

def root_book_part
  @root_book_part ||= OpenStax::Content::BookPart.new(hash: tree, is_root: true, book: self)
end

#short_idObject



51
52
53
# File 'lib/openstax/content/book.rb', line 51

def short_id
  @short_id ||= hash['shortId']
end

#titleObject



59
60
61
# File 'lib/openstax/content/book.rb', line 59

def title
  @title ||= hash.fetch('title')
end

#treeObject



63
64
65
# File 'lib/openstax/content/book.rb', line 63

def tree
  @tree ||= hash.fetch('tree')
end

#urlObject



27
28
29
# File 'lib/openstax/content/book.rb', line 27

def url
  @url ||= archive.url_for "#{uuid}@#{version}"
end

#url_fragmentObject



31
32
33
# File 'lib/openstax/content/book.rb', line 31

def url_fragment
  @url_fragment ||= url.chomp('.json')
end