Class: Prebake::Backends::Http

Inherits:
Base
  • Object
show all
Includes:
HttpClient
Defined in:
lib/prebake/backends/http.rb

Constant Summary

Constants included from HttpClient

Prebake::Backends::HttpClient::HTTP_METHODS, Prebake::Backends::HttpClient::TIMEOUT

Instance Method Summary collapse

Methods inherited from Base

#checksums_supported?

Constructor Details

#initialize(url:, token: nil) ⇒ Http

Returns a new instance of Http.



14
15
16
17
18
19
# File 'lib/prebake/backends/http.rb', line 14

def initialize(url:, token: nil)
  @url = url.chomp("/")
  @token = token

  warn_if_insecure_http(@url)
end

Instance Method Details

#delete(cache_key) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/prebake/backends/http.rb', line 73

def delete(cache_key)
  gem_response = http_request(:delete, gem_uri(cache_key))
  http_request(:delete, checksum_uri(cache_key))

  gem_response.is_a?(Net::HTTPSuccess)
rescue StandardError => e
  Logger.debug "Delete failed for #{cache_key}: #{e.message}"
  false
end

#exists?(cache_key) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
# File 'lib/prebake/backends/http.rb', line 66

def exists?(cache_key)
  response = http_request(:head, gem_uri(cache_key))
  response.is_a?(Net::HTTPSuccess)
rescue StandardError
  false
end

#fetch(cache_key) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/prebake/backends/http.rb', line 21

def fetch(cache_key)
  response = http_request(:get, gem_uri(cache_key))
  return nil unless response.is_a?(Net::HTTPSuccess)

  path = File.join(Dir.tmpdir, "prebake-#{SecureRandom.hex(16)}.gem")
  File.binwrite(path, response.body)
  path
rescue StandardError => e
  Logger.debug "Fetch failed for #{cache_key}: #{e.message}"
  nil
end

#fetch_checksum(cache_key) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/prebake/backends/http.rb', line 33

def fetch_checksum(cache_key)
  response = http_request(:get, checksum_uri(cache_key))
  return nil unless response.is_a?(Net::HTTPSuccess)

  response.body.strip
rescue StandardError => e
  Logger.debug "Checksum fetch failed for #{cache_key}: #{e.message}"
  nil
end

#push(gem_path, cache_key, checksum) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/prebake/backends/http.rb', line 43

def push(gem_path, cache_key, checksum)
  gem_response = http_request(:put, gem_uri(cache_key), body: File.binread(gem_path))

  unless gem_response.is_a?(Net::HTTPSuccess)
    Logger.warn "Failed to push #{cache_key}: #{gem_response.code}"
    return false
  end

  checksum_response = http_request(:put, checksum_uri(cache_key), body: checksum)

  unless checksum_response.is_a?(Net::HTTPSuccess)
    Logger.warn "Checksum push failed for #{cache_key}: #{checksum_response.code}, removing gem"
    http_request(:delete, gem_uri(cache_key))
    return false
  end

  Logger.info "Pushed #{cache_key}"
  true
rescue StandardError => e
  Logger.warn "Push failed for #{cache_key}: #{e.message}"
  false
end