class RailsAssetslessContainer::Strategy::AssetServer

Attributes

retry_count[R]
retry_wait[R]
suffix[R]

Public Class Methods

new( asset_host: nil, suffix: ENV["SHA"], retry_count: 3, retry_wait: 1 ) click to toggle source
# File lib/rails_assetsless_container/strategy/asset_server.rb, line 6
def initialize(
  asset_host: nil,
  suffix: ENV["SHA"],
  retry_count: 3,
  retry_wait: 1
)
  @asset_host = asset_host
  @suffix = suffix
  @retry_count = retry_count
  @retry_wait = retry_wait
end

Public Instance Methods

after_sprockets(path) click to toggle source
# File lib/rails_assetsless_container/strategy/asset_server.rb, line 18
def after_sprockets(path)
  FileUtils.copy(path, public_dir.join(sprockets_manifest_path))
end
after_webpacker(path) click to toggle source
# File lib/rails_assetsless_container/strategy/asset_server.rb, line 22
def after_webpacker(path)
  FileUtils.copy(path, public_dir.join(webpacker_manifest_path))
end
write_sprockets_manifest(path) click to toggle source
# File lib/rails_assetsless_container/strategy/asset_server.rb, line 26
def write_sprockets_manifest(path)
  uri = build_manifest_uri(sprockets_manifest_path)
  mkdir(path)
  File.write(path, download_with_retry(uri))
end
write_webpacker_manifest(path) click to toggle source
# File lib/rails_assetsless_container/strategy/asset_server.rb, line 32
def write_webpacker_manifest(path)
  uri = build_manifest_uri(webpacker_manifest_path)
  mkdir(path)
  File.write(path, download_with_retry(uri))
end

Private Instance Methods

asset_host() click to toggle source
# File lib/rails_assetsless_container/strategy/asset_server.rb, line 46
def asset_host
  @asset_host ||= Rails.application.config.asset_host
end
build_manifest_uri(path) click to toggle source
# File lib/rails_assetsless_container/strategy/asset_server.rb, line 62
def build_manifest_uri(path)
  scheme = "https://" unless asset_host.start_with?("http")
  URI.parse("#{scheme}#{asset_host}/#{path}")
end
download_with_retry(uri) click to toggle source
# File lib/rails_assetsless_container/strategy/asset_server.rb, line 67
def download_with_retry(uri)
  with_retry do
    res = Net::HTTP.get_response(uri)
    raise("#{res.code}: #{res.body}") if res.code != "200"

    res.body
  end
end
mkdir(path) click to toggle source
# File lib/rails_assetsless_container/strategy/asset_server.rb, line 58
def mkdir(path)
  Pathname(path).dirname.mkpath
end
public_dir() click to toggle source
# File lib/rails_assetsless_container/strategy/asset_server.rb, line 42
def public_dir
  Rails.root.join("public")
end
sprockets_manifest_path() click to toggle source
# File lib/rails_assetsless_container/strategy/asset_server.rb, line 50
def sprockets_manifest_path
  "assets/.sprockets-manifest-#{suffix}.json"
end
webpacker_manifest_path() click to toggle source
# File lib/rails_assetsless_container/strategy/asset_server.rb, line 54
def webpacker_manifest_path
  "packs/manifest-#{suffix}.json"
end
with_retry() { || ... } click to toggle source
# File lib/rails_assetsless_container/strategy/asset_server.rb, line 76
def with_retry
  count = 0

  begin
    yield
  rescue StandardError => e
    count += 1
    raise(e) if count >= retry_count

    sleep(retry_wait) if retry_wait.positive?
    retry
  end
end