Class: SignalApi::Segment

Inherits:
SignalHttpApi show all
Defined in:
lib/signal_api/segment.rb

Overview

Create, manage, and add users to a segment.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name = nil, description = nil, segment_type = nil, account_id = nil) ⇒ Segment

Returns a new instance of Segment.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/signal_api/segment.rb', line 50

def initialize(id, name=nil, description=nil, segment_type=nil, =nil)
  @name = name
  @description = description
  @id = id
  @account_id = 

  if segment_type == "DYNAMIC"
    @segment_type = SegmentType::DYNAMIC
  elsif segment_type == "SEGMENT"
    @segment_type = SegmentType::STATIC
  end
end

Instance Attribute Details

#account_idObject (readonly)

The account_id of the segment



44
45
46
# File 'lib/signal_api/segment.rb', line 44

def 
  @account_id
end

#descriptionObject (readonly)

The description of the segment



38
39
40
# File 'lib/signal_api/segment.rb', line 38

def description
  @description
end

#idObject (readonly)

The ID of the segment



41
42
43
# File 'lib/signal_api/segment.rb', line 41

def id
  @id
end

#nameObject (readonly)

The name of the segment



35
36
37
# File 'lib/signal_api/segment.rb', line 35

def name
  @name
end

#segment_typeObject (readonly)

Type type of the segment



47
48
49
# File 'lib/signal_api/segment.rb', line 47

def segment_type
  @segment_type
end

Class Method Details

.create(name, description, segment_type) ⇒ Segment

Create a new segment on the Signal platform.

Parameters:

  • name (String)

    The name of the segment

  • description (String)

    A description of the segment

  • segment_type (SegmentType)

    The type of the segment

Returns:

  • (Segment)

    A Segment object representing the segment on the Signal platform



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/signal_api/segment.rb', line 70

def self.create(name, description, segment_type)
  if name.blank? || description.blank? || segment_type.blank?
    raise InvalidParameterException.new("name, description, and segment_type are all required")
  end

  unless [SegmentType::DYNAMIC, SegmentType::STATIC].include?(segment_type)
    raise InvalidParameterException.new("Invalid segment type")
  end

  builder = Builder::XmlMarkup.new
  body = builder.filter_group do |filter_group|
    filter_group.description(description)
    filter_group.name(name)
    filter_group.filter_group_type(segment_type)
  end

  SignalApi.logger.info "Attempting to create a segment: name => #{name}, description => \"#{description}\", type = #{segment_type}}"
  SignalApi.logger.debug "Segment data: #{body}"
  with_retries do
    response = post("/api/filter_groups/create.xml",
                    :body => body,
                    :format => :xml,
                    :headers => common_headers)

    if response.code == 200
      data = response.parsed_response['subscription_list_filter_group']
      new(data['id'], data['name'], data['description'], lookup_segment_type(data['filter_group_type_id']), data['account_id'])
    else
      handle_api_failure(response)
    end
  end
end

Instance Method Details

#add_users(segment_users) ⇒ Hash

Add mobile phone numbers to a segment.

Parameters:

  • segment_users (Array<SegmentUser>)

    An array of SegmentUsers to add to the segment

Returns:

  • (Hash)

    A hash containing some stats regarding the operation



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/signal_api/segment.rb', line 107

def add_users(segment_users)
  if segment_users.blank?
    raise InvalidParameterException.new("An array of SegmentUser objects must be provided")
  end

  builder = Builder::XmlMarkup.new
  body = builder.users(:type => :array) do |users|
    segment_users.each do |segment_user|
      users.user do |user|
        user.mobile_phone(segment_user.mobile_phone) if segment_user.mobile_phone
        user.email(segment_user.email_address) if segment_user.email_address
        user.user_data(segment_user.user_data) if segment_user.user_data
      end
    end
  end

  SignalApi.logger.info "Attempting to add users to segment #{@id}"
  self.class.with_retries do
    response = self.class.post("/api/filter_segments/#{@id}/update.xml",
                               :body => body,
                               :format => :xml,
                               :headers => self.class.common_headers)

    if response.code == 200
      data = response.parsed_response['subscription_list_segment_results']

      if data['users_not_found'] && data['users_not_found']['user_not_found']
        if data['users_not_found']['user_not_found'].respond_to?(:join)
          SignalApi.logger.warn data['users_not_found']['user_not_found'].join(", ")
        else
          SignalApi.logger.warn data['users_not_found']['user_not_found']
        end
      end

      { :total_users_processed => (data['total_users_processed'] || 0).to_i,
        :total_users_added     => (data['total_users_added'] || 0).to_i,
        :total_users_not_found => (data['total_users_not_found'] || 0).to_i,
        :total_duplicate_users => (data['total_duplicate_users'] || 0).to_i }
    else
      self.class.handle_api_failure(response)
    end
  end
end