Class: FlapjackConfigurator::FlapjackObjectBase

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

Overview

Baseline class representing a Flapjack object

Direct Known Subclasses

FlapjackContact, FlapjackSubObjectBase

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(my_id, current_config, getter_method, create_method, update_method, delete_method, logger, log_name) ⇒ FlapjackObjectBase

Returns a new instance of FlapjackObjectBase.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/flapjack_configurator/flapjack_object_base.rb', line 8

def initialize(my_id, current_config, getter_method, create_method, update_method, delete_method, logger, log_name)
  @config = {}
  @logger = logger
  @log_name = log_name # A user friendly name for log entries

  @getter_method = getter_method
  @create_method = create_method
  @update_method = update_method
  @delete_method = delete_method

  # Load the object from the API if needed
  # The current config from Flapjack is passable to avoid polling the API for each individual contact
  if my_id
    @config[:id] = my_id
    current_config = _load_from_api(my_id) unless current_config
  end

  if current_config
    @config.merge! current_config
    @obj_exists = true
  else
    @obj_exists = false
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/flapjack_configurator/flapjack_object_base.rb', line 6

def config
  @config
end

#obj_existsObject (readonly)

Returns the value of attribute obj_exists.



6
7
8
# File 'lib/flapjack_configurator/flapjack_object_base.rb', line 6

def obj_exists
  @obj_exists
end

Instance Method Details

#_load_from_api(my_id) ⇒ Object

Load the config from the API



34
35
36
37
38
39
40
# File 'lib/flapjack_configurator/flapjack_object_base.rb', line 34

def _load_from_api(my_id)
  api_data = @getter_method.call(my_id)
  return nil unless api_data

  fail "Unexpected number of responses for #{@log_name} #{my_id}" unless api_data.length == 1
  return api_data[0]
end

#_reload_configObject



47
48
49
50
51
52
53
54
# File 'lib/flapjack_configurator/flapjack_object_base.rb', line 47

def _reload_config
  my_id = id
  api_obj = @getter_method.call(my_id)
  fail "Config reload failed for config ID #{my_id}: not found" unless api_obj
  @config = api_obj[0]
  fail "Config reload failed for config ID #{my_id}: parse error" unless @config
  @obj_exists = true
end

#_update(config) ⇒ Object

Update the object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/flapjack_configurator/flapjack_object_base.rb', line 59

def _update(config)
  fail("Object #{id} doesn't exist") unless @obj_exists
  change_list = {}
  config.each do |k, v|
    k_sym = k.to_sym
    if @config[k_sym] != v
      change_list[k_sym] = v
    end
  end

  return false if change_list.empty?

  @logger.info("Updating #{@log_name} #{id}")
  @logger.debug("#{@log_name} #{id} changes: #{change_list}")
  fail "Failed to update #{id}" unless @update_method.call(id, change_list)
  _reload_config
  return true
end

#deleteObject

Delete the object



79
80
81
82
83
84
# File 'lib/flapjack_configurator/flapjack_object_base.rb', line 79

def delete
  fail("Object #{id} doesn't exist") unless @obj_exists
  @logger.info("Deleting #{@log_name} #{id}")
  fail "Failed to delete #{id}" unless @delete_method.call(id)
  @obj_exists = false
end

#idObject

Simple helper to return the ID



43
44
45
# File 'lib/flapjack_configurator/flapjack_object_base.rb', line 43

def id
  return @config[:id]
end