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 75 76 77 78 |
# 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) ⇒ Object
80 81 82 83 84 85 86 |
# 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) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# 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 ⇒ Object
102 103 104 |
# File 'lib/ipfs-api/connection.rb', line 102 def id JSON.parse(post('id').body)['ID'] end |
#ls(path) ⇒ Object
106 107 108 |
# File 'lib/ipfs-api/connection.rb', line 106 def ls path JSON.parse(post("ls?arg=#{CGI.escape(path)}").body) end |
#name ⇒ Object
110 111 112 |
# File 'lib/ipfs-api/connection.rb', line 110 def name NameCommand.new(self) end |