Class: MailerLite::Subscribers
- Inherits:
-
Object
- Object
- MailerLite::Subscribers
- Defined in:
- lib/mailerlite/subscribers/subscribers.rb
Overview
This is a class for manipulating the subscribers from MailerLite API.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#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.
-
#delete(subscriber_id) ⇒ HTTP::Response
Deletes the specified subscriber.
-
#fetch(filter:, limit: nil, cursor: nil) ⇒ HTTP::Response
Returns a list of subscribers that match the specified filter criteria.
-
#fetch_count ⇒ HTTP::Response
Returns the total number of subscribers in the MailerLite account.
-
#forget(subscriber_id) ⇒ HTTP::Response
Forgets the specified subscriber.
-
#get(subscriber_id) ⇒ HTTP::Response
Returns the details of the specified subscribers.
-
#get_single_import(import_id) ⇒ HTTP::Response
Returns the details of the specified subscribers.
-
#initialize(client: MailerLite::Client.new) ⇒ Subscribers
constructor
Inits the ‘Subscribers` class with the specified `client`.
-
#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.
Constructor Details
#initialize(client: MailerLite::Client.new) ⇒ Subscribers
Inits the ‘Subscribers` class with the specified `client`.
11 12 13 |
# File 'lib/mailerlite/subscribers/subscribers.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/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.
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.
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.
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_count ⇒ HTTP::Response
Returns the total number of subscribers in the MailerLite account.
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.
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
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
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.
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 |