Class: Ipfs::File

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-ipfs-http-client/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Examples:

given a filepath

Ipfs::File.new(path: 'path/to/file')
#=> #<Ipfs::File @path="path/to/file", @added=false>

given a multihash

Ipfs::File.new(multihash: 'QmVfpW2rKzzahcxt5LfYyNnnKvo1L7XyRF8Ykmhttcyztv')
#=> #<Ipfs::File @added=false, @multihash=#<Ipfs::Multihash ....>>

Parameters:

  • attributes (Hash{Symbol => String})

Raises:

  • (Error::InvalidMultihash, Errno::ENOENT)

    Whether the path leads to a non-file entity or the multihash may be invalid, an error is thrown.



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

#multihashIpfs::Multihash (readonly)

The file’s multihash as returned by Ipfs.

Returns:



10
11
12
# File 'lib/ruby-ipfs-http-client/file.rb', line 10

def multihash
  @multihash
end

#nameString (readonly)

The file’s name as returned by Ipfs.

Returns:

  • (String)

    the current value of name



10
11
12
# File 'lib/ruby-ipfs-http-client/file.rb', line 10

def name
  @name
end

#pathString (readonly)

The file’s path.

Returns:

  • (String)

    the current value of path



10
11
12
# File 'lib/ruby-ipfs-http-client/file.rb', line 10

def path
  @path
end

#sizeInteger (readonly)

The file’s size in bytes as returned by Ipfs.

Returns:

  • (Integer)

    the current value of size



10
11
12
# File 'lib/ruby-ipfs-http-client/file.rb', line 10

def size
  @size
end

Instance Method Details

#addIpfs::File

Note:

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.

Examples:

file not being added to Ipfs

file = Ipfs::File.new(path: 'path/to/file')
file.cat
#=> ''
file.multihash
#=> nil
file.name
#=> nil
file.size
#=> nil

file being added

file = Ipfs::File.new(path: 'path/to/file').add
file.cat
#=> 'file content'
file.multihash
#=> #<Ipfs::Multihash ...>
file.name
#=> 'file'
file.size
#=> 20

Returns:

  • (Ipfs::File)

    Returns the object on which the method was call.



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

#catString

Note:

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.

Examples:

Ipfs::File.new(path: 'path/to/file').add.cat
#=> 'file content'

Returns:

  • (String)

    The content is returned.



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