Class: Kashi::DSL::ContactGroup::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/kashi/dsl/contact_group.rb

Constant Summary collapse

PARAMS =
i/ContactID GroupName DesktopAlert Email Boxcar Pushover PingURL Mobile/
ATTRIBUTES =
i/contact_id group_name desktop_alert email boxcar pushover ping_url mobile/

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Result

Returns a new instance of Result.



14
15
16
17
# File 'lib/kashi/dsl/contact_group.rb', line 14

def initialize(context)
  @context = context
  @options = context.options
end

Instance Method Details

#cake(sc_contact_group) ⇒ Object



46
47
48
49
50
# File 'lib/kashi/dsl/contact_group.rb', line 46

def cake(sc_contact_group)
  @sc_contact_group = sc_contact_group
  self.contact_id = sc_contact_group['ContactID']
  self
end

#clientObject



80
81
82
# File 'lib/kashi/dsl/contact_group.rb', line 80

def client
  @client ||= ClientWrapper.new(@options)
end

#createObject



38
39
40
41
42
43
44
# File 'lib/kashi/dsl/contact_group.rb', line 38

def create
  Kashi.logger.info("Create ContactGroup `#{group_name}`")
  Kashi.logger.debug(create_params)
  return { 'Success' => true } if @options[:dry_run]

  client.contactgroups_update(create_params)
end

#create_paramsObject



23
24
25
26
27
# File 'lib/kashi/dsl/contact_group.rb', line 23

def create_params
  hash = modify_params
  hash.delete(:ContactID)
  hash
end

#dsl_hashObject



52
53
54
# File 'lib/kashi/dsl/contact_group.rb', line 52

def dsl_hash
  Kashi::Utils.normalize_hash(to_h)
end

#modifyObject



70
71
72
73
74
75
76
77
78
# File 'lib/kashi/dsl/contact_group.rb', line 70

def modify
  return unless updated?
  Kashi.logger.info("Modify ContactGroup `#{group_name}` #{contact_id}")
  Kashi.logger.info("<diff>\n#{Kashi::Utils.diff(sc_hash, dsl_hash, color: @options[:color])}")
  Kashi.logger.debug(modify_params)
  return if @options[:dry_run]

  client.contactgroups_update(modify_params)
end

#modify_paramsObject



29
30
31
32
33
34
35
36
# File 'lib/kashi/dsl/contact_group.rb', line 29

def modify_params
  hash = to_h.select { |k, _| ATTRIBUTES.include?(k) }
  ATTRIBUTES.zip(PARAMS).each do |from, to|
    hash[to] = hash.delete(from)
  end
  hash[:Email] = Array(hash[:Email]).join(',')
  hash
end

#sc_hashObject



56
57
58
59
60
61
62
63
64
# File 'lib/kashi/dsl/contact_group.rb', line 56

def sc_hash
  hash = @sc_contact_group.to_h.keys.each_with_object({}) do |k, h|
    h[k.to_s.to_snake.to_sym] = @sc_contact_group.to_h[k]
  end
  { emails: :email, mobiles: :mobile }.each do |k, v|
    hash[v] = hash.delete(k)
  end
  Kashi::Utils.normalize_hash(hash)
end

#to_hObject



19
20
21
# File 'lib/kashi/dsl/contact_group.rb', line 19

def to_h
  Hash[ATTRIBUTES.sort.map { |name| [name, public_send(name)] }]
end

#updated?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/kashi/dsl/contact_group.rb', line 66

def updated?
  dsl_hash != sc_hash
end