Class: SafeNet::Immutable
- Inherits:
-
Object
- Object
- SafeNet::Immutable
- Defined in:
- lib/safenet.rb
Instance Method Summary collapse
- #close_writer(handle_id, cipher_opts_handle) ⇒ Object
-
#create(contents) ⇒ Object
helper.
-
#create_from_file(path) ⇒ Object
helper.
- #drop_reader_handle(handle_id) ⇒ Object
- #drop_writer_handle(handle_id) ⇒ Object
-
#dump(name, path) ⇒ Object
helper.
- #get_reader_handle(handle_id) ⇒ Object
- #get_writer_handle ⇒ Object
-
#initialize(client_obj) ⇒ Immutable
constructor
A new instance of Immutable.
-
#read(name, chunk_pos = nil, max_chunk_size = 1_000_000) ⇒ Object
helper.
-
#read_data(handle_id, range = nil) ⇒ Object
eg range: “bytes=0-1000”.
- #write_data(handle_id, contents) ⇒ Object
Constructor Details
#initialize(client_obj) ⇒ Immutable
Returns a new instance of Immutable.
1022 1023 1024 |
# File 'lib/safenet.rb', line 1022 def initialize(client_obj) @client = client_obj end |
Instance Method Details
#close_writer(handle_id, cipher_opts_handle) ⇒ Object
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 |
# File 'lib/safenet.rb', line 1087 def close_writer(handle_id, cipher_opts_handle) # entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/immutable-data/#{handle_id}/#{cipher_opts_handle}" # api call uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) req = Net::HTTP::Put.new(uri.path, { 'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}" }) res = http.request(req) res.code == "200" ? JSON.parse(res.body)["handleId"] : JSON.parse(res.body) end |
#create(contents) ⇒ Object
helper
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 |
# File 'lib/safenet.rb', line 1131 def create(contents) # plain (not encrypted) hnd_cipher = @client.cipher.get_handle # write hnd_w = @client.immutable.get_writer_handle @client.immutable.write_data(hnd_w, contents) hnd_data_id = @client.immutable.close_writer(hnd_w, hnd_cipher) name = @client.data_id.serialize(hnd_data_id) @client.immutable.drop_writer_handle(hnd_w) @client.data_id.drop_handle(hnd_data_id) # release cipher handler @client.cipher.drop_handle(hnd_cipher) Base64.encode64(name).chomp end |
#create_from_file(path) ⇒ Object
helper
1150 1151 1152 |
# File 'lib/safenet.rb', line 1150 def create_from_file(path) @client.immutable.create(IO.binread(path)) end |
#drop_reader_handle(handle_id) ⇒ Object
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 |
# File 'lib/safenet.rb', line 1102 def drop_reader_handle(handle_id) # entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/immutable-data/reader/#{handle_id}" # api call uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) req = Net::HTTP::Delete.new(uri.path, { 'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}" }) res = http.request(req) res.code == "200" ? true : JSON.parse(res.body) end |
#drop_writer_handle(handle_id) ⇒ Object
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 |
# File 'lib/safenet.rb', line 1116 def drop_writer_handle(handle_id) # entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/immutable-data/writer/#{handle_id}" # api call uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) req = Net::HTTP::Delete.new(uri.path, { 'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}" }) res = http.request(req) res.code == "200" ? true : JSON.parse(res.body) end |
#dump(name, path) ⇒ Object
helper
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 |
# File 'lib/safenet.rb', line 1155 def dump(name, path) contents = nil File.open(path, "wb") do |file| contents = @client.immutable.read(name) file.write(contents) end contents.is_a?(Hash) ? contents : true end |
#get_reader_handle(handle_id) ⇒ Object
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 |
# File 'lib/safenet.rb', line 1026 def get_reader_handle(handle_id) # entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/immutable-data/reader/#{handle_id}" # api call uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) req = Net::HTTP::Get.new(uri.path, { 'Authorization' => "Bearer #{@client.key_helper.get_token()}" }) res = http.request(req) res.code == "200" ? JSON.parse(res.body)["handleId"] : JSON.parse(res.body) end |
#get_writer_handle ⇒ Object
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 |
# File 'lib/safenet.rb', line 1040 def get_writer_handle() # entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/immutable-data/writer" # api call uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) req = Net::HTTP::Get.new(uri.path, { 'Authorization' => "Bearer #{@client.key_helper.get_token()}" }) res = http.request(req) res.code == "200" ? JSON.parse(res.body)["handleId"] : JSON.parse(res.body) end |
#read(name, chunk_pos = nil, max_chunk_size = 1_000_000) ⇒ Object
helper
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 |
# File 'lib/safenet.rb', line 1167 def read(name, chunk_pos = nil, max_chunk_size = 1_000_000) name = Base64.decode64(name) hnd_data_id = @client.data_id.deserialize(name) hnd_r = @client.immutable.get_reader_handle(hnd_data_id) contents = if chunk_pos @client.immutable.read_data(hnd_r, "bytes=#{chunk_pos}-#{chunk_pos+max_chunk_size}") else @client.immutable.read_data(hnd_r) end @client.immutable.drop_reader_handle(hnd_r) @client.data_id.drop_handle(hnd_data_id) contents end |
#read_data(handle_id, range = nil) ⇒ Object
eg range: “bytes=0-1000”
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 |
# File 'lib/safenet.rb', line 1055 def read_data(handle_id, range = nil) # entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/immutable-data/#{handle_id}" # api call uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) header = { 'Authorization' => "Bearer #{@client.key_helper.get_token()}", 'Content-Type': 'text/plain' } header['Range'] = range if range req = Net::HTTP::Get.new(uri.path, header) res = http.request(req) (res.code == "200" || res.code == "206") ? res.body : JSON.parse(res.body) end |
#write_data(handle_id, contents) ⇒ Object
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 |
# File 'lib/safenet.rb', line 1069 def write_data(handle_id, contents) # Entry point url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/immutable-data/#{handle_id}" # API call uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) headers = { 'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}", 'Content-Type': 'text/plain' } headers["Content-Length"] = contents.size.to_s req = Net::HTTP::Post.new(uri.path, headers) req.body = contents res = http.request(req) res.code == "200" ? true : JSON.parse(res.body) end |