class Kashi::DSL::ContactGroup::Result

Constants

ATTRIBUTES
PARAMS

Public Class Methods

new(context) click to toggle source
# File lib/kashi/dsl/contact_group.rb, line 15
def initialize(context)
  @context = context
  @options = context.options
end

Public Instance Methods

cake(sc_contact_group) click to toggle source
# File lib/kashi/dsl/contact_group.rb, line 47
def cake(sc_contact_group)
  @sc_contact_group = sc_contact_group
  self.contact_id = sc_contact_group['ContactID']
  self
end
client() click to toggle source
# File lib/kashi/dsl/contact_group.rb, line 81
def client
  @client ||= ClientWrapper.new(@options)
end
create() click to toggle source
# File lib/kashi/dsl/contact_group.rb, line 39
def create
  Kashi.logger.info("Create ContactGroup `#{group_name}`".colorize(:green))
  Kashi.logger.debug(create_params)
  return { 'Success' => true } if @options[:dry_run]

  client.contactgroups_update(create_params)
end
create_params() click to toggle source
# File lib/kashi/dsl/contact_group.rb, line 24
def create_params
  hash = modify_params
  hash.delete(:ContactID)
  hash
end
dsl_hash() click to toggle source
# File lib/kashi/dsl/contact_group.rb, line 53
def dsl_hash
  Kashi::Utils.normalize_hash(to_h)
end
modify() click to toggle source
# File lib/kashi/dsl/contact_group.rb, line 71
def modify
  return unless updated?
  Kashi.logger.info("Modify ContactGroup `#{group_name}` #{contact_id}".colorize(:blue))
  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_params() click to toggle source
# File lib/kashi/dsl/contact_group.rb, line 30
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_hash() click to toggle source
# File lib/kashi/dsl/contact_group.rb, line 57
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_h() click to toggle source
# File lib/kashi/dsl/contact_group.rb, line 20
def to_h
  Hash[ATTRIBUTES.sort.map { |name| [name, public_send(name)] }]
end
updated?() click to toggle source
# File lib/kashi/dsl/contact_group.rb, line 67
def updated?
  dsl_hash != sc_hash
end