Class: Blobs::ApiClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_userObject

Returns the value of attribute current_user.



15
16
17
# File 'lib/blobs.rb', line 15

def current_user
  @current_user
end

#tokenObject

Returns the value of attribute token.



16
17
18
# File 'lib/blobs.rb', line 16

def token
  @token
end

Instance Method Details

#all(key = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/blobs.rb', line 43

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_urlObject



18
19
20
# File 'lib/blobs.rb', line 18

def base_url
  ENV['BLOB_STORE_API_BASE_URL']
end

#create(json, key = 'default') ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/blobs.rb', line 67

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



87
88
89
90
91
92
93
# File 'lib/blobs.rb', line 87

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



59
60
61
62
63
64
65
# File 'lib/blobs.rb', line 59

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



26
27
28
# File 'lib/blobs.rb', line 26

def log(msg)
  puts msg if DEBUG
end

#login(email, password) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/blobs.rb', line 30

def (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_tokenObject



22
23
24
# File 'lib/blobs.rb', line 22

def master_token
  ENV['MASTER_ACCESS_TOKEN']
end

#update(blob_id, json, key = 'default') ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/blobs.rb', line 77

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