class IPFS::Connection
Public Class Methods
new(base_url = 'http://127.0.0.1:5001')
click to toggle source
# File lib/ipfs-api/connection.rb, line 9 def initialize base_url = 'http://127.0.0.1:5001' @base_url = base_url end
Public Instance Methods
add(nodes, &block)
click to toggle source
# File lib/ipfs-api/connection.rb, line 13 def add nodes, &block boundary = generate_boundary tree_walker = Upload::TreeWalker.depth_first(nodes) node_map = {} producer = IO::StreamProducer.new do |buf| buf << %Q{\ --#{boundary}\r\n\ Content-Disposition: file; filename="root"\r\n\ Content-Type: application/x-directory\r\n\ \r\n\ \r\n\ } tree_walker.each do |node, depth| node_map[node.path] = node if node.folder? buf << %Q{\ --#{boundary}\r\n\ Content-Disposition: file; filename="root#{node.path.gsub('/', '%2F')}"\r\n\ Content-Type: application/x-directory\r\n\ \r\n\ \r\n\ } elsif node.file? buf << %Q{\ --#{boundary}\r\n\ Content-Disposition: file; filename="root#{node.path.gsub('/', '%2F')}"\r\n\ Content-Type: application/octet-stream\r\n\ \r\n\ #{node.content}\r\n\ } else raise "Unknown node type: #{node}" end end buf << %Q{\ --#{boundary}\r\n\ } end headers = { 'Content-Type' => "multipart/form-data; boundary=#{boundary}" } stream = producer.stream uploaded_nodes = [] post("add?encoding=json&r=true&progress=true", stream, headers) do |chunk| next if chunk.empty? upload = nil begin upload = JSON.parse(chunk) rescue JSON::ParserError end next if upload.nil? path, bytes, hash = ['Name', 'Bytes', 'Hash'].map { |p| upload[p] } next if not path.start_with?('root/') path = path[4..-1] node = node_map[path] next if not node node.bytes = bytes if bytes node.hash = hash if hash if block_given? block.call(node) elsif hash uploaded_nodes << node end end block_given? ? nil : uploaded_nodes end
cat(path)
click to toggle source
# File lib/ipfs-api/connection.rb, line 80 def cat path result = '' post("cat?arg=#{CGI.escape(path)}") do |chunk| result << chunk end result end
get(path, destination = nil)
click to toggle source
# File lib/ipfs-api/connection.rb, line 88 def get path, destination = nil producer = IO::StreamProducer.new do |buf| post("get?arg=#{CGI.escape(path)}") do |chunk| buf << chunk end buf.close end if destination.nil? return producer.stream else return IO::Tar.extract(producer.stream, destination) end end
id()
click to toggle source
# File lib/ipfs-api/connection.rb, line 102 def id JSON.parse(post('id').body)['ID'] end
ls(path)
click to toggle source
# File lib/ipfs-api/connection.rb, line 106 def ls path JSON.parse(post("ls?arg=#{CGI.escape(path)}").body) end
name()
click to toggle source
# File lib/ipfs-api/connection.rb, line 110 def name NameCommand.new(self) end