Class: SafeNet::Immutable

Inherits:
Object
  • Object
show all
Defined in:
lib/safenet.rb

Instance Method Summary collapse

Constructor Details

#initialize(client_obj) ⇒ Immutable

Returns a new instance of Immutable.



1062
1063
1064
# File 'lib/safenet.rb', line 1062

def initialize(client_obj)
  @client = client_obj
end

Instance Method Details

#close_writer(handle_id, cipher_opts_handle) ⇒ Object



1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
# File 'lib/safenet.rb', line 1127

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



1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
# File 'lib/safenet.rb', line 1171

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



1190
1191
1192
# File 'lib/safenet.rb', line 1190

def create_from_file(path)
  @client.immutable.create(IO.binread(path))
end

#drop_reader_handle(handle_id) ⇒ Object



1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
# File 'lib/safenet.rb', line 1142

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



1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
# File 'lib/safenet.rb', line 1156

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



1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
# File 'lib/safenet.rb', line 1195

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



1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
# File 'lib/safenet.rb', line 1066

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_handleObject



1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
# File 'lib/safenet.rb', line 1080

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



1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
# File 'lib/safenet.rb', line 1207

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”



1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
# File 'lib/safenet.rb', line 1095

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



1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
# File 'lib/safenet.rb', line 1109

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