Class: Shark::RSpec::FakeAssetService::ObjectCache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/shark/rspec/fake_asset_service/object_cache.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObjectCache

Returns a new instance of ObjectCache.



9
10
11
12
# File 'lib/shark/rspec/fake_asset_service/object_cache.rb', line 9

def initialize
  @objects = {}
  @blobs = {}
end

Class Method Details

.clearObject



18
19
20
# File 'lib/shark/rspec/fake_asset_service/object_cache.rb', line 18

def self.clear
  instance.clear
end

Instance Method Details

#add(payload_data) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/shark/rspec/fake_asset_service/object_cache.rb', line 22

def add(payload_data)
  id = payload_data.delete('id') || SecureRandom.uuid
  base_uri = "#{host}/assets"
  public_id = PublicId.encode_id(id)

  @objects[id] = {
    'id' => id,
    'attributes' => payload_data,
    'links' => {
      'download' => "#{base_uri}/public/#{public_id}",
      'upload' => "#{base_uri}/#{id}/upload",
      'show' => "#{base_uri}/#{id}",
      'self' => "#{base_uri}/#{id}"
    }
  }
end

#add_blob(id, blob) ⇒ Object



39
40
41
# File 'lib/shark/rspec/fake_asset_service/object_cache.rb', line 39

def add_blob(id, blob)
  @blobs[id] = blob
end

#clearObject



43
44
45
# File 'lib/shark/rspec/fake_asset_service/object_cache.rb', line 43

def clear
  @objects = {}
end

#find(id) ⇒ Object



47
48
49
# File 'lib/shark/rspec/fake_asset_service/object_cache.rb', line 47

def find(id)
  @objects[id]
end

#find_blob(id) ⇒ Object



51
52
53
# File 'lib/shark/rspec/fake_asset_service/object_cache.rb', line 51

def find_blob(id)
  @blobs[id]
end

#hostObject



14
15
16
# File 'lib/shark/rspec/fake_asset_service/object_cache.rb', line 14

def host
  Shark.configuration.asset_service.site
end

#objectsObject



63
64
65
# File 'lib/shark/rspec/fake_asset_service/object_cache.rb', line 63

def objects
  @objects.values
end

#objects=(new_objects) ⇒ Object



67
68
69
70
71
72
# File 'lib/shark/rspec/fake_asset_service/object_cache.rb', line 67

def objects=(new_objects)
  @objects = new_objects.map do |new_object|
    object_id = new_object['id']
    [object_id, new_object]
  end.to_h
end

#remove(id) ⇒ Object



55
56
57
# File 'lib/shark/rspec/fake_asset_service/object_cache.rb', line 55

def remove(id)
  @objects.delete(id)
end

#remove_blob(id) ⇒ Object



59
60
61
# File 'lib/shark/rspec/fake_asset_service/object_cache.rb', line 59

def remove_blob(id)
  @blobs.delete(id)
end