Class: MailerLite::Subscribers

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Inits the ‘Subscribers` 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/subscribers/subscribers.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/subscribers/subscribers.rb', line 6

def client
  @client
end

Instance Method Details

#create(email:, fields: nil, groups: nil, status: nil, subscribed_at: nil, ip_address: nil, opted_in_at: nil, optin_ip: nil, unsubscribed_at: nil) ⇒ HTTP::Response

Creates a new subscriber with the specified details.

Parameters:

  • email (String)

    the email address of the new subscriber

  • fields (Hash) (defaults to: nil)

    a hash of custom fields and their values for the subscriber

  • groups (Array) (defaults to: nil)

    an array of group IDs to add the subscriber to

  • status (String) (defaults to: nil)

    the status of the new subscriber

  • subscribed_at (DateTime) (defaults to: nil)

    the date and time when the subscriber was added

  • ip_address (String) (defaults to: nil)

    the IP address of the subscriber

  • opted_in_at (DateTime) (defaults to: nil)

    the date and time when the subscriber confirmed their subscription

  • optin_ip (String) (defaults to: nil)

    the IP address of the subscriber when they confirmed their subscription

  • unsubscribed_at (DateTime) (defaults to: nil)

    the date and time when the subscriber was unsubscribed

Returns:

  • (HTTP::Response)

    the response from the API



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mailerlite/subscribers/subscribers.rb', line 43

def create(email:, fields: nil, groups: nil, status: nil, subscribed_at: nil, ip_address: nil, opted_in_at: nil, optin_ip: nil, unsubscribed_at: nil)
  params = { 'email' => email }

  params['fields'] = fields if fields
  params['groups'] = groups if groups
  params['status'] = status if status
  params['subscribed_at'] = subscribed_at if subscribed_at
  params['ip_address'] = ip_address if ip_address
  params['opted_in_at'] = opted_in_at if opted_in_at
  params['optin_ip'] = optin_ip if optin_ip
  params['unsubscribed_at'] = unsubscribed_at if unsubscribed_at

  client.http.post("#{MAILERLITE_API_URL}/subscribers", json: params.compact)
end

#delete(subscriber_id) ⇒ HTTP::Response

Deletes the specified subscriber.

Parameters:

  • subscriber_id (String)

    the ID of the subscriber to delete

Returns:

  • (HTTP::Response)

    the response from the API



114
115
116
# File 'lib/mailerlite/subscribers/subscribers.rb', line 114

def delete(subscriber_id)
  client.http.delete("#{MAILERLITE_API_URL}/subscribers/#{subscriber_id}")
end

#fetch(filter:, limit: nil, cursor: nil) ⇒ HTTP::Response

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

Parameters:

  • filter (:status)
    Array

    the status of the subscribers to include in the results

  • limit (Integer) (defaults to: nil)

    the maximum number of subscribers to return

  • page (Integer)

    the page number of the results to return

Returns:

  • (HTTP::Response)

    the response from the API



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

def fetch(filter:, limit: nil, cursor: nil)
  params = { 'filter[status]' => filter[:status] }

  params['limit'] = limit if limit
  params['cursor'] = cursor if cursor
  uri = URI("#{MAILERLITE_API_URL}/subscribers")
  uri.query = URI.encode_www_form(params.compact)
  client.http.get(uri)
end

#fetch_countHTTP::Response

Returns the total number of subscribers in the MailerLite account.

Returns:

  • (HTTP::Response)

    the response from the API



106
107
108
# File 'lib/mailerlite/subscribers/subscribers.rb', line 106

def fetch_count
  client.http.get("#{MAILERLITE_API_URL}/subscribers/?limit=0")
end

#forget(subscriber_id) ⇒ HTTP::Response

Forgets the specified subscriber.

Parameters:

  • subscriber_id (String)

    the ID of the subscriber to forget

Returns:

  • (HTTP::Response)

    the response from the API



122
123
124
# File 'lib/mailerlite/subscribers/subscribers.rb', line 122

def forget(subscriber_id)
  client.http.post("#{MAILERLITE_API_URL}/subscribers/#{subscriber_id}/forget")
end

#get(subscriber_id) ⇒ HTTP::Response

Returns the details of the specified subscribers

Parameters:

  • subscriber_id (String)

    the ID of the subscriber to get

Returns:

  • (HTTP::Response)

    the response from the API



91
92
93
# File 'lib/mailerlite/subscribers/subscribers.rb', line 91

def get(subscriber_id)
  client.http.get("#{MAILERLITE_API_URL}/subscribers/#{subscriber_id}")
end

#get_single_import(import_id) ⇒ HTTP::Response

Returns the details of the specified subscribers

Parameters:

  • import_id (String)

    the ID of the import to fetch report

Returns:

  • (HTTP::Response)

    the response from the API



99
100
101
# File 'lib/mailerlite/subscribers/subscribers.rb', line 99

def get_single_import(import_id)
  client.http.get("#{MAILERLITE_API_URL}/subscribers/import/#{import_id}")
end

#update(subscriber_id, email: nil, fields: nil, groups: nil, status: nil, subscribed_at: nil, ip_address: nil, opted_in_at: nil, optin_ip: nil, unsubscribed_at: nil) ⇒ HTTP::Response

Updates an existing subscriber with the specified details.

Parameters:

  • subscriber_id (String)

    the ID of the subscriber to update

  • email (String) (defaults to: nil)

    the email address of the new subscriber

  • fields (Hash) (defaults to: nil)

    a hash of custom fields and their values for the subscriber

  • groups (Array) (defaults to: nil)

    an array of group IDs to add the subscriber to

  • status (String) (defaults to: nil)

    the status of the new subscriber

  • subscribed_at (DateTime) (defaults to: nil)

    the date and time when the subscriber was added

  • ip_address (String) (defaults to: nil)

    the IP address of the subscriber

  • opted_in_at (DateTime) (defaults to: nil)

    the date and time when the subscriber confirmed their subscription

  • optin_ip (String) (defaults to: nil)

    the IP address of the subscriber when they confirmed their subscription

  • unsubscribed_at (DateTime) (defaults to: nil)

    the date and time when the subscriber was unsubscribed

Returns:

  • (HTTP::Response)

    the response from the API



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mailerlite/subscribers/subscribers.rb', line 71

def update(subscriber_id, email: nil, fields: nil, groups: nil, status: nil, subscribed_at: nil, ip_address: nil, opted_in_at: nil, optin_ip: nil, unsubscribed_at: nil)
  params = {}

  params['email'] = email if email
  params['fields'] = fields if fields
  params['groups'] = groups if groups
  params['status'] = status if status
  params['subscribed_at'] = subscribed_at if subscribed_at
  params['ip_address'] = ip_address if ip_address
  params['opted_in_at'] = opted_in_at if opted_in_at
  params['optin_ip'] = optin_ip if optin_ip
  params['unsubscribed_at'] = unsubscribed_at if unsubscribed_at

  client.http.put("#{MAILERLITE_API_URL}/subscribers/#{subscriber_id}", json: params.compact)
end