Class: Ipfs::File
- Inherits:
-
Object
- Object
- Ipfs::File
- Defined in:
- lib/ruby-ipfs-http-client/file.rb
Instance Attribute Summary collapse
-
#multihash ⇒ Ipfs::Multihash
readonly
The file’s multihash as returned by Ipfs.
-
#name ⇒ String
readonly
The file’s name as returned by Ipfs.
-
#path ⇒ String
readonly
The file’s path.
-
#size ⇒ Integer
readonly
The file’s size in bytes as returned by Ipfs.
Instance Method Summary collapse
-
#add ⇒ Ipfs::File
Add a file to the Ipfs’ node.
-
#cat ⇒ String
Use the #multihash to get the content of a file from Ipfs and returns it.
-
#initialize(**attributes) ⇒ Ipfs::File
constructor
Create an Ipfs file object, either from a Multihash or from a filepath allowing a file to be added to and be retrieved from Ipfs.
Constructor Details
#initialize(**attributes) ⇒ Ipfs::File
Create an Ipfs file object, either from a Multihash or from a filepath allowing a file to be added to and be retrieved from Ipfs.
30 31 32 33 34 35 36 |
# File 'lib/ruby-ipfs-http-client/file.rb', line 30 def initialize(**attributes) attributes.each { |name, value| instance_variable_set("@#{name}".to_sym, send("init_#{name}", value)) } @added = false end |
Instance Attribute Details
#multihash ⇒ Ipfs::Multihash (readonly)
The file’s multihash as returned by Ipfs.
10 11 12 |
# File 'lib/ruby-ipfs-http-client/file.rb', line 10 def multihash @multihash end |
#name ⇒ String (readonly)
The file’s name as returned by Ipfs.
10 11 12 |
# File 'lib/ruby-ipfs-http-client/file.rb', line 10 def name @name end |
#path ⇒ String (readonly)
The file’s path.
10 11 12 |
# File 'lib/ruby-ipfs-http-client/file.rb', line 10 def path @path end |
#size ⇒ Integer (readonly)
The file’s size in bytes as returned by Ipfs.
10 11 12 |
# File 'lib/ruby-ipfs-http-client/file.rb', line 10 def size @size end |
Instance Method Details
#add ⇒ Ipfs::File
the call to Ipfs completes data about the added file. See #multihash, #size and #name.
Add a file to the Ipfs’ node.
An Ipfs::File instantiated from a multihash will not be added to Ipfs (as the presence of the multihash already suppose its addition to a node). In such case, the object is still returned but no call to Ipfs occurs.
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ruby-ipfs-http-client/file.rb', line 69 def add tap { Ipfs::Client.execute(Command::Add, @path).tap { |response| @added = true @multihash = init_multihash(response['Hash']) @size = response['Size'].to_i @name = response['Name'] } if !@added } end |
#cat ⇒ String
the file must be added first or have a multihash. See #add and #multihash.
Use the #multihash to get the content of a file from Ipfs and returns it.
90 91 92 93 94 95 96 |
# File 'lib/ruby-ipfs-http-client/file.rb', line 90 def cat begin Ipfs::Client.execute(Command::Cat, @multihash).to_s if @multihash rescue Ipfs::Error::InvalidDagStream '' end end |