Class: OpenStax::Utilities::Assets::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/utilities/assets/manifest.rb

Overview

Reads and parses the assets manifest

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManifest

Returns a new instance of Manifest.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/openstax/utilities/assets/manifest.rb', line 10

def initialize
  @assets = HashWithIndifferentAccess.new

  url = OpenStax::Utilities::Assets.url_for('assets.json')

  begin
    response = self.class.client.get url

    if response.success?
      contents = JSON.parse response.body

      @version = contents['version']

      if contents['entrypoints'].blank?
        Rails.logger.error { "failed to parse manifest from #{url}" }
      else
        contents['entrypoints'].each do |entry_key, chunks|
          @assets[entry_key] = chunks['js'].map do |chunk|
            OpenStax::Utilities::Assets.url_for(chunk)
          end
        end
      end
    else
      Rails.logger.error { "status #{response.status} when reading remote url: #{url}" }
    end
  rescue Faraday::ConnectionFailed, Addressable::URI::InvalidURIError, Errno::ECONNREFUSED
  end

  Rails.logger.info do
    "running in development mode with assets served by webpack at #{
      OpenStax::Utilities::Assets.url
    }"
  end if @assets.blank?
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



8
9
10
# File 'lib/openstax/utilities/assets/manifest.rb', line 8

def version
  @version
end

Instance Method Details

#[](asset) ⇒ Object



45
46
47
48
49
# File 'lib/openstax/utilities/assets/manifest.rb', line 45

def [](asset)
  return [ OpenStax::Utilities::Assets.url_for("#{asset}.js") ] if @assets.blank?

  @assets[asset] || []
end