class OpenStax::Utilities::Assets::Manifest

Reads and parses the assets manifest

Attributes

version[R]

Public Class Methods

new() click to toggle source
# 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

Protected Class Methods

client() click to toggle source

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

# 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

Public Instance Methods

[](asset) click to toggle source
# 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