Websites/app/logical/requests/bunny.rb

38 lines
1.1 KiB
Ruby

# 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, &block)
r = self.class.get("/#{storage_zone_name}/#{path}", headers: { "AccessKey" => access_key })
file = r.success? ? r.body : nil
block&.call(file)
file
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