ipfs-http-client 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1c484d383e82fbda6adc76d1be04f4c435b3c3fdc88dfb104137ccb6040da8bf
4
+ data.tar.gz: 051171d83d847886ca6324d58b4c45b994ee1f8032591fffff3a28159567f9de
5
+ SHA512:
6
+ metadata.gz: 0d022faa8431d19fd449102fc96cc852cb39d2c0ffdfc4762f6e6743e0275af49e06d8ecebef30af567619866e98efd29bc2563c895d4484cc2eb289ea64424a
7
+ data.tar.gz: a9e7afacf2587b67d32fe9961c50c78a1397608c5ed0c5f8cd287bb306613c6a3d645c70860b6598621b31a76e9193700e46cc4280f280bbbd858666aa7ea2ce
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /.idea/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ /.ruby-version
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ipfs.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Pierpaolo Frasa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
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'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install ipfs
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
+ ## Development
79
+
80
+ 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.
81
+
82
+ 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).
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/Fryie/ipfs-ruby).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ipfs-http-client"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ipfs-http-client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ipfs-http-client'
8
+ spec.version = Ipfs::VERSION
9
+ spec.authors = ['Pierpaolo Frasa', 'Jingyang']
10
+ spec.email = ['pfrasa@gmail.com', 'simple.continue@gmail.com']
11
+ spec.license = 'MIT'
12
+
13
+ spec.summary = 'IPFS HTTP client'
14
+ spec.description = 'Ruby HTTP client for the Interplanetary File System'
15
+ spec.homepage = 'https://ipfs.io'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'http', '~> 3.0'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.10'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'byebug', '~> 9'
27
+ spec.add_development_dependency 'rspec', '~> 3'
28
+ spec.add_development_dependency 'webmock', '~> 3'
29
+ spec.add_development_dependency 'sinatra', '~> 2'
30
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ipfs-http-client/version"
4
+ require "ipfs-http-client/commands"
5
+ require "ipfs-http-client/client"
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ipfs-http-client/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
+ end
48
+ end
@@ -0,0 +1,7 @@
1
+ require 'ipfs-http-client/commands/base'
2
+ require 'ipfs-http-client/commands/ls'
3
+ require 'ipfs-http-client/commands/cat'
4
+ require 'ipfs-http-client/commands/cat_to_file'
5
+ require 'ipfs-http-client/commands/add'
6
+ require 'ipfs-http-client/commands/add_dir'
7
+ require 'ipfs-http-client/commands/pin_rm'
@@ -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 = http_get(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 = http_get(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,22 @@
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 Base
10
+ def self.http_get(client, path_with_query, args = nil)
11
+ url = "#{client.base_url}#{path_with_query}"
12
+ response = HTTP.get(*[url, args].compact)
13
+ yield response if block_given?
14
+ response
15
+ end
16
+
17
+ def self.parse(str)
18
+ JSON.parse(str)
19
+ end
20
+ end
21
+ end
22
+ 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
+ http_get(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
+ http_get(client, "/cat?arg=#{node}") do |response|
8
+ begin
9
+ file = File.open(path, 'wb')
10
+ while (chunk = response.body.readpartial)
11
+ file.write chunk
12
+ end
13
+ ensure
14
+ file.close if file
15
+ end
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 = http_get(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 PinRm < Ipfs::Commands::Base
6
+ def self.call(client, node, recursive: false)
7
+ response = http_get 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.3.0'
5
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ipfs-http-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Pierpaolo Frasa
8
+ - Jingyang
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2019-05-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: http
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.10'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.10'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '10.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: byebug
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '9'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '9'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '3'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3'
84
+ - !ruby/object:Gem::Dependency
85
+ name: webmock
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '3'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '3'
98
+ - !ruby/object:Gem::Dependency
99
+ name: sinatra
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '2'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '2'
112
+ description: Ruby HTTP client for the Interplanetary File System
113
+ email:
114
+ - pfrasa@gmail.com
115
+ - simple.continue@gmail.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".travis.yml"
123
+ - Gemfile
124
+ - LICENSE
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - ipfs-http-client.gemspec
130
+ - lib/ipfs-http-client.rb
131
+ - lib/ipfs-http-client/client.rb
132
+ - lib/ipfs-http-client/commands.rb
133
+ - lib/ipfs-http-client/commands/add.rb
134
+ - lib/ipfs-http-client/commands/add_dir.rb
135
+ - lib/ipfs-http-client/commands/base.rb
136
+ - lib/ipfs-http-client/commands/cat.rb
137
+ - lib/ipfs-http-client/commands/cat_to_file.rb
138
+ - lib/ipfs-http-client/commands/ls.rb
139
+ - lib/ipfs-http-client/commands/pin_rm.rb
140
+ - lib/ipfs-http-client/version.rb
141
+ homepage: https://ipfs.io
142
+ licenses:
143
+ - MIT
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 2.7.9
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: IPFS HTTP client
165
+ test_files: []