biilabs-test 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.
- checksums.yaml +7 -0
- data/lib/biilabs.rb +10 -0
- data/lib/biilabs/base.rb +8 -0
- data/lib/biilabs/client.rb +134 -0
- data/lib/biilabs/configuration.rb +21 -0
- data/lib/biilabs/default_tryter.rb +75 -0
- data/lib/biilabs/version.rb +3 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: aec13f13de0ee724bdbda24f4626b1d6c84340fe537d94bb92ff9ffd6456945b
|
4
|
+
data.tar.gz: 312ef68ee19761b1a7a9066127d6adf368e5cc38e2c2d4343586c1e8d9b5fa8a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 962e9c76611d99f158b807393596bf85a16139cea1ca5f2e9b3caba21e72035aebd325dfdbf7344ef25a665b695c27d3519c5ad76692285d642d8f4e48453da2
|
7
|
+
data.tar.gz: a542c97c4bf68c6751dbce3f4f65581418f03617ad1ea43e3cf315e3ee0682756a77ec51aa69a7ab13f7135eca6d05afda1e2944f94377557d0aac04764dfdeb
|
data/lib/biilabs.rb
ADDED
data/lib/biilabs/base.rb
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'json'
|
3
|
+
require 'biilabs/default_tryter.rb'
|
4
|
+
require 'biilabs/configuration.rb'
|
5
|
+
require 'biilabs/base.rb'
|
6
|
+
|
7
|
+
# TODO : need more info about response status
|
8
|
+
module Biilabs
|
9
|
+
class Client < Base
|
10
|
+
|
11
|
+
# API_HOST = 'http://node0.puyuma.org:5566'
|
12
|
+
SAVE_PATH = '/transaction'
|
13
|
+
GET_BY_ID_PATH = '/transaction'
|
14
|
+
GET_BY_TAG_PATH = '/tag'
|
15
|
+
|
16
|
+
def initialize(option={})
|
17
|
+
@show_raw_data = false
|
18
|
+
super(option)
|
19
|
+
end
|
20
|
+
|
21
|
+
def show_raw_data(show=true)
|
22
|
+
@show_raw_data = show
|
23
|
+
end
|
24
|
+
|
25
|
+
def save(tag: 'default', message:)
|
26
|
+
# TODO : check max length of tag & message (27 trytes)
|
27
|
+
# max length of message is 2187 ?
|
28
|
+
|
29
|
+
data = biilabs_put_format(tag: tag, message: message)
|
30
|
+
response = http_put_tangle(data)
|
31
|
+
raw_data = JSON.parse(response.body)
|
32
|
+
info = node_summary(raw_data)
|
33
|
+
|
34
|
+
return_result(
|
35
|
+
response: response,
|
36
|
+
info: info,
|
37
|
+
raw_data: raw_data
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
def get(node_id)
|
42
|
+
response = http_get_tangle(node_id)
|
43
|
+
raw_data = JSON.parse(response.body)
|
44
|
+
info = node_summary(raw_data)
|
45
|
+
|
46
|
+
return_result(
|
47
|
+
response: response,
|
48
|
+
info: info,
|
49
|
+
raw_data: raw_data
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_by_tag(tag)
|
54
|
+
tryte_tag = tryter.to_trytes(tag)
|
55
|
+
response = http_tag_get_tangle(tryte_tag)
|
56
|
+
raw_data = JSON.parse(response.body)
|
57
|
+
info = nodes_summary(raw_data)
|
58
|
+
|
59
|
+
return_result(
|
60
|
+
response: response,
|
61
|
+
info: info,
|
62
|
+
raw_data: raw_data
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def tryter
|
69
|
+
@tryter ||= DefaultTryter.new
|
70
|
+
end
|
71
|
+
|
72
|
+
def connection
|
73
|
+
@connection ||= Faraday.new(url: config.host) do |faraday|
|
74
|
+
faraday.request :url_encoded # form-encode POST params
|
75
|
+
faraday.response :logger # log requests and responses to $stdout
|
76
|
+
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def return_result(response:, info:, raw_data:)
|
81
|
+
result = {
|
82
|
+
status: response.status,
|
83
|
+
info: info
|
84
|
+
}
|
85
|
+
result[:raw_data] = raw_data if @show_raw_data
|
86
|
+
result
|
87
|
+
end
|
88
|
+
|
89
|
+
def biilabs_put_format(tag: ,message:)
|
90
|
+
trytes_tag = tryter.to_trytes(tag)
|
91
|
+
trytes_message = tryter.to_trytes(message)
|
92
|
+
{ tag: trytes_tag, message: trytes_message }.to_json
|
93
|
+
end
|
94
|
+
|
95
|
+
KEY_MESSAGE = 'signature_and_message_fragment'
|
96
|
+
KEY_TAG = 'tag'
|
97
|
+
KEY_NODE_ID = 'hash'
|
98
|
+
KEY_CREATED_AT = 'attachment_timestamp'
|
99
|
+
def node_summary(result)
|
100
|
+
{
|
101
|
+
message: tryter.from_trytes(result[KEY_MESSAGE]),
|
102
|
+
tag: tryter.from_trytes(result[KEY_TAG]),
|
103
|
+
node_id: result[KEY_NODE_ID],
|
104
|
+
created_at: result[KEY_NODE_ID]
|
105
|
+
}
|
106
|
+
end
|
107
|
+
|
108
|
+
def nodes_summary(result_of_nodes)
|
109
|
+
result_of_nodes['transactions'].map do |result_of_node|
|
110
|
+
node_summary(result_of_node)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def http_put_tangle(trytes_data)
|
115
|
+
connection.post do |req|
|
116
|
+
req.url SAVE_PATH
|
117
|
+
req.headers['Content-Type'] = 'application/json'
|
118
|
+
req.body = trytes_data
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def http_get_tangle(node_id)
|
123
|
+
connection.get do |req|
|
124
|
+
req.url "#{GET_BY_ID_PATH}/#{node_id}"
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def http_tag_get_tangle(tag)
|
129
|
+
connection.get do |req|
|
130
|
+
req.url "#{GET_BY_TAG_PATH}/#{tag}"
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Biilabs
|
2
|
+
class << self
|
3
|
+
attr_writer :configuration
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.configuration
|
7
|
+
yield config
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.config
|
11
|
+
@config ||= Configuration.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.reset
|
15
|
+
@config = Configuration.new
|
16
|
+
end
|
17
|
+
|
18
|
+
class Configuration
|
19
|
+
attr_accessor :host
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# You can write other function to map string to TRYTE_VALUES
|
2
|
+
#
|
3
|
+
# !!!!!!! Waring !!!!!!
|
4
|
+
# for this map-function
|
5
|
+
# all input chars must be a "ASCII character"
|
6
|
+
|
7
|
+
module Biilabs
|
8
|
+
class DefaultTryter
|
9
|
+
TRYTE_VALUES = '9ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
10
|
+
TRYTE_REGEX = /^[9A-Z]*$/
|
11
|
+
|
12
|
+
def is_string(input)
|
13
|
+
input.kind_of? String
|
14
|
+
end
|
15
|
+
|
16
|
+
def is_trytes(input)
|
17
|
+
is_string(input) && TRYTE_REGEX.match?(input)
|
18
|
+
end
|
19
|
+
|
20
|
+
# ==== biilabs format example ====
|
21
|
+
# raw message : 'p'
|
22
|
+
# message after method "to_trytes" : 'PC'
|
23
|
+
# message store on BiiLabs : 'PC9999...999' (2187 chars)
|
24
|
+
#
|
25
|
+
#
|
26
|
+
# 1. ignore 99 in method "from_trytes"
|
27
|
+
# 2. "output of mehtod to_trytes" is a string (even-length)
|
28
|
+
# "message store on BiiLabs" is a string (odd-length)
|
29
|
+
# ================================
|
30
|
+
|
31
|
+
# input must be a "ASCII character"
|
32
|
+
def to_trytes(input)
|
33
|
+
return nil if !is_string(input)
|
34
|
+
trytes = ''
|
35
|
+
|
36
|
+
(0...input.length).step(1) do |i|
|
37
|
+
char = input[i]
|
38
|
+
asciiValue = char.bytes.sum
|
39
|
+
|
40
|
+
# If not recognizable ASCII character, return null
|
41
|
+
return nil if asciiValue > 255
|
42
|
+
|
43
|
+
firstValue = asciiValue % 27
|
44
|
+
secondValue = (asciiValue - firstValue) / 27
|
45
|
+
|
46
|
+
trytesValue = TRYTE_VALUES[firstValue] + TRYTE_VALUES[secondValue]
|
47
|
+
|
48
|
+
trytes += trytesValue
|
49
|
+
end
|
50
|
+
|
51
|
+
trytes
|
52
|
+
end
|
53
|
+
|
54
|
+
def from_trytes(input)
|
55
|
+
return nil if !is_trytes(input)
|
56
|
+
|
57
|
+
outputString = ''
|
58
|
+
|
59
|
+
# "length - 1" to avoid odd-length-input
|
60
|
+
(0...(input.length - 1)).step(2) do |i|
|
61
|
+
trytes = input[i] + input[i + 1]
|
62
|
+
break if trytes == '99'
|
63
|
+
|
64
|
+
firstValue = TRYTE_VALUES.index(trytes[0])
|
65
|
+
secondValue = TRYTE_VALUES.index(trytes[1])
|
66
|
+
|
67
|
+
decimalValue = firstValue + secondValue * 27
|
68
|
+
|
69
|
+
outputString += decimalValue.chr
|
70
|
+
end
|
71
|
+
|
72
|
+
outputString
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: biilabs-test
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- gsx
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- gsx@kdanmobile.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/biilabs.rb
|
21
|
+
- lib/biilabs/base.rb
|
22
|
+
- lib/biilabs/client.rb
|
23
|
+
- lib/biilabs/configuration.rb
|
24
|
+
- lib/biilabs/default_tryter.rb
|
25
|
+
- lib/biilabs/version.rb
|
26
|
+
homepage:
|
27
|
+
licenses: []
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubygems_version: 3.0.3
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: A gem to work with "BiiLabs" Tangle.
|
48
|
+
test_files: []
|