Class: Service::NftService

Inherits:
Service
  • Object
show all
Defined in:
lib/active_storage/service/nft_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, gateway_endpoint:) ⇒ NftService

Returns a new instance of NftService.



7
8
9
# File 'lib/active_storage/service/nft_service.rb', line 7

def initialize(api_key:, gateway_endpoint:)
  @client = Nft::Client.new api_key, gateway_endpoint
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/active_storage/service/nft_service.rb', line 5

def client
  @client
end

Instance Method Details

#delete(key) ⇒ Object



43
44
45
# File 'lib/active_storage/service/nft_service.rb', line 43

def delete(key)
  # nope
end

#delete_prefixed(key) ⇒ Object



39
40
41
# File 'lib/active_storage/service/nft_service.rb', line 39

def delete_prefixed(key)
  # haha, no
end

#download(key, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_storage/service/nft_service.rb', line 21

def download(key, &block)
  if block_given?
    instrument :streaming_download, key: key do
      @client.download key, &block
    end
  else
    instrument :download, key: key do
      @client.download key
    end
  end
end

#download_chunk(key, range) ⇒ Object



33
34
35
36
37
# File 'lib/active_storage/service/nft_service.rb', line 33

def download_chunk(key, range)
  instrument :download_chunk, key: key, range: range do
    @client.cat key, range.begin, range.size
  end
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/active_storage/service/nft_service.rb', line 53

def exists?(key)
  instrument :exist, key: key do
    @client.file_exists?(key)
  end
end

#upload(key, io, checksum: nil) ⇒ Object

File is uploaded to NFT.storage and a hash is returned which is used to retrieve the file Change the key of the blob to that of the hash



14
15
16
17
18
19
# File 'lib/active_storage/service/nft_service.rb', line 14

def upload(key, io, checksum: nil, **)
  instrument :upload, key: key, checksum: checksum do
    cid_key = @client.add io.path
    blob = Blob.find_by_key(key).update!(key: cid_key)
  end
end

#url(key, content_type: nil, filename: nil, expires_in: nil, disposition: nil) ⇒ Object



47
48
49
50
51
# File 'lib/active_storage/service/nft_service.rb', line 47

def url(key, content_type: nil, filename: nil, expires_in: nil, disposition: nil)
  instrument :url, key: key do
    @client.build_file_url key, filename.to_s
  end
end

#url_for_direct_upload(key, expires_in: nil, content_type: nil, content_length: nil, checksum: nil) ⇒ Object



59
60
61
62
63
# File 'lib/active_storage/service/nft_service.rb', line 59

def url_for_direct_upload(key, expires_in: nil, content_type: nil, content_length: nil, checksum: nil)
  instrument :url_for_direct_upload, key: key do
    "#{@client.api_endpoint}/api/v0/add"
  end
end