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
- #all(key = nil) ⇒ Object
- #base_url ⇒ Object
- #create(json, key = 'default') ⇒ Object
- #destroy(blob_id) ⇒ Object
- #find(blob_id) ⇒ Object
- #log(msg) ⇒ Object
- #login(email, password) ⇒ Object
- #master_token ⇒ Object
- #update(blob_id, json, key = 'default') ⇒ Object
Instance Attribute Details
#current_user ⇒ Object
Returns the value of attribute current_user.
12 13 14 |
# File 'lib/blobs.rb', line 12 def current_user @current_user end |
#token ⇒ Object
Returns the value of attribute token.
13 14 15 |
# File 'lib/blobs.rb', line 13 def token @token end |
Instance Method Details
#all(key = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/blobs.rb', line 40 def all(key = nil) raise 'No access token!' unless self.token url = "#{base_url}/blobs?access_token=#{CGI::escape(self.token)}" url += "&key=#{CGI::escape(sha256(key))}" if key response = HTTParty.get(url) 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'] } end end |
#base_url ⇒ Object
15 16 17 |
# File 'lib/blobs.rb', line 15 def base_url ENV['BLOB_STORE_API_BASE_URL'] end |
#create(json, key = 'default') ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/blobs.rb', line 64 def create(json, key = 'default') raise 'No access token!' unless self.token raise "No json hash!" unless json raise "No key!" unless key response = HTTParty.post("#{base_url}/blobs?access_token=#{self.token}", { body: { json: encrypt(json.to_json), key: sha256(key) } }) log "Blob created: #{response.parsed_response.inspect}" response.parsed_response end |
#destroy(blob_id) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/blobs.rb', line 84 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}?access_token=#{self.token}") log "Blob destroyed: #{response.parsed_response.inspect}" response.parsed_response end |
#find(blob_id) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/blobs.rb', line 56 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}?access_token=#{self.token}") log "Blob: #{response.parsed_response.inspect}" response.parsed_response ? JSON.parse(decrypt(response.parsed_response['json'])) : nil end |
#log(msg) ⇒ Object
23 24 25 |
# File 'lib/blobs.rb', line 23 def log(msg) puts msg if DEBUG end |
#login(email, password) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/blobs.rb', line 27 def login(email, password) auth = { username: email, password: password } response = HTTParty.post("#{base_url}/auth?access_token=#{master_token}", 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'] else nil end end |
#master_token ⇒ Object
19 20 21 |
# File 'lib/blobs.rb', line 19 def master_token ENV['MASTER_ACCESS_TOKEN'] end |
#update(blob_id, json, key = 'default') ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/blobs.rb', line 74 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}?access_token=#{self.token}", { body: { json: encrypt(json.to_json), key: sha256(key) } }) log "Blob update: #{response.parsed_response.inspect}" response.parsed_response end |