Class: IPFS::Connection
- Inherits:
-
Object
- Object
- IPFS::Connection
- Defined in:
- lib/ipfs-api/connection.rb
Defined Under Namespace
Classes: NameCommand
Instance Method Summary collapse
- #add(nodes, &block) ⇒ Object
- #cat(path) ⇒ Object
- #get(path, destination = nil) ⇒ Object
- #id ⇒ Object
-
#initialize(base_url = 'http://127.0.0.1:5001') ⇒ Connection
constructor
A new instance of Connection.
- #ls(path) ⇒ Object
- #name ⇒ Object
Constructor Details
#initialize(base_url = 'http://127.0.0.1:5001') ⇒ Connection
Returns a new instance of Connection.
9 10 11 |
# File 'lib/ipfs-api/connection.rb', line 9 def initialize base_url = 'http://127.0.0.1:5001' @base_url = base_url end |
Instance Method Details
#add(nodes, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ipfs-api/connection.rb', line 13 def add nodes, &block boundaries = [ generate_boundary ] headers = { 'Content-Disposition' => 'form-data: name="files"', 'Content-Type' => "multipart/form-data; boundary=#{boundaries[0]}" } walker = Upload::TreeWalker.depth_first(nodes) node_map = {} stream = IO::ReadFromWriterIO.new do |buf| next if walker.nil? begin node, depth = walker.next node_map[node.path] = node rescue StopIteration depth = -1 walker = nil end while boundaries.size > depth+1 && boundary = boundaries.shift buf << %Q{\ --#{boundary}--\r\n\ \r\n\ \r\n\ } end next if walker.nil? if node.folder? boundaries.unshift generate_boundary buf << %Q{\ --#{boundaries[1]}\r\n\ Content-Disposition: form-data; name="file"; filename="#{node.path}"\r\n\ Content-Type: multipart/mixed; boundary=#{boundaries[0]}\r\n\ \r\n\ \r\n\ } elsif node.file? buf << %Q{\ --#{boundaries[0]}\r\n\ Content-Disposition: file; filename="#{node.path}"\r\n\ Content-Type: application/octet-stream\r\n\ \r\n\ #{node.content}\r\n\ } else raise "Unknown node type: #{node}" end end nodes = [] post("add?encoding=json&r=true&progress=true", stream, headers) do |chunk| next if chunk.empty? upload = JSON.parse(chunk) path, bytes, hash = ['Name', 'Bytes', 'Hash'].map { |p| upload[p] } node = node_map[path] node.bytes = bytes if bytes node.hash = hash if hash if block_given? block.call(node) elsif hash nodes << node end end block_given? ? nil : nodes end |
#cat(path) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/ipfs-api/connection.rb', line 76 def cat path result = '' post("cat?arg=#{CGI.escape(path)}") do |chunk| result << chunk end result end |
#get(path, destination = nil) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ipfs-api/connection.rb', line 84 def get path, destination = nil stream = IO::ReadFromWriterIO.new do |buf| post("get?arg=#{CGI.escape(path)}") do |chunk| buf << chunk end buf.close end if destination.nil? return stream else return IO::Tar.extract(stream, destination) end end |
#id ⇒ Object
98 99 100 |
# File 'lib/ipfs-api/connection.rb', line 98 def id JSON.parse(post('id').body)['ID'] end |
#ls(path) ⇒ Object
102 103 104 |
# File 'lib/ipfs-api/connection.rb', line 102 def ls path JSON.parse(post("ls?arg=#{CGI.escape(path)}").body) end |
#name ⇒ Object
106 107 108 |
# File 'lib/ipfs-api/connection.rb', line 106 def name NameCommand.new(self) end |