Sha256: 970e388575b1bb1628bdbd445717557510e954773fcd70839df7fefe3fe7561d

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true
require "net/http"
require "pathname"

module ThemeCheck
  class RemoteAssetFile
    class << self
      def cache
        @cache ||= {}
      end

      def from_src(src)
        key = uri(src).to_s
        cache[key] = RemoteAssetFile.new(src) unless cache.key?(key)
        cache[key]
      end

      def uri(src)
        URI.parse(src.sub(%r{^//}, "https://"))
      rescue URI::InvalidURIError
        nil
      end
    end

    def initialize(src)
      @uri = RemoteAssetFile.uri(src)
      @content = nil
    end

    def content
      return if @uri.nil?
      return @content unless @content.nil?

      @content = request(@uri)

    rescue OpenSSL::SSL::SSLError, Zlib::StreamError, *NET_HTTP_EXCEPTIONS
      @contents = ''
    end

    def gzipped_size
      return if @uri.nil?
      @gzipped_size ||= content.bytesize
    end

    private

    def request(uri)
      res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
        req = Net::HTTP::Get.new(uri)
        req['Accept-Encoding'] = 'gzip, deflate, br'
        http.request(req)
      end

      res.body
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
theme-check-1.15.0 lib/theme_check/remote_asset_file.rb
theme-check-1.14.0 lib/theme_check/remote_asset_file.rb
theme-check-1.13.0 lib/theme_check/remote_asset_file.rb
theme-check-1.12.1 lib/theme_check/remote_asset_file.rb
theme-check-1.12.0 lib/theme_check/remote_asset_file.rb