Class: SafeNet::SD

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

Instance Method Summary collapse

Constructor Details

#initialize(client_obj) ⇒ SD

Returns a new instance of SD.



837
838
839
# File 'lib/safenet.rb', line 837

def initialize(client_obj)
  @client = client_obj
end

Instance Method Details

#create(name, contents, type = 500) ⇒ Object



964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
# File 'lib/safenet.rb', line 964

def create(name, contents, type = 500)
  name = name.is_a?(String) ? SafeNet::s2b(name) : name

  # plain (not encrypted)
  hnd_cipher = @client.cipher.get_handle

  # create
  hnd = @client.sd.create_sd(name, type, hnd_cipher, contents)
  res = @client.sd.put(hnd) # saves on the network
  @client.sd.drop_handle(hnd) # release handler

  # release cipher handler
  @client.cipher.drop_handle(hnd_cipher)

  res
end

#create_sd(name, type_tag = 500, hnd_cipher_opts = nil, data = nil, version = 0) ⇒ Object



841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'lib/safenet.rb', line 841

def create_sd(name, type_tag = 500, hnd_cipher_opts = nil, data = nil, version = 0)
  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/structured-data"

  # Payload
  payload = {
    name: name,
    typeTag: type_tag,
    cipherOpts: hnd_cipher_opts,
    data: Base64.encode64(data),
    version: version
  }

  # API call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Post.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}",
    'Content-Type' => 'application/json'
  })
  req.body = payload.to_json
  res = http.request(req)
  res.code == "200" ? JSON.parse(res.body)['handleId'] : JSON.parse(res.body)
end

#drop_handle(handle_id) ⇒ Object



952
953
954
955
956
957
958
959
960
961
962
# File 'lib/safenet.rb', line 952

def drop_handle(handle_id)
  # entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/structured-data/handle/#{handle_id}"

  # api call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Delete.new(uri.path)
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(res.body)
end

#get_data_id_handle(handle_id) ⇒ Object



909
910
911
912
913
914
915
916
917
918
919
920
921
# File 'lib/safenet.rb', line 909

def get_data_id_handle(handle_id)
  # entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/structured-data/data-id/#{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)
  JSON.parse(res.body)
end

#get_handle(data_id_handle) ⇒ Object



895
896
897
898
899
900
901
902
903
904
905
906
907
# File 'lib/safenet.rb', line 895

def get_handle(data_id_handle)
  # entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/structured-data/handle/#{data_id_handle}"

  # 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)
  JSON.parse(res.body)
end

#get_metadata(handle_id) ⇒ Object



923
924
925
926
927
928
929
930
931
932
933
934
935
# File 'lib/safenet.rb', line 923

def (handle_id)
  # entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/structured-data/metadata/#{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)
  JSON.parse(res.body)
end

#post(handle_id) ⇒ Object



881
882
883
884
885
886
887
888
889
890
891
892
893
# File 'lib/safenet.rb', line 881

def post(handle_id)
  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/structured-data/#{handle_id}"

  # API call
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  req = Net::HTTP::Post.new(uri.path, {
    'Authorization' => "Bearer #{@client.key_helper.get_valid_token()}"
  })
  res = http.request(req)
  res.code == "200" ? true : JSON.parse(res.body)
end

#put(handle_id) ⇒ Object



867
868
869
870
871
872
873
874
875
876
877
878
879
# File 'lib/safenet.rb', line 867

def put(handle_id)
  # Entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/structured-data/#{handle_id}"

  # 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" ? true : JSON.parse(res.body)
end

#read(name, type = 500) ⇒ Object



981
982
983
984
985
986
987
988
989
990
991
# File 'lib/safenet.rb', line 981

def read(name, type = 500)
  name = name.is_a?(String) ? SafeNet::s2b(name) : name

  hnd_sd_data_id = @client.data_id.get_data_id_sd(name)
  hnd_sd = @client.sd.get_handle(hnd_sd_data_id)['handleId']
  contents = @client.sd.read_data(hnd_sd)
  @client.sd.drop_handle(hnd_sd)
  @client.data_id.drop_handle(hnd_sd_data_id)

  contents
end

#read_data(handle_id, version = nil) ⇒ Object



937
938
939
940
941
942
943
944
945
946
947
948
949
950
# File 'lib/safenet.rb', line 937

def read_data(handle_id, version = nil)
  # entry point
  url = "#{@client.app_info[:launcher_server]}#{API_VERSION}/structured-data/#{handle_id}"
  url = "#{url}/#{version}?" if ! version.nil?

  # 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" ? res.body : JSON.parse(res.body)
end

#read_or_create(name, contents = '', type = 500) ⇒ Object



1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'lib/safenet.rb', line 1010

def read_or_create(name, contents = '', type = 500)
  sd = @client.sd.read(name)
  if sd.is_a?(Hash) # doesn't exist
    sd = contents
    @client.sd.create(name, sd, type)
  end

  sd
end

#update(name, contents, type = 500) ⇒ Object



993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
# File 'lib/safenet.rb', line 993

def update(name, contents, type = 500)
  name = name.is_a?(String) ? SafeNet::s2b(name) : name

  # plain (not encrypted)
  hnd_cipher = @client.cipher.get_handle

  # create
  hnd = @client.sd.create_sd(name, type, hnd_cipher, contents)
  res = @client.sd.post(hnd) # saves on the network
  @client.sd.drop_handle(hnd) # release handler

  # release cipher handler
  @client.cipher.drop_handle(hnd_cipher)

  res
end