Class: SendGrid4r::CLI::Campaign::Contact::Segment

Inherits:
SgThor
  • Object
show all
Defined in:
lib/sendgrid4r/cli/campaign/contact/segment.rb

Overview

SendGrid Web API v3 Marketing Campaigns - Segment

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Segment

Returns a new instance of Segment.



8
9
10
11
12
# File 'lib/sendgrid4r/cli/campaign/contact/segment.rb', line 8

def initialize(*args)
  super
  @condition_factory = SendGrid4r::Factory::ConditionFactory.new
  @segment_factory = SendGrid4r::Factory::SegmentFactory.new
end

Instance Method Details

#createObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sendgrid4r/cli/campaign/contact/segment.rb', line 18

def create
  conditions = options[:conditions].map do |c|
    array = c.delete(' ').split(/[:,]/)
    hash = array.each_slice(2).map do |k, v|
      [k.to_sym, v.nil? ? '' : v]
    end.to_h
    @condition_factory.create(hash)
  end
  params = @segment_factory.create(
    name: options[:name],
    list_id: options[:list_id],
    conditions: conditions
  )
  puts @client.post_segment(params: params)
rescue RestClient::ExceptionWithResponse => e
  puts e.inspect
end

#deleteObject



77
78
79
80
81
# File 'lib/sendgrid4r/cli/campaign/contact/segment.rb', line 77

def delete
  puts @client.delete_segment(parameterise(options))
rescue RestClient::ExceptionWithResponse => e
  puts e.inspect
end

#getObject



45
46
47
48
49
# File 'lib/sendgrid4r/cli/campaign/contact/segment.rb', line 45

def get
  puts @client.get_segment(parameterise(options))
rescue RestClient::ExceptionWithResponse => e
  puts e.inspect
end

#listObject



37
38
39
40
41
# File 'lib/sendgrid4r/cli/campaign/contact/segment.rb', line 37

def list
  puts @client.get_segments
rescue RestClient::ExceptionWithResponse => e
  puts e.inspect
end

#recipient(action) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/sendgrid4r/cli/campaign/contact/segment.rb', line 85

def recipient(action)
  case action
  when 'list'
    puts @client.get_recipients_on_segment(parameterise(options))
  else
    puts "error: #{action} is not supported in action parameter"
  end
end

#updateObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sendgrid4r/cli/campaign/contact/segment.rb', line 55

def update
  conditions = options[:conditions].map do |c|
    array = c.delete(' ').split(/[:,]/)
    hash = array.each_slice(2).map do |k, v|
      [k.to_sym, v.nil? ? '' : v]
    end.to_h
    @condition_factory.create(hash)
  end
  params = @segment_factory.create(
    name: options[:name],
    conditions: conditions
  )
  puts @client.patch_segment(
    segment_id: options[:segment_id],
    params: params
  )
rescue RestClient::ExceptionWithResponse => e
  puts e.inspect
end