ruby-skynet 1.1.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/skynet.rb +24 -0
- data/lib/skynet/download.rb +31 -0
- data/lib/skynet/helper.rb +35 -0
- data/lib/skynet/upload.rb +77 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f2b19582a200c85d2d1813aa2321e7727c0fdf5360e0d0a09bd300797d5cdb84
|
4
|
+
data.tar.gz: 399126445145634df99dc83db1448948701ed562f52759639828fbd421146849
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fd7750bcc625d36ac9a17c5b4e668f3e5c148430e90dd3cfb25eadf605f2a3e8fc8413dfc48ddae661456ab3940510904a97f47d90f5bd017314d7b522f78ae8
|
7
|
+
data.tar.gz: 37a1d003f6776d2778881bc715c9632d07f6d0d8c99eba707d9a25b6e199b0e3a001aa79fbaf21cd3ca675f5c608e9e53ad246bfd13f0310729111195b630100
|
data/lib/skynet.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'httparty'
|
4
|
+
require_relative 'skynet/download.rb'
|
5
|
+
require_relative 'skynet/upload.rb'
|
6
|
+
|
7
|
+
# The entrypoint for the SDK
|
8
|
+
class Skynet
|
9
|
+
|
10
|
+
# Prefix for the protocol
|
11
|
+
URI_SKYNET_PREFIX = 'sia://'
|
12
|
+
|
13
|
+
extend Download
|
14
|
+
extend Upload
|
15
|
+
|
16
|
+
# Removes the Skynet::URI_SKYNET_PREFIX constant from string
|
17
|
+
def self.strip_prefix(str)
|
18
|
+
if str.index(URI_SKYNET_PREFIX).nil?
|
19
|
+
str.delete_prefix(URI_SKYNET_PREFIX)
|
20
|
+
else
|
21
|
+
str
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'helper.rb'
|
4
|
+
|
5
|
+
# Module for handling inbound requests
|
6
|
+
module Download
|
7
|
+
|
8
|
+
# Array with http redirect & http permanent code
|
9
|
+
# Used to determine if the GET request should start downloading or follow redirect
|
10
|
+
HTTP_REDIRECT_PERMANENT = [301, 302].freeze
|
11
|
+
extend Helper::Download
|
12
|
+
|
13
|
+
# Download file from the skynet portal
|
14
|
+
# file_name & skylink is required, the rest is optional since they come with default values
|
15
|
+
def download_file(file_name, skylink, options = {}, stream = true)
|
16
|
+
options = Helper::Download.default_options
|
17
|
+
options = Helper::Download.default_options.merge(options) unless options.empty?
|
18
|
+
|
19
|
+
portal = options[:portal_url]
|
20
|
+
skylink = Skynet.strip_prefix(skylink)
|
21
|
+
url = "#{portal}#{skylink}?attachment=#{options[:download]}"
|
22
|
+
|
23
|
+
File.open(file_name, 'w') do |file|
|
24
|
+
HTTParty.get(url, follow_redirects: true, stream_body: stream) do |chunk|
|
25
|
+
file << chunk unless HTTP_REDIRECT_PERMANENT.include? chunk.code
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
'Download successful!'
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Class with general settings
|
4
|
+
module Helper
|
5
|
+
|
6
|
+
# The default skynet portal
|
7
|
+
PORTAL_URL = 'https://siasky.net/'
|
8
|
+
|
9
|
+
# Module for keeping download settings
|
10
|
+
module Download
|
11
|
+
|
12
|
+
# Default options for the download module
|
13
|
+
def self.default_options
|
14
|
+
{
|
15
|
+
portal_url: Helper::PORTAL_URL,
|
16
|
+
download: true
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Module for keeping upload settings
|
22
|
+
module Upload
|
23
|
+
|
24
|
+
# Default options for the upload module
|
25
|
+
def self.default_options
|
26
|
+
{
|
27
|
+
portal_url: Helper::PORTAL_URL,
|
28
|
+
portal_upload_path: 'skynet/skyfile',
|
29
|
+
portal_file_fieldname: 'file',
|
30
|
+
portal_directory_fieldname: 'files[]',
|
31
|
+
custom_filename: nil,
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'helper.rb'
|
4
|
+
|
5
|
+
# Module for handling outbound requests
|
6
|
+
module Upload
|
7
|
+
extend Helper::Upload
|
8
|
+
|
9
|
+
# Default headers provided for uploading files,
|
10
|
+
# any argument provided is merged with the default values
|
11
|
+
def http_post_header(data = {})
|
12
|
+
default_data = {
|
13
|
+
headers: {
|
14
|
+
'Accept' => 'application/octet-stream',
|
15
|
+
'Content-Type' => 'application/octet-stream',
|
16
|
+
'Transfer-Encoding' => 'gzip, chunked'
|
17
|
+
}
|
18
|
+
}
|
19
|
+
|
20
|
+
default_data.merge(data) unless data.empty?
|
21
|
+
end
|
22
|
+
|
23
|
+
# Uploads file to the skynet, file_path is required but options are optional since default values are provided
|
24
|
+
def upload_file(file_path, options = {})
|
25
|
+
options = Helper::Upload.default_options
|
26
|
+
options = Helper::Upload.default_options.merge(options) unless options.empty?
|
27
|
+
return "File #{file_path} does not exist!" unless File.exist?(file_path)
|
28
|
+
|
29
|
+
host = options[:portal_url]
|
30
|
+
path = options[:portal_upload_path]
|
31
|
+
filename = options[:custom_filename] || file_path
|
32
|
+
|
33
|
+
url = "#{host}#{path}?filename=#{filename}"
|
34
|
+
binary_content = IO.read(file_path, mode: 'rb')
|
35
|
+
|
36
|
+
header_data = http_post_header({
|
37
|
+
headers: {
|
38
|
+
'Content-Disposition' => 'attachment; filename="#{filename}"'
|
39
|
+
},
|
40
|
+
body: binary_content,
|
41
|
+
options[:portal_file_fieldname] => filename
|
42
|
+
})
|
43
|
+
|
44
|
+
upload_request = HTTParty.post(url, header_data)
|
45
|
+
parsed_request = upload_request.to_hash
|
46
|
+
"Upload successful, skylink: " + parsed_request['skylink']
|
47
|
+
end
|
48
|
+
|
49
|
+
# Still work in progress
|
50
|
+
# Uploads a whole directory to the skynet
|
51
|
+
# def upload_directory(directory_path, options = {})
|
52
|
+
# options = Helper::Upload.default_options
|
53
|
+
# options = Helper::Upload.default_options.merge(options) unless options.empty?
|
54
|
+
# return "Given path is not a directory!" unless Dir.exist?(directory_path)
|
55
|
+
|
56
|
+
# directory_entries = Dir.glob("#{directory_path}/*")
|
57
|
+
|
58
|
+
# binary_content = IO.read(filename, mode: 'rb')
|
59
|
+
|
60
|
+
# header_data = http_post_header({
|
61
|
+
# headers: {
|
62
|
+
# 'Content-Disposition' => 'attachment; ' + filename
|
63
|
+
# },
|
64
|
+
# body: binary_content,
|
65
|
+
# options[:portal_directory_fieldname] => filename
|
66
|
+
# })
|
67
|
+
|
68
|
+
# host = options[:portal_url]
|
69
|
+
# path = options[:portal_upload_path]
|
70
|
+
# dir_name = options[:custom_filename] || directory_path
|
71
|
+
# url = "#{host}#{path}?filename=#{dir_name}"
|
72
|
+
|
73
|
+
# upload_request = HTTParty.post(url, header_data)
|
74
|
+
# parsed_request = upload_request.to_hash
|
75
|
+
# "Upload successful, skylink: " + parsed_request['skylink']
|
76
|
+
# end
|
77
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-skynet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.15
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- beyarz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-03-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.18.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.18.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rdoc
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '6.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '6.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.9'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.9'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubygems-tasks
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.2.5
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.2.5
|
83
|
+
description: Ruby gem for integrating Sias Skynet into Ruby apps
|
84
|
+
email: ''
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- lib/skynet.rb
|
90
|
+
- lib/skynet/download.rb
|
91
|
+
- lib/skynet/helper.rb
|
92
|
+
- lib/skynet/upload.rb
|
93
|
+
homepage: https://rubygems.org/gems/sia-skynet
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata:
|
97
|
+
source_code_uri: https://github.com/beyarz/sia-skynet
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubygems_version: 3.1.2
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Sia skynet gem
|
117
|
+
test_files: []
|