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



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
44
45
# File 'lib/openstax/utilities/assets/manifest.rb', line 10

def initialize
  @assets = HashWithIndifferentAccess.new

  url = OpenStax::Utilities::Assets.url_for(
    OpenStax::Utilities.configuration.assets_manifest_filename
  )

  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



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

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

  @assets[asset] || []
end