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
1033 1034 1035 |
# File 'lib/safenet.rb', line 1033 def initialize(client_obj) @client = client_obj end |
Instance Method Details
#close_writer(handle_id, cipher_opts_handle) ⇒ Object
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 |
# File 'lib/safenet.rb', line 1098 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
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 |
# File 'lib/safenet.rb', line 1142 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
1161 1162 1163 |
# File 'lib/safenet.rb', line 1161 def create_from_file(path) @client.immutable.create(IO.binread(path)) end |
#drop_reader_handle(handle_id) ⇒ Object
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 |
# File 'lib/safenet.rb', line 1113 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
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
# File 'lib/safenet.rb', line 1127 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
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 |
# File 'lib/safenet.rb', line 1166 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
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 |
# File 'lib/safenet.rb', line 1037 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
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 |
# File 'lib/safenet.rb', line 1051 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
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 |
# File 'lib/safenet.rb', line 1178 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”
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 |
# File 'lib/safenet.rb', line 1066 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
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 |
# File 'lib/safenet.rb', line 1080 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 |