Class: OpenStax::Content::Abl

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

Instance Method Summary collapse

Constructor Details

#initialize(url: nil) ⇒ Abl

Returns a new instance of Abl.



5
6
7
# File 'lib/openstax/content/abl.rb', line 5

def initialize(url: nil)
  @url = url
end

Instance Method Details

#approved_books(archive: OpenStax::Content::Archive.new) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/openstax/content/abl.rb', line 41

def approved_books(archive: OpenStax::Content::Archive.new)
  # Can be removed once we have no more CNX books
  version_by_collection_id = latest_approved_version_by_collection_id(archive: archive)

  body_hash[:approved_books].flat_map do |approved_book|
    if approved_book[:versions].nil?
      # CNX-hosted book
      version = version_by_collection_id[approved_book[:collection_id]]

      next [] if version.nil?

      approved_book[:books].map do |book|
        OpenStax::Content::Book.new(
          archive: archive,
          uuid: book[:uuid],
          version: version[:content_version].sub('1.', ''),
          slug: book[:slug],
          style: approved_book[:style]
        )
      end
    else
      # Git-hosted book
      approved_book[:versions].flat_map do |version|
        next [] if version[:min_code_version] > archive.version

         = version[:commit_metadata]

        [:books].map do |book|
          OpenStax::Content::Book.new(
            archive: archive,
            uuid: book[:uuid],
            version: version[:commit_sha][0..6],
            slug: book[:slug],
            style: book[:style]
          )
        end
      end
    end
  end
end

#body_hashObject



17
18
19
# File 'lib/openstax/content/abl.rb', line 17

def body_hash
  @body_hash ||= JSON.parse(body_string, symbolize_names: true)
end

#body_stringObject



13
14
15
# File 'lib/openstax/content/abl.rb', line 13

def body_string
  @body_string ||= Faraday.get(url).body
end

#digestObject



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

def digest
  Digest::SHA256.hexdigest body_string
end

#latest_approved_version_by_collection_id(archive: OpenStax::Content::Archive.new) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/openstax/content/abl.rb', line 25

def latest_approved_version_by_collection_id(archive: OpenStax::Content::Archive.new)
  {}.tap do |hash|
    body_hash[:approved_versions].each do |version|
      next if version[:min_code_version] > archive.version

      existing_version = hash[version[:collection_id]]

      next if !existing_version.nil? &&
              (existing_version[:content_version].split('.').map(&:to_i) <=>
               version[:content_version].split('.').map(&:to_i)) >= 0

      hash[version[:collection_id]] = version
    end
  end
end

#urlObject



9
10
11
# File 'lib/openstax/content/abl.rb', line 9

def url
  @url ||= OpenStax::Content.abl_url
end