Class: BooticCli::Themes::APIAsset

Inherits:
ItemWithTime
  • Object
show all
Defined in:
lib/bootic_cli/themes/api_theme.rb

Constant Summary collapse

REQUEST_OPTS =
{
  open_timeout: 5,
  read_timeout: 5
}

Instance Method Summary collapse

Methods inherited from ItemWithTime

#updated_on

Instance Method Details

#==(other) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/bootic_cli/themes/api_theme.rb', line 27

def ==(other)
  if digest.to_s == '' || other.digest.to_s == ''
    # puts "One or the other digest is empty: #{digest} -- #{other.digest}"
    return super
  end

  # file sizes may differ as they are served by CDN (that shrinks them)
  # puts "Comparing APIAsset vs other digest:\n#{digest}\n#{other.digest}"
  self.digest == other.digest # && self.file_size == other.file_size
end

#fetch_data(attempt = 1, skip_verify = false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bootic_cli/themes/api_theme.rb', line 38

def fetch_data(attempt = 1, skip_verify = false)
  uri = URI.parse(rels[:file].href)
  opts = REQUEST_OPTS.merge({
    verify_mode: skip_verify ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER,
    use_ssl: uri.port == 443
  })

  Net::HTTP.start(uri.host, uri.port, opts) do |http|
    resp = http.get(uri.path)
    raise "Invalid response: #{resp.code}" unless resp.code.to_i == 200
    resp.body
  end
rescue Net::OpenTimeout, Net::ReadTimeout => e
  raise if attempt > 3 # max attempts
  # puts "#{e.class} for #{File.basename(uri.path)}! Retrying request..."
  fetch_data(attempt + 1)
rescue OpenSSL::SSL::SSLError => e
  # retry but skipping verification
  fetch_data(attempt + 1, true)
end

#fileObject



23
24
25
# File 'lib/bootic_cli/themes/api_theme.rb', line 23

def file
  @file ||= StringIO.new(fetch_data)
end