Sha256: e1c43e85428564c521b2f3ef97f21508723d965f9a503e6ea06ebcd4336db1c2

Contents?: true

Size: 908 Bytes

Versions: 6

Compression:

Stored size: 908 Bytes

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://"))
      end
    end

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

    def content
      return @content unless @content.nil?

      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

      @content = res.body
    end

    def gzipped_size
      @gzipped_size ||= content.bytesize
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
theme-check-0.8.0 lib/theme_check/remote_asset_file.rb
theme-check-0.7.3 lib/theme_check/remote_asset_file.rb
theme-check-0.7.2 lib/theme_check/remote_asset_file.rb
theme-check-0.7.1 lib/theme_check/remote_asset_file.rb
theme-check-0.7.0 lib/theme_check/remote_asset_file.rb
theme-check-0.6.0 lib/theme_check/remote_asset_file.rb