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

Class Method 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

Class Method Details

.clientObject

Faraday is probably thread-safe but makes no guarantees github.com/lostisland/faraday/issues/370 We could potentially reuse the client object However, bugs happen: github.com/lostisland/faraday/issues/1068



57
58
59
60
61
62
63
# File 'lib/openstax/utilities/assets/manifest.rb', line 57

def self.client
  Faraday.new do |builder|
    builder.use :http_cache, store: Rails.cache

    builder.adapter Faraday.default_adapter
  end
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