ipfs-http-client-rb 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d09bf89ae84e1ce2363b4ada3831ab5b1ce57022b05ba49853d42c783214436a
4
+ data.tar.gz: 13b450716eea62fc7cea55b931d34554aec5378b55f6f076bdd04cf0888e5b94
5
+ SHA512:
6
+ metadata.gz: 135ea9bf170b2819701ec028737dd830594ff133d4a66edb08ae275751215a1539c806701fe825e926c536cf18e99b41bd3571fb406b89979285926539cfa0fd
7
+ data.tar.gz: ea93ed525cd5f5f79ef3fb27532209e343acc5bcde8b6edbd9c7d336ec7208f4fad2133d854a246651d4c63caf30f09547dfaef7e82ed03004ce21e3610f8002
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # IPFS HTTP Client for Ruby
2
+
3
+ ![](https://ipfs.io/ipfs/QmQJ68PFMDdAsgCZvA1UVzzn18asVcf7HVvCDgpjiSCAse)
4
+
5
+ > This gem provides IPFS HTTP client for Ruby, see [https://ipfs.io](https://ipfs.io).
6
+
7
+ **Work in progress.**
8
+
9
+ ## Installation
10
+ Requires Ruby >= 2.1, since it uses the new required keyword arguments syntax.
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'ipfs-http-client-rb'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install ipfs-http-client-rb
25
+
26
+ ## Usage
27
+ ### Initialize client
28
+
29
+ ```
30
+ pry(main)> client = Ipfs::Client.new 'http://localhost:5001'
31
+ => #<Ipfs::Client:0x00007fb6ec459610 @endpoint="http://localhost:5001">
32
+
33
+ ```
34
+
35
+ ### add
36
+ #### Add a file
37
+
38
+ ```
39
+ pry(main)> client.add "a.txt"
40
+ {"Name"=>"a.txt", "Hash"=>"Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9", "Size"=>"10"}
41
+ ```
42
+
43
+ #### Add a directory
44
+ ```
45
+ pry(main)> client.add_dir "~/ipfs"
46
+ => [{"Name"=>"ipfs/sub/txt.txt", "Hash"=>"QmVpJWKCh3QbJYGWyPXXBY4t95B89uEMNT7VUoD2JtkpH9", "Size"=>"12"},
47
+ {"Name"=>"ipfs/xx.txt", "Hash"=>"QmUFgREzM1tEKTvUHdo5hTQhxn9j1qHM1ZwR4u8e8Srgji", "Size"=>"13"},
48
+ {"Name"=>"ipfs/sub", "Hash"=>"QmdcJi9C6Gy2yCXGhevEmuAn4o4c57PsL8Va3b22TXpBRV", "Size"=>"65"},
49
+ {"Name"=>"ipfs", "Hash"=>"QmVwpFxhz1HJ96DbrXhgFBxPj4RkGT5snw6pACXkVwPbSw", "Size"=>"175"}]
50
+ ```
51
+
52
+ ### ls
53
+
54
+ ```
55
+ pry(main)> client.ls "Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9"
56
+ => {"Objects"=>[{"Hash"=>"Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9", "Links"=>[]}]
57
+ ```
58
+
59
+ ### cat
60
+ #### Cat a file
61
+ ```
62
+ pry(main)> client.cat "Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9"
63
+ => "The file content.\n"
64
+ ```
65
+
66
+ #### Cat a file and write to disk
67
+ ```
68
+ pry(main)> client.cat_to_file "Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9", "~/ipfs/a.txt"
69
+ => true
70
+ ```
71
+
72
+ ### pin rm
73
+ ```
74
+ pry(main)> client.pin_rm "Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9"
75
+ => {"Pins"=>["Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9"]}
76
+ ```
77
+
78
+ ### pin add
79
+ ```
80
+ pry(main)> client.pin_add "Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9"
81
+ => {"Pins"=>["Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9"]}
82
+ ```
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
87
+
88
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/Fryie/ipfs-ruby).
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ipfs-http-client-rb/version"
4
+ require "ipfs-http-client-rb/commands"
5
+ require "ipfs-http-client-rb/client"
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ipfs-http-client-rb/commands'
4
+
5
+ module Ipfs
6
+ class Client
7
+ DEFAULT_ENDPOINT = 'http://localhost:5000'.freeze
8
+ API_VERSION = 'v0'.freeze
9
+
10
+ attr_reader :endpoint
11
+
12
+ def self.default
13
+ new(DEFAULT_ENDPOINT)
14
+ end
15
+
16
+ def initialize(endpoint)
17
+ @endpoint = endpoint
18
+ end
19
+
20
+ def base_url
21
+ "#{endpoint}/api/#{API_VERSION}"
22
+ end
23
+
24
+ def ls(node)
25
+ Commands::Ls.call self, node
26
+ end
27
+
28
+ def cat(node)
29
+ Commands::Cat.call self, node
30
+ end
31
+
32
+ def cat_to_file(node, path)
33
+ Commands::CatToFile.call self, node, path
34
+ end
35
+
36
+ def add(file)
37
+ Commands::Add.call self, file
38
+ end
39
+
40
+ def add_dir(dir)
41
+ Commands::AddDir.call self, dir
42
+ end
43
+
44
+ def pin_rm(node, recursive: true)
45
+ Commands::PinRm.call self, node, recursive: recursive
46
+ end
47
+
48
+ def pin_add(node, recursive: true)
49
+ Commands::PinAdd.call self, node, recursive: recursive
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,8 @@
1
+ require 'ipfs-http-client-rb/commands/base'
2
+ require 'ipfs-http-client-rb/commands/ls'
3
+ require 'ipfs-http-client-rb/commands/cat'
4
+ require 'ipfs-http-client-rb/commands/cat_to_file'
5
+ require 'ipfs-http-client-rb/commands/add'
6
+ require 'ipfs-http-client-rb/commands/add_dir'
7
+ require 'ipfs-http-client-rb/commands/pin_rm'
8
+ require 'ipfs-http-client-rb/commands/pin_add'
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ipfs
4
+ module Commands
5
+ class Add < Ipfs::Commands::Base
6
+ def self.call(client, file)
7
+ response = request(client, "/add", {form: {file: HTTP::FormData::File.new(file)}})
8
+ parse(response.body)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ipfs
4
+ module Commands
5
+ class AddDir < Ipfs::Commands::Base
6
+ def self.call(client, dir)
7
+ root = File.dirname dir
8
+
9
+ form_files = []
10
+ form_files << HTTP::FormData::Part.new(
11
+ "",
12
+ content_type: 'application/x-directory',
13
+ filename: File.basename(dir)
14
+ )
15
+
16
+ Dir["#{dir}/**/*"].each do |file|
17
+ filename = file[(root.size + 1)..file.size]
18
+
19
+ if File.directory?(file)
20
+ form_files << HTTP::FormData::Part.new(
21
+ "",
22
+ content_type: 'application/x-directory',
23
+ filename: filename
24
+ )
25
+ else
26
+ form_files << HTTP::FormData::File.new(file, filename: filename)
27
+ end
28
+ end
29
+
30
+ response = request(client, "/add?recursive=true",
31
+ {form: {file: form_files }})
32
+
33
+ response.body.to_s.split("\n").map { |s| JSON.parse s }
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'http'
4
+ require 'json'
5
+ require 'http/form_data'
6
+
7
+ module Ipfs
8
+ module Commands
9
+ class Error < StandardError; end
10
+
11
+ class Base
12
+ def self.request(client, path_with_query, args = nil)
13
+ url = "#{client.base_url}#{path_with_query}"
14
+ response = HTTP.get(*[url, args].compact)
15
+ if response.code >= 200 && response.code <= 299
16
+ response
17
+ else
18
+ raise Error, response.body
19
+ end
20
+ end
21
+
22
+ def self.parse(str)
23
+ JSON.parse(str)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ipfs
4
+ module Commands
5
+ class Cat < Ipfs::Commands::Base
6
+ def self.call(client, node)
7
+ request(client, "/cat?arg=#{node}").to_s
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ipfs
4
+ module Commands
5
+ class CatToFile < Ipfs::Commands::Base
6
+ def self.call(client, node, path)
7
+ response = request(client, "/cat?arg=#{node}")
8
+
9
+ begin
10
+ file = File.open(path, 'wb')
11
+ while (chunk = response.body.readpartial)
12
+ file.write chunk
13
+ end
14
+ ensure
15
+ file.close if file
16
+ end
17
+
18
+ true
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ipfs
4
+ module Commands
5
+ class Ls < Ipfs::Commands::Base
6
+ def self.call(client, node)
7
+ response = request(client, "/ls?arg=#{node}")
8
+ parse(response.body)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ipfs
4
+ module Commands
5
+ class PinAdd < Ipfs::Commands::Base
6
+ def self.call(client, node, recursive: false)
7
+ response = request(client, "/pin/add?arg=#{node}&recursive=#{recursive}")
8
+ parse(response.body)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ipfs
4
+ module Commands
5
+ class PinRm < Ipfs::Commands::Base
6
+ def self.call(client, node, recursive: false)
7
+ response = request(client, "/pin/rm?arg=#{node}&recursive=#{recursive}")
8
+ parse(response.body)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ipfs
4
+ VERSION = '0.0.1'
5
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ipfs-http-client-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dongri Jin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.4.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.4.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.2.16
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.2.16
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 13.0.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 13.0.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 11.1.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 11.1.3
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.10.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.10.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 3.12.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 3.12.2
97
+ - !ruby/object:Gem::Dependency
98
+ name: sinatra
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.1.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.1.0
111
+ description: Ruby HTTP client for the Interplanetary File System
112
+ email:
113
+ - dongrify@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - README.md
119
+ - lib/ipfs-http-client-rb.rb
120
+ - lib/ipfs-http-client-rb/client.rb
121
+ - lib/ipfs-http-client-rb/commands.rb
122
+ - lib/ipfs-http-client-rb/commands/add.rb
123
+ - lib/ipfs-http-client-rb/commands/add_dir.rb
124
+ - lib/ipfs-http-client-rb/commands/base.rb
125
+ - lib/ipfs-http-client-rb/commands/cat.rb
126
+ - lib/ipfs-http-client-rb/commands/cat_to_file.rb
127
+ - lib/ipfs-http-client-rb/commands/ls.rb
128
+ - lib/ipfs-http-client-rb/commands/pin_add.rb
129
+ - lib/ipfs-http-client-rb/commands/pin_rm.rb
130
+ - lib/ipfs-http-client-rb/version.rb
131
+ homepage: https://github.com/dongri/ipfs-http-client-rb
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '2.7'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubygems_version: 3.2.3
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: IPFS HTTP client
154
+ test_files: []