Class: Kashi::DSL::ContactGroup::Result
- Inherits:
-
Object
- Object
- Kashi::DSL::ContactGroup::Result
- 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
- #cake(sc_contact_group) ⇒ Object
- #client ⇒ Object
- #create ⇒ Object
- #create_params ⇒ Object
- #dsl_hash ⇒ Object
-
#initialize(context) ⇒ Result
constructor
A new instance of Result.
- #modify ⇒ Object
- #modify_params ⇒ Object
- #sc_hash ⇒ Object
- #to_h ⇒ Object
- #updated? ⇒ Boolean
Constructor Details
#initialize(context) ⇒ Result
Returns a new instance of Result.
15 16 17 18 |
# File 'lib/kashi/dsl/contact_group.rb', line 15 def initialize(context) @context = context @options = context. end |
Instance Method Details
#cake(sc_contact_group) ⇒ Object
47 48 49 50 51 |
# 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 ⇒ Object
81 82 83 |
# File 'lib/kashi/dsl/contact_group.rb', line 81 def client @client ||= ClientWrapper.new(@options) end |
#create ⇒ Object
39 40 41 42 43 44 45 |
# 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 ⇒ Object
24 25 26 27 28 |
# File 'lib/kashi/dsl/contact_group.rb', line 24 def create_params hash = modify_params hash.delete(:ContactID) hash end |
#dsl_hash ⇒ Object
53 54 55 |
# File 'lib/kashi/dsl/contact_group.rb', line 53 def dsl_hash Kashi::Utils.normalize_hash(to_h) end |
#modify ⇒ Object
71 72 73 74 75 76 77 78 79 |
# 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 ⇒ Object
30 31 32 33 34 35 36 37 |
# 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 ⇒ Object
57 58 59 60 61 62 63 64 65 |
# 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 ⇒ Object
20 21 22 |
# File 'lib/kashi/dsl/contact_group.rb', line 20 def to_h Hash[ATTRIBUTES.sort.map { |name| [name, public_send(name)] }] end |
#updated? ⇒ Boolean
67 68 69 |
# File 'lib/kashi/dsl/contact_group.rb', line 67 def updated? dsl_hash != sc_hash end |