Class: KSConnect::API::Plugin::Data

Inherits:
Object
  • Object
show all
Includes:
Logs
Defined in:
lib/ksconnect/api/plugin/data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logs

included

Constructor Details

#initialize(plugin_name, domain_name, opts = {}) ⇒ Data

Returns a new instance of Data.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ksconnect/api/plugin/data.rb', line 12

def initialize(plugin_name, domain_name, opts={})
  @plugin_name = plugin_name
  @domain_name = domain_name

  @type = opts[:type] || "data"
  @use_cache = opts[:use_cache].nil? ? true : !!opts[:use_cache]
  @auto_notify = !!opts[:auto_notify]

  @cache = ActiveSupport::HashWithIndifferentAccess.new if @use_cache
  @cache_uuid = nil
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/ksconnect/api/plugin/data.rb', line 10

def type
  @type
end

Instance Method Details

#[](field) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/ksconnect/api/plugin/data.rb', line 36

def [](field)
  if @use_cache
    @cache = redis.hgetall(key) if @cache.empty?
    @cache[field]
  else
    redis.hget(key, field)
  end
end

#[]=(field, value) ⇒ Object



24
25
26
27
28
# File 'lib/ksconnect/api/plugin/data.rb', line 24

def []=(field, value)
  @cache[field] = value if @use_cache
  redis.hset(key, field, value)
  redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' }.to_json) if @auto_notify
end

#delete(field) ⇒ Object



58
59
60
61
62
# File 'lib/ksconnect/api/plugin/data.rb', line 58

def delete(field)
  @cache.delete(field) if @use_cache
  redis.hdel(key, field)
  redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' }.to_json) if @auto_notify
end

#getallObject



45
46
47
48
49
50
51
# File 'lib/ksconnect/api/plugin/data.rb', line 45

def getall
  if @use_cache
    @cache = redis.hgetall(key) if @cache.empty?; @cache
  else
    redis.hgetall(key)
  end
end

#keyObject



64
65
66
67
# File 'lib/ksconnect/api/plugin/data.rb', line 64

def key
  set_data_uuid unless @cache_uuid
  "kloudsec_data:#{@cache_uuid}"
end

#reloadObject



53
54
55
56
# File 'lib/ksconnect/api/plugin/data.rb', line 53

def reload
  @cache = redis.hgetall(key) if @use_cache
  @cache
end

#setall(hash) ⇒ Object



30
31
32
33
34
# File 'lib/ksconnect/api/plugin/data.rb', line 30

def setall(hash)
  @cache.merge!(hash) if @use_cache
  redis.mapped_hmset(key, hash)
  redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' }.to_json) if @auto_notify
end