ruby-safenet 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -25
  3. data/lib/safenet.rb +38 -10
  4. data/lib/safenet/version.rb +1 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 974008fc8230e6155b425d997bb8a305dcd8537e
4
- data.tar.gz: 1183766c5674d6f23291a8e2a4259b68a120c10e
3
+ metadata.gz: 65eb893168713c7f73eb4215b249373a0bbf2b6a
4
+ data.tar.gz: 8c3409c023698747ec846be0f3af97ecca26ce84
5
5
  SHA512:
6
- metadata.gz: 5eccd278929da2180b8e7da31eb8efe53cf6711eef128dcd7ac31ff167c9f9691798ea1dc3d4cc8cd258c1a8e0e311e3afe27142126a70dbbfbdcc48c50edccf
7
- data.tar.gz: a3910ea35830bdde568de7d1f05ef67d40e6ab26c5607f6013f76fab5f860cb824dd7e07e7032491e1bd80deba9910979b1367211f6addfdf12bc8b52bb8d0bd
6
+ metadata.gz: 507e67bb1b94ef540de9ce2fddd16c827c2c26050afbf9db7e98c9ed225204eed87cd17aa9a5f8670c6becfec215ed83c0b93a844b031bbce31c8d67e1194e04
7
+ data.tar.gz: 0781b391ce210bb7177a0cba85c54c89c39d62f1c28c31312d4f8e28706cbd6f2af09e3ed0a2ff656e791371c1f321afe066d30c6e142b54c1992fe64ccc6b1c
data/README.md CHANGED
@@ -20,7 +20,7 @@ safe.nfs.create_file("/mydir/index.html", "Hello world!<br>I'm a webpage :D")
20
20
  safe.dns.register_service("my-wonderful-app", "www", "/mydir")
21
21
  safe.nfs.get_file("/mydir/index.html")
22
22
 
23
- # Then, open http://www.my-wonderful-app.safenet/
23
+ # Then, open safe://www.my-wonderful-app/
24
24
  ```
25
25
 
26
26
  You can also set a more detailed App info:
@@ -54,7 +54,7 @@ end
54
54
  ## Structured Data (SD) - With helpers:
55
55
 
56
56
  ```ruby
57
- safe = SafeNet::quick_start
57
+ safe = safenet_quick
58
58
 
59
59
  safe.sd.create('my_sd', 'Hello SD!')
60
60
  puts safe.sd.read('my_sd') # Hello SD!
@@ -63,6 +63,20 @@ safe.sd.update('my_sd', 'Hello SD 2!')
63
63
  puts safe.sd.read('my_sd') # Hello SD 2!
64
64
  ```
65
65
 
66
+ ## Immutable Data - With helpers:
67
+ ```ruby
68
+ # client
69
+ safe = safenet_quick
70
+
71
+ # write / read
72
+ name = safe.immutable.create('Hello SD') # name = base64 encoded
73
+ puts safe.immutable.read(name)
74
+
75
+ # write from file
76
+ name = safe.immutable.create_from_file("#{Rails.root}/my_file.txt")
77
+ safe.immutable.dump(name, "#{Rails.root}/from_safenet.txt")
78
+ ```
79
+
66
80
  ## Structured Data (SD) - With safe primitives:
67
81
 
68
82
  ```ruby
@@ -91,19 +105,6 @@ safe.data_id.drop_handle(hnd_sd_data_id)
91
105
  puts contents # print SD contents on screen
92
106
  ```
93
107
 
94
- ## Immutable Data - With helpers:
95
- ```ruby
96
- # client
97
- safe = SafeNet::quick_start
98
-
99
- # write
100
- name = safe.immutable.write('Hello SD')
101
- puts "Immutable name:\n * Binary: #{name}\n * Hex...: #{name.unpack("H*").first}\n * Base64: #{Base64.encode64(name)}"
102
-
103
- # read
104
- puts safe.immutable.read(name)
105
- ```
106
-
107
108
  ## Immutable Data - With safe primitives:
108
109
 
109
110
  ```ruby
@@ -145,16 +146,6 @@ safe.data_id.drop_handle(hnd_data_id)
145
146
  puts contents
146
147
  ```
147
148
 
148
- <!-- id = SafeNet.s2b('my_sd')
149
- safe.sd.update(id, 500, "Hi John")
150
- safe.sd.get(id, 500)
151
- safe.sd.update(id, 500, "Hello World!") -->
152
-
153
- <!-- id = safe.immutable.create("Hello World!") # => "861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b49264b510751b1ff9e537937bc46b5d6ff4ecc8"
154
- safe.immutable.create_from_file("/home/daniel/dog.jpg")
155
- safe.immutable.get(id) # => "Hello World!" -->
156
- ```
157
-
158
149
  ## Supported methods:
159
150
  |Module|Method|Arguments|Optional|Doc|
160
151
  |------|------|---------|--------|---|
@@ -1006,6 +1006,16 @@ module SafeNet
1006
1006
 
1007
1007
  res
1008
1008
  end
1009
+
1010
+ def read_or_create(name, contents = '', type = 500)
1011
+ sd = @client.sd.read(name)
1012
+ if sd.is_a?(Hash) # doesn't exist
1013
+ sd = contents
1014
+ @client.sd.create(name, sd, type)
1015
+ end
1016
+
1017
+ sd
1018
+ end
1009
1019
  end
1010
1020
 
1011
1021
  class Immutable
@@ -1118,7 +1128,7 @@ module SafeNet
1118
1128
  end
1119
1129
 
1120
1130
  # helper
1121
- def write(contents)
1131
+ def create(contents)
1122
1132
  # plain (not encrypted)
1123
1133
  hnd_cipher = @client.cipher.get_handle
1124
1134
 
@@ -1133,11 +1143,30 @@ module SafeNet
1133
1143
  # release cipher handler
1134
1144
  @client.cipher.drop_handle(hnd_cipher)
1135
1145
 
1136
- name
1146
+ Base64.encode64(name).chomp
1147
+ end
1148
+
1149
+ # helper
1150
+ def create_from_file(path)
1151
+ @client.immutable.create(IO.binread(path))
1152
+ end
1153
+
1154
+ # helper
1155
+ def dump(name, path)
1156
+ contents = nil
1157
+
1158
+ File.open(path, "wb") do |file|
1159
+ contents = @client.immutable.read(name)
1160
+ file.write(contents)
1161
+ end
1162
+
1163
+ contents.is_a?(Hash) ? contents : true
1137
1164
  end
1138
1165
 
1139
1166
  # helper
1140
1167
  def read(name, chunk_pos = nil, max_chunk_size = 1_000_000)
1168
+ name = Base64.decode64(name)
1169
+
1141
1170
  hnd_data_id = @client.data_id.deserialize(name)
1142
1171
  hnd_r = @client.immutable.get_reader_handle(hnd_data_id)
1143
1172
  contents = if chunk_pos
@@ -1281,14 +1310,6 @@ module SafeNet
1281
1310
  res = http.request(req)
1282
1311
  res.code == "200" ? JSON.parse(res.body)['handleId'] : JSON.parse(res.body)
1283
1312
  end
1284
-
1285
- end
1286
-
1287
- def self.quick_start(low_level_api = true, safe_drive_access = true)
1288
- permissions = []
1289
- permissions << 'LOW_LEVEL_API' if low_level_api
1290
- permissions << 'SAFE_DRIVE_ACCESS' if safe_drive_access
1291
- SafeNet::Client.new(permissions: permissions)
1292
1313
  end
1293
1314
 
1294
1315
  def self.s2b(str)
@@ -1310,3 +1331,10 @@ module SafeNet
1310
1331
  end
1311
1332
  end
1312
1333
  end
1334
+
1335
+ def safenet_quick(low_level_api = true, safe_drive_access = true)
1336
+ permissions = []
1337
+ permissions << 'LOW_LEVEL_API' if low_level_api
1338
+ permissions << 'SAFE_DRIVE_ACCESS' if safe_drive_access
1339
+ SafeNet::Client.new(permissions: permissions)
1340
+ end
@@ -1,3 +1,3 @@
1
1
  module Safenet
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-safenet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Loureiro