Class: MailerLite::Campaigns
- Inherits:
-
Object
- Object
- MailerLite::Campaigns
- Defined in:
- lib/mailerlite/campaigns/campaigns.rb
Overview
This is a class for manipulating the Campaigns from MailerLite API.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#activity(campaign_id:, filter: nil, page: nil, limit: nil, sort: nil) ⇒ HTTP::Response
activity the subscriber activity for specified campaign.
-
#cancel(campaign_id) ⇒ HTTP::Response
Cancels the specified campaign.
-
#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.
-
#delete(campaign_id) ⇒ HTTP::Response
Deletes the specified campaign.
-
#fetch(campaign_id) ⇒ HTTP::Response
Returns the details of the specified Campaigns.
-
#get(filter:, limit: nil, page: nil) ⇒ HTTP::Response
Returns a list of Campaigns that match the specified filter criteria.
-
#initialize(client: MailerLite::Client.new) ⇒ Campaigns
constructor
Inits the ‘Campaigns` class with the specified `client`.
-
#languages ⇒ HTTP::Response
Get a list of all campaign languages available.
-
#schedule(campaign_id:, delivery:, schedule: nil, resend: nil) ⇒ HTTP::Response
Schedules the specified campaign.
-
#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.
Constructor Details
#initialize(client: MailerLite::Client.new) ⇒ Campaigns
Inits the ‘Campaigns` class with the specified `client`.
11 12 13 |
# File 'lib/mailerlite/campaigns/campaigns.rb', line 11 def initialize(client: MailerLite::Client.new) @client = client end |
Instance Attribute Details
#client ⇒ Object (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
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.
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.
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.
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
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
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 |
#languages ⇒ HTTP::Response
Get a list of all campaign languages available
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.
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.
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 |