Websites/app/logical/requests/bunny.rb

36 lines
1.0 KiB
Ruby
Raw Normal View History

2024-05-03 03:04:43 +00:00
# frozen_string_literal: true
module Requests
class Bunny
include HTTParty
base_uri "https://storage.bunnycdn.com"
attr_reader :access_key, :storage_zone_name
def initialize(access_key:, storage_zone_name:)
@access_key = access_key
@storage_zone_name = storage_zone_name
end
def delete(path)
self.class.delete("/#{storage_zone_name}/#{path}", headers: { "AccessKey" => access_key })
end
def exists?(path)
# bunny doesn't support HEAD, for some reason - so we have to download the ENTIRE file to check if it exists
# I'm unsure if this counts against us, so it should be used sparingly
!get(path).nil?
end
def get(path)
r = self.class.get("/#{storage_zone_name}/#{path}", headers: { "AccessKey" => access_key })
r.success? ? r.body : nil
end
def put(path, body)
self.class.put("/#{storage_zone_name}/#{path}", body: body, headers: { "AccessKey" => access_key, "Checksum" => Digest::SHA2.hexdigest(body) })
end
end
end