Class: MailerLite::Campaigns

Inherits:
Object
  • Object
show all
Defined in:
lib/mailerlite/campaigns/campaigns.rb

Overview

This is a class for manipulating the Campaigns from MailerLite API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: MailerLite::Client.new) ⇒ Campaigns

Inits the ‘Campaigns` class with the specified `client`.

Parameters:

  • client (MailerLite::Client) (defaults to: MailerLite::Client.new)

    the ‘Client` instance to use



11
12
13
# File 'lib/mailerlite/campaigns/campaigns.rb', line 11

def initialize(client: MailerLite::Client.new)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/mailerlite/campaigns/campaigns.rb', line 6

def client
  @client
end

Instance Method Details

#activity(campaign_id:, filter: nil, page: nil, limit: nil, sort: nil) ⇒ HTTP::Response

activity the subscriber activity for specified campaign

Parameters:

Returns:

  • (HTTP::Response)

    the response from the API



198
199
200
201
202
203
204
205
206
207
# File 'lib/mailerlite/campaigns/campaigns.rb', line 198

def activity(campaign_id:, filter: nil, page: nil, limit: nil, sort: nil)
  params = {}
  params['filter'] = {} if filter
  params['filter']['type'] = filter[:type] if filter.key?(:type)
  params['filter']['search'] = filter[:search] if filter.key?(:search)
  params['page'] = page if page
  params['limit'] = limit if limit
  params['sort'] = sort if sort
  client.http.post("#{MAILERLITE_API_URL}/campaigns/#{campaign_id}/reports/subscriber-activity", body: params.compact.to_json)
end

#cancel(campaign_id) ⇒ HTTP::Response

Cancels the specified campaign.

Parameters:

  • campaign_id (String)

    the ID of the campaign to delete

Returns:

  • (HTTP::Response)

    the response from the API



178
179
180
# File 'lib/mailerlite/campaigns/campaigns.rb', line 178

def cancel(campaign_id)
  client.http.post("#{MAILERLITE_API_URL}/campaigns/#{campaign_id}/cancel")
end

#create(name:, type:, emails:, language_id: nil, groups: nil, segments: nil, ab_settings: nil, resend_settings: nil) ⇒ HTTP::Response

Creates a new campaign with the specified details.

Parameters:

  • name (String)

    Maximum string length of 255 characters

  • language_id (integer) (defaults to: nil)

    Used to define the language in the unsubscribe template. Must be a valid language id. Defaults to english

  • type (String)

    Must be one of the following: regular, ab, resend. Type resend is only available for accounts on growing or advanced plans

  • emails (Array)

    Must contain 1 email object item

  • groups (Array) (defaults to: nil)

    Must contain valid group ids belonging to the account

  • segments (Array) (defaults to: nil)

    Must contain valid segment ids belonging to the account. If both groups and segments are provided, only segments are used

  • ab_settings (Array<Array>) (defaults to: nil)

    only if type is ab - All items of the array are required

  • resend_settings (:test_type, :select_winner_by, :b_value) (defaults to: nil)

Returns:

  • (HTTP::Response)

    the response from the API



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mailerlite/campaigns/campaigns.rb', line 43

def create(name:, type:, emails:, language_id: nil, groups: nil, segments: nil, ab_settings: nil, resend_settings: nil)
  params = { 'name' => name }
  params['type'] = type
  params['emails'] = emails
  params['language_id'] = language_id if language_id
  params['groups'] = groups if groups
  params['segments'] = segments if segments
  case type
  when 'ab'
    params['ab_settings'] = {
      test_type: ab_settings[:test_type],
      select_winner_by: ab_settings[:select_winner_by],
      after_time_amount: ab_settings[:after_time_amount],
      after_time_unit: ab_settings[:after_time_unit],
      test_split: ab_settings[:test_split]
    }
    case ab_settings[:test_type]
    when 'subject'
      params['ab_settings']['b_value'] = {
        subject: ab_settings[:b_value][:subject]
      }

    when 'sender'
      params['ab_settings']['b_value'] = {
        from_name: ab_settings[:b_value][:from_name],
        from: ab_settings[:b_value][:from]
      }
    end
  when 'resend'
    params['resend_settings'] = {
      test_type: resend_settings[:test_type],
      select_winner_by: resend_settings[:select_winner_by],
      b_value: {
        subject: resend_settings[:b_value][:subject]
      }
    }
  end

  client.http.post("#{MAILERLITE_API_URL}/campaigns", body: params.compact.to_json)
end

#delete(campaign_id) ⇒ HTTP::Response

Deletes the specified campaign.

Parameters:

  • campaign_id (String)

    the ID of the campaign to delete

Returns:

  • (HTTP::Response)

    the response from the API



186
187
188
# File 'lib/mailerlite/campaigns/campaigns.rb', line 186

def delete(campaign_id)
  client.http.delete("#{MAILERLITE_API_URL}/campaigns/#{campaign_id}")
end

#fetch(campaign_id) ⇒ HTTP::Response

Returns the details of the specified Campaigns

Parameters:

  • campaign_id (String)

    the ID of the campaign to fetch

Returns:

  • (HTTP::Response)

    the response from the API



170
171
172
# File 'lib/mailerlite/campaigns/campaigns.rb', line 170

def fetch(campaign_id)
  client.http.get("#{MAILERLITE_API_URL}/campaigns/#{campaign_id}")
end

#get(filter:, limit: nil, page: nil) ⇒ HTTP::Response

Returns a list of Campaigns that match the specified filter criteria.

The filter type of the Campaigns to include in the results, must be one of [regular, ab, resend, rss], Defaults to all

Parameters:

  • filter (:status, :type)
    String

    the status of the Campaigns to include in the results, must be one of [sent, draft, ready], Defaults to ready.

  • limit (Integer) (defaults to: nil)

    the maximum number of Campaigns to return

  • page (Integer) (defaults to: nil)

    the page number of the results to return

Returns:

  • (HTTP::Response)

    the response from the API



22
23
24
25
26
27
28
29
30
# File 'lib/mailerlite/campaigns/campaigns.rb', line 22

def get(filter:, limit: nil, page: nil)
  params = { 'filter[status]' => filter[:status] }
  params['filter[type]'] = filter[:type] if filter.key?(:type)
  params['limit'] = limit if limit
  params['page'] = page if page
  uri = URI("#{MAILERLITE_API_URL}/campaigns")
  uri.query = URI.encode_www_form(params.compact)
  client.http.get(uri)
end

#languagesHTTP::Response

Get a list of all campaign languages available

Returns:

  • (HTTP::Response)

    the response from the API



212
213
214
# File 'lib/mailerlite/campaigns/campaigns.rb', line 212

def languages
  client.http.get("#{MAILERLITE_API_URL}/campaigns/languages")
end

#schedule(campaign_id:, delivery:, schedule: nil, resend: nil) ⇒ HTTP::Response

Schedules the specified campaign.

Parameters:

  • campaign_id (Integer)

    the ID of the campaign to schedule

  • delivery (String)

    yes unless campaign type is rss Must be one of the following: instant, scheduled, timezone_based

  • schedule (:date, :hours, :minutes, :timezone_id) (defaults to: nil)

    [Array] campaign schedule

  • resend (:delivery, :date, :hours, :minutes, :timezone_id) (defaults to: nil)

Returns:

  • (HTTP::Response)

    the response from the API



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/mailerlite/campaigns/campaigns.rb', line 146

def schedule(campaign_id:, delivery:, schedule: nil, resend: nil)
  params = {}
  params['delivery'] = delivery if delivery
  if %w[scheduled timezone_based].include?(delivery) && schedule
    params['schedule'] = {}
    params['schedule']['date'] = schedule[:date] if (delivery == 'scheduled') && schedule.key?(:date)
    params['schedule']['hours'] = schedule[:hours] if schedule.key?(:hours)
    params['schedule']['minutes'] = schedule[:minutes] if schedule.key?(:minutes)
    params['schedule']['timezone_id'] = schedule[:timezone_id] if schedule.key?(:timezone_id)
  end
  params['resend'] = {} if resend
  params['resend']['delivery'] = resend[:delivery] if resend&.key?(:delivery)
  params['resend']['date'] = resend[:date] if resend&.key?(:date)
  params['resend']['hours'] = resend[:hours] if resend&.key?(:hours)
  params['resend']['minutes'] = resend[:minutes] if resend&.key?(:minutes)
  params['resend']['timezone_id'] = resend[:timezone_id] if resend&.key?(:timezone_id)

  client.http.post("#{MAILERLITE_API_URL}/campaigns/#{campaign_id}/schedule", body: params.compact.to_json)
end

#update(campaign_id:, name:, type:, emails:, language_id: nil, groups: nil, segments: nil, ab_settings: nil, resend_settings: nil) ⇒ HTTP::Response

Update a new campaign with the specified details.

Parameters:

  • campaign_id (Integer)

    the ID of the campaign to update

  • name (String)

    Maximum string length of 255 characters

  • language_id (integer) (defaults to: nil)

    Used to define the language in the unsubscribe template. Must be a valid language id. Defaults to english

  • type (String)

    Must be one of the following: regular, ab, resend. Type resend is only available for accounts on growing or advanced plans

  • emails (Array<Array>)

    Must contain 1 email object item

  • groups (Array) (defaults to: nil)

    Must contain valid group ids belonging to the account

  • segments (Array) (defaults to: nil)

    Must contain valid segment ids belonging to the account. If both groups and segments are provided, only segments are used

  • ab_settings (Array<Array>) (defaults to: nil)

    only if type is ab - All items of the array are required

  • resend_settings (:test_type, :select_winner_by, :b_value) (defaults to: nil)

Returns:

  • (HTTP::Response)

    the response from the API



96
97
98
99
100
101
102
103
104
105
106
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
# File 'lib/mailerlite/campaigns/campaigns.rb', line 96

def update(campaign_id:, name:, type:, emails:, language_id: nil, groups: nil, segments: nil, ab_settings: nil, resend_settings: nil)
  params = { 'name' => name }
  params['emails'] = emails
  params['language_id'] = language_id if language_id
  params['groups'] = groups if groups
  params['segments'] = segments if segments
  case type
  when 'ab'
    params['ab_settings'] = {
      test_type: ab_settings[:test_type],
      select_winner_by: ab_settings[:select_winner_by],
      after_time_amount: ab_settings[:after_time_amount],
      after_time_unit: ab_settings[:after_time_unit],
      test_split: ab_settings[:test_split]
    }
    case ab_settings[:test_type]
    when 'subject'
      params['ab_settings']['b_value'] = {
        subject: ab_settings[:b_value][:subject]
      }

    when 'sender'
      params['ab_settings']['b_value'] = {
        from_name: ab_settings[:b_value][:from_name],
        from: ab_settings[:b_value][:from]
      }

    end
  when 'resend'
    params['resend_settings'] = {
      test_type: resend_settings[:test_type],
      select_winner_by: resend_settings[:select_winner_by],
      b_value: {
        subject: resend_settings[:b_value][:subject]
      }
    }

  end

  client.http.put("#{MAILERLITE_API_URL}/campaigns/#{campaign_id}", body: params.compact.to_json)
end