Sha256: b2458cf64053bb3f33a7648b37a1d7c199adb071e37b97f8e065fcbd54afdc2c

Contents?: true

Size: 1.52 KB

Versions: 11

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module Shark
  module RSpec
    module FakeAssetService
      class ObjectCache
        include Singleton

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

        def host
          Shark.configuration.asset_service.site
        end

        def self.clear
          instance.clear
        end

        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

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

        def clear
          @objects = {}
        end

        def find(id)
          @objects[id]
        end

        def find_blob(id)
          @blobs[id]
        end

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

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

        def objects
          @objects.values
        end

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
bima-shark-sdk-3.1.1 lib/shark/rspec/fake_asset_service/object_cache.rb
bima-shark-sdk-2.5.0 lib/shark/rspec/fake_asset_service/object_cache.rb
bima-shark-sdk-3.1.0 lib/shark/rspec/fake_asset_service/object_cache.rb
bima-shark-sdk-3.0.0 lib/shark/rspec/fake_asset_service/object_cache.rb
bima-shark-sdk-2.4.4 lib/shark/rspec/fake_asset_service/object_cache.rb
bima-shark-sdk-2.4.3 lib/shark/rspec/fake_asset_service/object_cache.rb
bima-shark-sdk-2.4.2 lib/shark/rspec/fake_asset_service/object_cache.rb
bima-shark-sdk-2.4.1 lib/shark/rspec/fake_asset_service/object_cache.rb
bima-shark-sdk-2.4.0 lib/shark/rspec/fake_asset_service/object_cache.rb
bima-shark-sdk-2.3.1 lib/shark/rspec/fake_asset_service/object_cache.rb
bima-shark-sdk-2.3.0 lib/shark/rspec/fake_asset_service/object_cache.rb