Sha256: 2cef5550f687e358504265e65996f0c12744cb1bcad16f4ce1918dd35f468945

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module Motor
  module Assets
    InvalidPathError = Class.new(StandardError)

    ASSETS_PATH = Pathname.new(__dir__).join('../../ui/dist')
    MANIFEST_PATH = ASSETS_PATH.join('manifest.json')
    DEV_SERVER_URL = 'http://localhost:9090/'

    module_function

    def manifest
      JSON.parse(MANIFEST_PATH.read)
    end

    def icons
      manifest.select do |k, v|
        !k.ends_with?('.gz') && v.starts_with?('icons/') && v.exclude?('DS_Store')
      end.keys
    end

    def asset_path(path)
      Motor::Admin.routes.url_helpers.motor_asset_path(manifest[path])
    end

    def load_asset(filename, gzip: false)
      if Motor.development?
        load_from_dev_server(filename)
      else
        load_from_disk(filename, gzip: gzip)
      end
    end

    def load_from_disk(filename, gzip:)
      filename += '.gz' if gzip

      path = ASSETS_PATH.join(filename)

      raise InvalidPathError unless path.to_s.starts_with?(ASSETS_PATH.to_s)

      path.read
    end

    def load_from_dev_server(filename)
      uri = URI(DEV_SERVER_URL + filename)

      Net::HTTP.get_response(uri).body
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
motor-admin-0.1.52 lib/motor/assets.rb
motor-admin-0.1.51 lib/motor/assets.rb
motor-admin-0.1.50 lib/motor/assets.rb
motor-admin-0.1.49 lib/motor/assets.rb
motor-admin-0.1.48 lib/motor/assets.rb