class InlineSvg::WebpackAssetFinder

Public Class Methods

find_asset(filename) click to toggle source
# File lib/inline_svg/webpack_asset_finder.rb, line 3
def self.find_asset(filename)
  new(filename)
end
new(filename) click to toggle source
# File lib/inline_svg/webpack_asset_finder.rb, line 7
def initialize(filename)
  @filename = filename
  manifest_lookup = asset_helper.manifest.lookup(@filename)
  @asset_path =  manifest_lookup.present? ? URI(manifest_lookup).path : ""
end

Public Instance Methods

pathname() click to toggle source
# File lib/inline_svg/webpack_asset_finder.rb, line 13
def pathname
  return if @asset_path.blank?

  if asset_helper.dev_server.running?
    dev_server_asset(@asset_path)
  elsif asset_helper.config.public_path.present?
    File.join(asset_helper.config.public_path, @asset_path)
  end
end

Private Instance Methods

asset_helper() click to toggle source
# File lib/inline_svg/webpack_asset_finder.rb, line 25
def asset_helper
  @asset_helper ||=
    if defined?(::Shakapacker)
      ::Shakapacker
    else
      ::Webpacker
    end
end
dev_server_asset(file_path) click to toggle source
# File lib/inline_svg/webpack_asset_finder.rb, line 34
def dev_server_asset(file_path)
  asset = fetch_from_dev_server(file_path)

  begin
    Tempfile.new(file_path).tap do |file|
      file.binmode
      file.write(asset)
      file.rewind
    end
  rescue StandardError => e
    Rails.logger.error "[inline_svg] Error creating tempfile for #{@filename}: #{e}"
    raise
  end
end
fetch_from_dev_server(file_path) click to toggle source
# File lib/inline_svg/webpack_asset_finder.rb, line 49
def fetch_from_dev_server(file_path)
  http = Net::HTTP.new(asset_helper.dev_server.host, asset_helper.dev_server.port)
  http.use_ssl = asset_helper.dev_server.protocol == "https"
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  http.request(Net::HTTP::Get.new(file_path)).body
rescue StandardError => e
  Rails.logger.error "[inline_svg] Error fetching #{@filename} from webpack-dev-server: #{e}"
  raise
end