biilabs-client 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 752b4cba9ae435509057658bcac856920171db3f0612ee5cc941e1e3bf37afd2
4
+ data.tar.gz: 989ff275b67bac0ad9e79a17884436aa1a00292ab9322725b011ea36306aef90
5
+ SHA512:
6
+ metadata.gz: fca5cb12d477189f54b2765291bf21fcdddf005a492804c795be91e56a529d4eb67d86178a92e2705c6cc967320bc7d306d2a2b9d8064a660b4bce856d2d797c
7
+ data.tar.gz: 10fe5ce8262492fb95114dad535fe179cad239af49c4354094f30eb43f6c8e9c4ac26749c802044e145533015bcfbe6310cceb51a26eefa836082959d4161fb5
@@ -0,0 +1,64 @@
1
+ # Biilabs Client
2
+
3
+ To post/get tangles on IoTA
4
+
5
+ ## Install
6
+
7
+ from console
8
+
9
+ gem install biilabs-client
10
+
11
+ with bundler, write follwing line in your Gemfile
12
+
13
+ gem 'biilabs-client'
14
+
15
+ ## Usage
16
+ setup endpoint in config/biilabs-client.yml
17
+ check config/secret-keeper_example.yml for example
18
+
19
+
20
+ require on demand
21
+
22
+ irb> require 'biilabs-client'
23
+
24
+ convert string to trytes
25
+
26
+ irb> "Hello World".to_trytes.value
27
+ # "RBTC9D9DCDEAFCCDFD9DSC"
28
+
29
+ convert trytes string to normal string
30
+
31
+ irb> Trytes.new("RBTC9D9DCDEAFCCDFD9DSC").to_string
32
+ # "Hello World"
33
+
34
+ post tangle to IoTA via Biilabs
35
+
36
+ irb> BiilabsClient.new.post_tangle('my tag', 'my message')
37
+ # {
38
+ # "hash"=>"LTSDZIYLKSLQHCOZPHWWCNUNSFZCVLTZELARIONAGR9RGY9ZXC9J9AYHUZMGEODZXI9AOMJ9PCKB99999",
39
+ # "signature_and_message_fragment"=>"ADMDEAADTCGDGDPCVCTC9999...",
40
+ # "tag"=>"ADMDEAHDPCVC999999999999999",
41
+ # ...
42
+ # }
43
+
44
+ get tangle from IoTA via Biilabs
45
+
46
+ irb> BiilabsClient.new.get_tangle('LTSDZIYLKSLQHCOZPHWWCNUNSFZCVLTZELARIONAGR9RGY9ZXC9J9AYHUZMGEODZXI9AOMJ9PCKB99999')
47
+ # {
48
+ # "hash"=>"LTSDZIYLKSLQHCOZPHWWCNUNSFZCVLTZELARIONAGR9RGY9ZXC9J9AYHUZMGEODZXI9AOMJ9PCKB99999",
49
+ # "signature_and_message_fragment"=>"ADMDEAADTCGDGDPCVCTC9999...",
50
+ # "tag"=>"ADMDEAHDPCVC999999999999999",
51
+ # ...
52
+ # }
53
+
54
+
55
+ get tangles by tag from IoTA via Biilabs
56
+
57
+ irb> BiilabsClient.new.get_tangle_by_tag('ADMDEAHDPCVC999999999999999')
58
+ # {
59
+ # "transactions"=>[{
60
+ # "hash"=>"LTSDZIYLKSLQHCOZPHWWCNUNSFZCVLTZELARIONAGR9RGY9ZXC9J9AYHUZMGEODZXI9AOMJ9PCKB99999",
61
+ # "signature_and_message_fragment"=>"ADMDEAADTCGDGDPCVCTC9999...",
62
+ # "tag"=>"ADMDEAHDPCVC999999999999999",
63
+ # ...}]
64
+ # }
@@ -0,0 +1,106 @@
1
+ class BiilabsClient
2
+ attr_reader :config
3
+
4
+ def initialize
5
+ config_path = 'config/biilabs-client.yml'
6
+ env = ENV['RAILS_ENV'] || 'development'
7
+ string = File.open(config_path, 'rb') { |f| f.read }
8
+ fail "#{config_path} not existed nor not readable" if string.nil?
9
+ @config = YAML.load(string)[env]
10
+ fail "#{config_path} incorrect or environment not exist" if @config.nil?
11
+ end
12
+
13
+ def post_tangle(tag, message)
14
+ path = '/transaction'
15
+ body = {
16
+ tag: tag.to_trytes.value,
17
+ message: message
18
+ }
19
+ resp = connection.post do |req|
20
+ req.url path
21
+ req.headers['Content-Type'] = 'application/json'
22
+ req.body = body.to_json
23
+ end
24
+ response_handler(resp)
25
+ end
26
+
27
+ def get_tangle(tangle_id)
28
+ path = "/transaction/#{tangle_id}"
29
+ resp = connection.get(path)
30
+ response_handler(resp)
31
+ end
32
+
33
+ def get_tangle_by_tag(tag)
34
+ path = "/tag/#{tag}"
35
+ resp = connection.get(path)
36
+ response_handler(resp)
37
+ end
38
+
39
+ private
40
+
41
+ def connection
42
+ @connection ||= Faraday.new(url: config['endpoint']) do |faraday|
43
+ faraday.request :url_encoded # form-encode POST params
44
+ # faraday.response :logger # log requests and responses to $stdout
45
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
46
+ end
47
+ end
48
+
49
+ def response_handler(resp)
50
+ if resp.status == 200
51
+ JSON.load(resp.body)
52
+ else
53
+ { status: resp.status, body: resp.body }
54
+ end
55
+ end
56
+ end
57
+
58
+ class String
59
+ def to_trytes
60
+ Trytes.new(self)
61
+ end
62
+ end
63
+
64
+ class Trytes
65
+ attr_reader :value
66
+
67
+ def initialize(string)
68
+ @value = string
69
+ return if trytes?
70
+ @value = ''
71
+ string.each_char do |char|
72
+ asciiValue = char.unpack('c*').first
73
+ return if asciiValue > 255
74
+ firstValue = asciiValue % 27
75
+ secondValue = (asciiValue - firstValue) / 27
76
+ tryte = trytes_chars[firstValue] + trytes_chars[secondValue]
77
+ @value += tryte
78
+ end
79
+ end
80
+
81
+ def to_string
82
+ return unless trytes?
83
+ string = ''
84
+ (0..(value.length - 1)).step(2) do |i|
85
+ tryte = value[i] + value[i + 1]
86
+ break if tryte == '99'
87
+ firstValue = trytes_chars.index(tryte[0])
88
+ secondValue = trytes_chars.index(tryte[1])
89
+ decimalValue = firstValue + secondValue * 27
90
+ string += decimalValue.chr
91
+ end
92
+ string
93
+ end
94
+
95
+ private
96
+
97
+ def trytes_chars
98
+ '9ABCDEFGHIJKLMNOPQRSTUVWXYZ'
99
+ end
100
+
101
+ def trytes?
102
+ return false unless value.kind_of? String
103
+ return false unless /^[9A-Z]*$/.match(value)
104
+ true
105
+ end
106
+ end
@@ -0,0 +1,32 @@
1
+ describe BiilabsClient do
2
+ before(:all) do
3
+ @client = BiilabsClient.new
4
+ @tag = 'my tag'
5
+ @mssage = 'my document digest'
6
+ end
7
+ describe '.post_tangle' do
8
+ it 'should return tangle info' do
9
+ result = @client.post_tangle(@tag, @mssage)
10
+ expect(result['hash'].to_trytes.value).to eq(result['hash'])
11
+ expect(result['tag'].to_trytes.to_string).to eq(@tag)
12
+ expect(result['signature_and_message_fragment'].to_trytes.to_string).to eq(@mssage)
13
+ end
14
+ end
15
+
16
+ describe '.get_tangle' do
17
+ it 'should return tangle info' do
18
+ tangle_id = 'QTIQXETRPHVP9WNATWGT9CHEMMBVMEYJWXWQ9OCETLPFI9YEJEYFZMIQCPKGKNVPIQZNWDIS9FSEA9999'
19
+ result = @client.get_tangle(tangle_id)
20
+ expect(result['hash'].to_trytes.value).to eq(tangle_id)
21
+ expect(result['tag'].to_trytes.to_string).to eq(@tag)
22
+ expect(result['signature_and_message_fragment'].to_trytes.to_string).to eq(@mssage)
23
+ end
24
+ end
25
+
26
+ describe '.get_tangle_by_tag' do
27
+ it 'should return tangles with same tag' do
28
+ result = @client.get_tangle_by_tag(@tag.to_trytes.value)
29
+ expect(result['transactions'].first['tag'].to_trytes.to_string).to eq(@tag)
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: biilabs-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ray Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-06-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.15'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.8.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.8.0
55
+ description: Wrapper for BiiLabs APIs
56
+ email: ray-lee@kdanmobile.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files:
60
+ - README.md
61
+ files:
62
+ - README.md
63
+ - lib/biilabs_client.rb
64
+ - spec/biilabs_client_spec.rb
65
+ homepage: https://github.com/redtear1115/biilabs-client
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options:
71
+ - "--charset=UTF-8"
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.3.1
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.0.1
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: A gem to work with BiiLabs Tangle.
89
+ test_files:
90
+ - spec/biilabs_client_spec.rb