Class: Skynet::Client
- Inherits:
-
Object
- Object
- Skynet::Client
- Defined in:
- lib/skynet/client.rb
Overview
Client for interacting with Skynet
Default portal is set to siasky.net
Constant Summary collapse
- DEFAULT_SKYNET_PORTAL_URL =
'https://siasky.net'
- DEFAULT_SKYNET_PORTAL_PATH =
'/skynet/skyfile'
- URI_SKYNET_PREFIX =
'sia://'
- DEFAULT_USER_AGENT =
'Sia-Agent'
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#user_agent ⇒ Object
readonly
Returns the value of attribute user_agent.
Instance Method Summary collapse
-
#download_file(path, skylink) ⇒ String
Download a file.
-
#get_metadata(skylink) ⇒ Hash
Downloads the metadata of a skylink.
-
#initialize(custom_config = {}) ⇒ Client
constructor
Initializes the client, allows to set a custom portal or uses siasky.net as default.
- #upload_directory(directory, opts = {}) ⇒ Object
-
#upload_file(file, custom_opts = {}) ⇒ String, Hash
Takes a file path and uploads it.
Constructor Details
#initialize(custom_config = {}) ⇒ Client
Initializes the client, allows to set a custom portal or uses siasky.net as default
34 35 36 37 38 39 40 41 |
# File 'lib/skynet/client.rb', line 34 def initialize(custom_config = {}) @config = @config.merge!(custom_config) @api_key = config[:api_key] || nil Typhoeus::Config.user_agent = config[:user_agent] @on_upload_progress = config[:on_upload_progress] Typhoeus::Config.verbose = true if custom_config[:verbose] end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
20 21 22 |
# File 'lib/skynet/client.rb', line 20 def config @config end |
#user_agent ⇒ Object (readonly)
Returns the value of attribute user_agent.
19 20 21 |
# File 'lib/skynet/client.rb', line 19 def user_agent @user_agent end |
Instance Method Details
#download_file(path, skylink) ⇒ String
Download a file
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/skynet/client.rb', line 104 def download_file(path, skylink) skylink = strip_uri_prefix(skylink) f = File.open(path, 'wb') begin request = Typhoeus::Request.new("#{portal}/#{skylink}", headers: default_headers) request.on_headers do |response| raise 'Request failed' if response.code != 200 end request.on_body do |chunk| f.write(chunk) end request.on_complete do |_response| f.close end request.run ensure f.close end path end |
#get_metadata(skylink) ⇒ Hash
Downloads the metadata of a skylink
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/skynet/client.rb', line 143 def (skylink) skylink = strip_uri_prefix(skylink) res = Typhoeus::Request.head( "#{portal}/#{skylink}", headers: default_headers ) raise Skynet::NoMetadataError, 'No metadata returned' unless res.headers['skynet-file-metadata'] JSON.parse res.headers['skynet-file-metadata'] end |
#upload_directory(directory, opts = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/skynet/client.rb', line 82 def upload_directory(directory, opts = {}) = (config.merge(opts)) multipart = prepare_multipart_body(directory) header = default_headers.merge({ 'Content-Type' => "multipart/form-data; boundary=#{multipart.boundary}" }) res = Typhoeus::Request.new( "#{portal}#{portal_path}", method: :post, params: .merge(filename: File.basename(directory)), headers: header, body: multipart.to_s ).run format_response(res, ) end |
#upload_file(file, custom_opts = {}) ⇒ String, Hash
Takes a file path and uploads it
76 77 78 79 80 |
# File 'lib/skynet/client.rb', line 76 def upload_file(file, custom_opts = {}) res = upload(file, config.merge(custom_opts)).run format_response(res, custom_opts) end |