Class: Shark::RSpec::FakeContactService::ObjectCache

Inherits:
Object
  • Object
show all
Includes:
Helpers::CacheHelper, Singleton
Defined in:
lib/shark/rspec/fake_contact_service/object_cache.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::CacheHelper

#find, #included_resources

Constructor Details

#initializeObjectCache

Returns a new instance of ObjectCache.



12
13
14
# File 'lib/shark/rspec/fake_contact_service/object_cache.rb', line 12

def initialize
  @objects = []
end

Instance Attribute Details

#objectsObject

Returns the value of attribute objects.



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

def objects
  @objects
end

Class Method Details

.clearObject



16
17
18
# File 'lib/shark/rspec/fake_contact_service/object_cache.rb', line 16

def self.clear
  instance.objects = []
end

Instance Method Details

#add(object) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/shark/rspec/fake_contact_service/object_cache.rb', line 20

def add(object)
  unless object['attributes'].keys.include?('avatar_url')
    avatar_url = "https://contactservice/path/to/avatar/#{object['id']}.png"
    object['attributes']['avatar_url'] = avatar_url
  end

  objects.push(object)

  object
end

#objects_contain(type, params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/shark/rspec/fake_contact_service/object_cache.rb', line 31

def objects_contain(type, params)
  filtered_objects = []
  filter = params['filter']

  if filter['contact_id']
    filtered_objects = objects.select do |object|
      return false unless object['type'] == type

      ids = (object['attributes']['contact_ids'] || []).map(&:to_s)
      ids.include?(filter.values.first)
    end
  end
  filtered_objects
end

#search_objects(type, params) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/shark/rspec/fake_contact_service/object_cache.rb', line 46

def search_objects(type, params)
  filtered_objects = []
  filters = params['filter'] && params['filter']['filter']
  condition, filter = filters.first

  if condition.present? && filter.present?
    filtered_objects = objects.select do |object|
      return false unless object['type'] == type

      attribute_key, value = filter.first
      attribute = object['attributes'][attribute_key]

      case condition
      when 'contains_word_prefixes'
        attribute.to_s.start_with?(value)
      when 'contains_words'
        words = case value
                when Array
                  value
                when Hash
                  value.keys
                  # else
                  #   value.split(",").map(&:strip)
                end
        words.include?(attribute.to_s)
      when 'equals'
        attribute.to_s == value
      when 'is_blank'
        attribute.blank?
      when 'is_true'
        attribute == true
      else
        false
      end
    end
  end

  filtered_objects
end