Class: Blobs::ApiClient
- Inherits:
-
Object
- Object
- Blobs::ApiClient
- Defined in:
- lib/blobs.rb
Instance Attribute Summary collapse
-
#current_user ⇒ Object
Returns the value of attribute current_user.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #add_file(file_path, key = 'default') ⇒ Object
- #all(key = nil) ⇒ Object
- #base_url ⇒ Object
- #create(json, key = 'default', is_file = false) ⇒ Object
- #create_user(email, password) ⇒ Object
- #destroy(blob_id) ⇒ Object
- #export_key_file(key_file = "#{ENV['HOME']}/.blobs.key.enc") ⇒ Object
- #file_from_json(file_json, path = '/tmp', blob_id = nil) ⇒ Object
- #file_to_json(file_path) ⇒ Object
- #find(blob_id) ⇒ Object
- #get_file(blob_id, path = '/tmp') ⇒ Object
- #log(msg) ⇒ Object
- #login(email, password) ⇒ Object
- #master_token ⇒ Object
- #update(blob_id, json, key = 'default') ⇒ Object
- #update_file(blob_id, file_path, key = 'default') ⇒ Object
- #user_token ⇒ Object
Instance Attribute Details
#current_user ⇒ Object
Returns the value of attribute current_user.
14 15 16 |
# File 'lib/blobs.rb', line 14 def current_user @current_user end |
#token ⇒ Object
Returns the value of attribute token.
15 16 17 |
# File 'lib/blobs.rb', line 15 def token @token end |
Instance Method Details
#add_file(file_path, key = 'default') ⇒ Object
137 138 139 140 141 142 |
# File 'lib/blobs.rb', line 137 def add_file(file_path, key = 'default') raise 'No file path given!' unless file_path raise "File doesn't exist!" unless File.file?(file_path) resp = create(file_to_json(file_path), key, true) resp ? resp['id'] : nil end |
#all(key = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/blobs.rb', line 52 def all(key = nil) raise 'No access token!' unless self.token url = "#{base_url}/blobs" url += "&key=#{CGI::escape(sha256(key))}" if key response = HTTParty.get(url, { headers: headers }) log "Blobs: #{response.parsed_response.inspect}" response.parsed_response.map do |blob| { id: blob['id'], created_at: blob['createdAt'], json: (blob['json'] ? (JSON.parse(decrypt(blob['json'])) rescue nil) : nil), key: blob['key'], file: blob['file'] ? blob['file'] : false } end end |
#base_url ⇒ Object
17 18 19 |
# File 'lib/blobs.rb', line 17 def base_url ENV['BLOB_STORE_API_BASE_URL'] end |
#create(json, key = 'default', is_file = false) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/blobs.rb', line 77 def create(json, key = 'default', is_file = false) raise 'No access token!' unless self.token raise "No json hash!" unless json raise "No key!" unless key response = HTTParty.post("#{base_url}/blobs", { body: { json: encrypt(json.to_json), key: sha256(key), isFile: is_file }, headers: headers } ) log "Blob created: #{response.parsed_response.inspect}" response.parsed_response end |
#create_user(email, password) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/blobs.rb', line 43 def create_user(email, password) raise "You need to set the master key to create users!" unless master_token user = { email: email, password: password } response = HTTParty.post("#{base_url}/users", body: user, headers: { 'Authorization': "Bearer #{master_token}" }) log "#{response.parsed_response.inspect}" response.parsed_response end |
#destroy(blob_id) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/blobs.rb', line 101 def destroy(blob_id) raise 'No access token!' unless self.token raise "No blob id!" unless blob_id response = HTTParty.delete("#{base_url}/blobs/#{blob_id}", { headers: headers }) log "Blob destroyed: #{response.parsed_response.inspect}" response.parsed_response end |
#export_key_file(key_file = "#{ENV['HOME']}/.blobs.key.enc") ⇒ Object
113 114 115 116 117 |
# File 'lib/blobs.rb', line 113 def export_key_file(key_file = "#{ENV['HOME']}/.blobs.key.enc") export_file = "#{ENV['HOME']}/blobs.key-#{Time.now.to_i}" decrypt_file(key_file, export_file) export_file end |
#file_from_json(file_json, path = '/tmp', blob_id = nil) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/blobs.rb', line 125 def file_from_json(file_json, path = '/tmp', blob_id = nil) if file_json and file_json['file'] and file_json['file_name'] decoded_string = Base64.decode64(file_json['file']) blob_file_path = "#{path}/#{blob_id ? "#{blob_id}-" : ''}#{file_json['file_name']}" File.open(blob_file_path, 'wb') do |f| f.write(decoded_string) f.close end blob_file_path end end |
#file_to_json(file_path) ⇒ Object
119 120 121 122 123 |
# File 'lib/blobs.rb', line 119 def file_to_json(file_path) encoded_string = Base64.encode64(File.open(file_path, 'rb').read) { file: encoded_string, file_name: file_path.split('/').last, file_size: File.size(file_path) } end |
#find(blob_id) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/blobs.rb', line 69 def find(blob_id) raise 'No access token!' unless self.token raise "No blob id!" unless blob_id response = HTTParty.get("#{base_url}/blobs/#{blob_id}", { headers: headers }) log "Blob: #{response.parsed_response.inspect}" response.parsed_response ? JSON.parse(decrypt(response.parsed_response['json'])) : nil end |
#get_file(blob_id, path = '/tmp') ⇒ Object
150 151 152 153 154 155 |
# File 'lib/blobs.rb', line 150 def get_file(blob_id, path = '/tmp') if (blob = find(blob_id)) return file_from_json(blob, path, blob_id) if blob['file'] and blob['file_name'] end nil end |
#log(msg) ⇒ Object
25 26 27 |
# File 'lib/blobs.rb', line 25 def log(msg) puts msg if DEBUG end |
#login(email, password) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/blobs.rb', line 29 def login(email, password) auth = { username: email, password: password } response = HTTParty.post("#{base_url}/auth", basic_auth: auth) if response.code == 201 and response.parsed_response log "#{response.parsed_response.inspect}" self.current_user = response.parsed_response['user'] self.token = response.parsed_response['token'] encryption_key return self.token end nil end |
#master_token ⇒ Object
21 22 23 |
# File 'lib/blobs.rb', line 21 def master_token ENV['MASTER_KEY'] end |
#update(blob_id, json, key = 'default') ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/blobs.rb', line 91 def update(blob_id, json, key = 'default') raise 'No access token!' unless self.token raise "No blob id!" unless json raise "No json hash!" unless json response = HTTParty.put("#{base_url}/blobs/#{blob_id}", { body: { json: encrypt(json.to_json), key: sha256(key) }, headers: headers }) log "Blob update: #{response.parsed_response.inspect}" response.parsed_response end |
#update_file(blob_id, file_path, key = 'default') ⇒ Object
144 145 146 147 148 |
# File 'lib/blobs.rb', line 144 def update_file(blob_id, file_path, key = 'default') raise 'No file path given!' unless file_path raise "File doesn't exist!" unless File.file?(file_path) update(blob_id, file_to_json(file_path), key) end |
#user_token ⇒ Object
109 110 111 |
# File 'lib/blobs.rb', line 109 def user_token Base64.decode64(self.current_user['userToken']) end |