Class: Nft::Client
- Inherits:
-
Object
- Object
- Nft::Client
- Defined in:
- lib/nft/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#gateway_endpoint ⇒ Object
readonly
Returns the value of attribute gateway_endpoint.
-
#http_client ⇒ Object
readonly
Returns the value of attribute http_client.
Instance Method Summary collapse
-
#add(path) ⇒ Object
Uploads a file to NFT.storage and returns the IPFS CID.
- #build_file_url(hash, filename = '') ⇒ Object
- #delete(key) ⇒ Object
- #download(hash, &block) ⇒ Object
- #file_exists?(cid) ⇒ Boolean
-
#initialize(api_key, gateway_endpoint) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(api_key, gateway_endpoint) ⇒ Client
Returns a new instance of Client.
13 14 15 16 17 |
# File 'lib/nft/client.rb', line 13 def initialize(api_key, gateway_endpoint) @api_key = api_key @gateway_endpoint = gateway_endpoint @http_client = HTTP end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
11 12 13 |
# File 'lib/nft/client.rb', line 11 def api_key @api_key end |
#gateway_endpoint ⇒ Object (readonly)
Returns the value of attribute gateway_endpoint.
11 12 13 |
# File 'lib/nft/client.rb', line 11 def gateway_endpoint @gateway_endpoint end |
#http_client ⇒ Object (readonly)
Returns the value of attribute http_client.
11 12 13 |
# File 'lib/nft/client.rb', line 11 def http_client @http_client end |
Instance Method Details
#add(path) ⇒ Object
Uploads a file to NFT.storage and returns the IPFS CID
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/nft/client.rb', line 20 def add(path) res = @http_client.auth("Bearer #{@api_key}").post( "https://api.nft.storage/upload", body: File.open(path) ) if res.code >= 200 && res.code <= 299 JSON.parse(res.body)['value']['cid'] else raise Error, res.body end end |
#build_file_url(hash, filename = '') ⇒ Object
58 59 60 61 |
# File 'lib/nft/client.rb', line 58 def build_file_url(hash, filename = '') query = filename.empty? ? '' : "?filename=#{filename}" "#{@gateway_endpoint}/ipfs/#{hash}#{query}" end |
#delete(key) ⇒ Object
43 44 45 |
# File 'lib/nft/client.rb', line 43 def delete(key) # haha, no. end |
#download(hash, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/nft/client.rb', line 32 def download(hash, &block) url = build_file_url(hash) res = @http_client.get(url) if block_given? res.body.each(&block) else res.body end end |
#file_exists?(cid) ⇒ Boolean
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/nft/client.rb', line 47 def file_exists?(cid) res = @http_client.auth("Bearer #{@api_key}").get( "https://api.nft.storage/check/#{cid}" ) if res.code >= 200 && res.code <= 299 true else false end end |