ruby-ipfs-api 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6be77b3c8b110052ee64c9dae0041ca4a01da400
4
- data.tar.gz: d667d4ceb0722a86261c88218f5c6444803659b0
3
+ metadata.gz: 24d678460e6540e0cdb6ba0adb78059cf62cb57c
4
+ data.tar.gz: 3654e0e6d008375a2a1c58ddd921dcb7992dbdeb
5
5
  SHA512:
6
- metadata.gz: 2998fe01ce08f03066de75ee6d1da7b00e10214b7da5bbbf0a7b37dcf4d5af51336366b86821d0b51fa3b2560dc46af890f317eca2bb3d3e7168bade698f1baf
7
- data.tar.gz: 7325c99bfbddad37937694b9496f51c06b18b9f22b2addac51d89034e83b1bce1babb0f3b027ac9db0048780d97ca29e2c43a7c4512febea64e6025c7c3647e2
6
+ metadata.gz: 68ebf7fc1d165de81ba34621dff8a403556fdb46e339dface000846c3d740c328d034ffdc2e6decdf6b98624a53a219e83d761d8729d6673a054a09295570627
7
+ data.tar.gz: c9fa6d861c4f2bb36f5e1b9341c5cf28d9532b7253d5c271b5cd6a8b8ca2cbb4b88f64b73f257801a40783734ac98abe21b2add0850eb85bdd5c5d7654d8b0f3
data/lib/api/files/cat.rb CHANGED
@@ -1,4 +1,4 @@
1
- require_relative '../../dagnode'
1
+ require_relative '../../dagstream'
2
2
  require_relative '../../errors'
3
3
 
4
4
  module Ipfs
@@ -6,17 +6,17 @@ module Ipfs
6
6
  class Cat
7
7
  PATH = '/cat'
8
8
 
9
- def self.make_request multi_hash
9
+ def self.build_request(multi_hash)
10
10
  {
11
- method: :get,
11
+ verb: :get,
12
12
  path: PATH,
13
- params: { :arg => multi_hash }
13
+ options: { params: { arg: multi_hash.raw } }
14
14
  }
15
15
  end
16
16
 
17
- def self.parse_response response
18
- DagNode.new response
17
+ def self.parse_response(response)
18
+ DagStream.new response
19
19
  end
20
20
  end
21
21
  end
22
- end
22
+ end
@@ -0,0 +1,19 @@
1
+ module Ipfs
2
+ module Command
3
+ class Ls
4
+ PATH = '/ls'
5
+
6
+ def self.build_request(multi_hash)
7
+ {
8
+ verb: :get,
9
+ path: PATH,
10
+ options: { params: { arg: multi_hash.raw } }
11
+ }
12
+ end
13
+
14
+ def self.parse_response(response)
15
+ JSON.parse(response.body)['Objects'][0]['Links']
16
+ end
17
+ end
18
+ end
19
+ end
@@ -3,16 +3,16 @@ module Ipfs
3
3
  class Id
4
4
  PATH = '/id'
5
5
 
6
- def self.make_request
6
+ def self.build_request
7
7
  {
8
- method: :get,
8
+ verb: :post,
9
9
  path: PATH
10
10
  }
11
11
  end
12
12
 
13
- def self.parse_response response
13
+ def self.parse_response(response)
14
14
  JSON.parse response
15
15
  end
16
16
  end
17
17
  end
18
- end
18
+ end
@@ -3,14 +3,14 @@ module Ipfs
3
3
  class Version
4
4
  PATH = '/version'
5
5
 
6
- def self.make_request
6
+ def self.build_request
7
7
  {
8
- method: :get,
8
+ verb: :get,
9
9
  path: PATH
10
10
  }
11
11
  end
12
12
 
13
- def self.parse_response response
13
+ def self.parse_response(response)
14
14
  JSON.parse response
15
15
  end
16
16
  end
data/lib/base.rb ADDED
@@ -0,0 +1,43 @@
1
+ module Ipfs
2
+ class Base58
3
+ ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
4
+ BASE = ALPHABET.length
5
+
6
+ def self.decode(number)
7
+ valid?(number) \
8
+ ? to_base10(number)
9
+ : 0
10
+ end
11
+
12
+ def self.to_base10(base58_number)
13
+ base58_number
14
+ .reverse
15
+ .split(//)
16
+ .each_with_index
17
+ .reduce(0) do |base10_number, (base58_numeral, index)|
18
+ base10_number + ALPHABET.index(base58_numeral) * (BASE**index)
19
+ end
20
+ end
21
+
22
+ def self.valid?(number)
23
+ number.match?(/\A[#{ALPHABET}]+\z/)
24
+ end
25
+
26
+ def self.encode(base10_number)
27
+ base10_number.is_a?(Integer) \
28
+ ? to_base58(base10_number) \
29
+ : ''
30
+ end
31
+
32
+ def self.to_base58(base10_number)
33
+ base58_number = ''
34
+
35
+ begin
36
+ base58_number << ALPHABET[base10_number % BASE]
37
+ base10_number /= BASE
38
+ end while base10_number > 0
39
+
40
+ base58_number.reverse
41
+ end
42
+ end
43
+ end
data/lib/dagstream.rb ADDED
@@ -0,0 +1,21 @@
1
+ require_relative './errors'
2
+
3
+ module Ipfs
4
+ class DagStream
5
+ attr_reader :content
6
+
7
+ def initialize(response)
8
+ if response.status.code == 200
9
+ @content = response
10
+ else
11
+ raise Error::InvalidDagStream, JSON.parse(response.body)['Message']
12
+ end
13
+ end
14
+
15
+ def to_s
16
+ @content.body.to_s
17
+ end
18
+
19
+ alias to_str to_s
20
+ end
21
+ end
data/lib/errors.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  module Ipfs
2
2
  module Error
3
- class InvalidDagNode < StandardError
3
+ class InvalidDagStream < StandardError
4
+ end
5
+
6
+ class InvalidMultihash < StandardError
4
7
  end
5
8
  end
6
9
  end
data/lib/ipfs-api.rb CHANGED
@@ -2,10 +2,12 @@ require_relative './request'
2
2
  require_relative './api/generic/id'
3
3
  require_relative './api/generic/version'
4
4
  require_relative './api/files/cat'
5
+ require_relative './api/files/ls'
6
+ require_relative './multihash'
5
7
 
6
8
  module Ipfs
7
9
  class Client
8
- def initialize server = {}
10
+ def initialize(server = {})
9
11
  @request = Request.new server
10
12
  end
11
13
 
@@ -17,14 +19,18 @@ module Ipfs
17
19
  execute Command::Version
18
20
  end
19
21
 
20
- def cat multi_hash
21
- execute Command::Cat, multi_hash
22
+ def cat(multi_hash)
23
+ execute Command::Cat, Multihash.new(multi_hash)
24
+ end
25
+
26
+ def ls(multi_hash)
27
+ execute Command::Ls, Multihash.new(multi_hash)
22
28
  end
23
29
 
24
30
  private
25
31
 
26
- def execute command, *args
27
- command.parse_response @request.call_api command.make_request *args
32
+ def execute(command, *args)
33
+ command.parse_response @request.call_api command.build_request *args
28
34
  end
29
35
  end
30
36
  end
data/lib/multihash.rb ADDED
@@ -0,0 +1,42 @@
1
+ require_relative './errors'
2
+
3
+ module Ipfs
4
+
5
+ class Multihash
6
+ attr_reader :hash_func_type, :digest_length, :digest_value
7
+
8
+ DEFAULT_LENGTH = 46
9
+ VALID_DIGEST_LENGTH = 'm'
10
+ FUNCTION_TYPE_CODE = {
11
+ sha256: 'Q'
12
+ }
13
+
14
+ def initialize(hash)
15
+ @hash_func_type = hash[0]
16
+ @digest_length = hash[1]
17
+ @digest_value = hash[2..-1]
18
+
19
+ raise Ipfs::Error::InvalidMultihash, "The hash '#{raw}' is invalid." unless valid?
20
+ end
21
+
22
+ def raw
23
+ "#{@hash_func_type}#{@digest_length}#{@digest_value}"
24
+ end
25
+
26
+ alias to_s raw
27
+
28
+ private
29
+
30
+ def valid?
31
+ correct_length? && encoded_digest?(:sha256)
32
+ end
33
+
34
+ def correct_length?
35
+ raw.length == DEFAULT_LENGTH
36
+ end
37
+
38
+ def encoded_digest?(encoding)
39
+ @hash_func_type == FUNCTION_TYPE_CODE[encoding]
40
+ end
41
+ end
42
+ end
data/lib/request.rb CHANGED
@@ -9,27 +9,28 @@ module Ipfs
9
9
  DEFAULT_PORT = 5001
10
10
  DEFAULT_BASE_PATH = '/api/v0'
11
11
 
12
- def initialize **api_server
12
+ def initialize(**api_server)
13
13
  @host = api_server[:host] || DEFAULT_HOST
14
14
  @port = api_server[:port] || DEFAULT_PORT
15
- @base_path = api_server[:base_path] || DEFAULT_BASE_PATH
15
+ @base_path = api_server[:base_path] || DEFAULT_BASE_PATH
16
16
  end
17
17
 
18
- def call_api command
18
+ def call_api(command)
19
19
  HTTP.request(
20
- command[:method],
20
+ command[:verb],
21
21
  url(command[:path]),
22
- params: command[:params]
22
+ command[:options] ? command[:options] : {}
23
23
  )
24
24
  end
25
25
 
26
26
  private
27
27
 
28
- def url command_path
29
- URI::HTTP.build \
28
+ def url(command_path)
29
+ URI::HTTP.build(
30
30
  host: @host,
31
31
  port: @port,
32
32
  path: @base_path + command_path
33
+ )
33
34
  end
34
35
  end
35
- end
36
+ end
metadata CHANGED
@@ -1,28 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-ipfs-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
- - Thomas Nieto
7
+ - Tom Benett
8
8
  - Neil Nilou
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-08-12 00:00:00.000000000 Z
12
+ date: 2017-11-27 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: A client library for the IPFS HTTP API, immplemented in Ruby
15
- email: thms.no@gmail.com
14
+ description: A client library for the IPFS HTTP API, implemented in Ruby
15
+ email: tom@benett.io
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/api/files/cat.rb
21
+ - lib/api/files/ls.rb
21
22
  - lib/api/generic/id.rb
22
23
  - lib/api/generic/version.rb
23
- - lib/dagnode.rb
24
+ - lib/base.rb
25
+ - lib/dagstream.rb
24
26
  - lib/errors.rb
25
27
  - lib/ipfs-api.rb
28
+ - lib/multihash.rb
26
29
  - lib/request.rb
27
30
  homepage: https://github.com/mahloun/ruby-ipfs-api
28
31
  licenses:
@@ -44,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
47
  version: '0'
45
48
  requirements: []
46
49
  rubyforge_project:
47
- rubygems_version: 2.6.8
50
+ rubygems_version: 2.6.13
48
51
  signing_key:
49
52
  specification_version: 4
50
53
  summary: ipfs client
data/lib/dagnode.rb DELETED
@@ -1,15 +0,0 @@
1
- require_relative './errors'
2
-
3
- module Ipfs
4
- class DagNode
5
- attr_reader :content
6
-
7
- def initialize http_response
8
- if http_response.status.code == 200
9
- @content = http_response.body
10
- else
11
- raise Error::InvalidDagNode.new JSON.parse(http_response.body)["Message"]
12
- end
13
- end
14
- end
15
- end