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.



866
867
868
# File 'lib/safenet.rb', line 866

def initialize(client_obj)
  @client = client_obj
end

Instance Method Details

#create(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 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



870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
# File 'lib/safenet.rb', line 870

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



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

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



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

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



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

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



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

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



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

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



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

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 Also known as: get



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

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



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

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



1039
1040
1041
1042
1043
1044
1045
1046
# File 'lib/safenet.rb', line 1039

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

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

create or update



1049
1050
1051
1052
1053
1054
1055
# File 'lib/safenet.rb', line 1049

def set(name, contents = '', type = 500)
  sd = @client.sd.update(name, contents)
  if sd.is_a?(Hash) && (sd["errorCode"] == -22) # doesn't exist
    sd = @client.sd.create(name, contents)
  end
  sd
end

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



1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
# File 'lib/safenet.rb', line 1022

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