Class: MailerLite::Groups

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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

def client
  @client
end

Instance Method Details

#assign_subscriber(group_id:, subscriber:) ⇒ HTTP::Response

Assign Subscriber to the specified group.

Parameters:

  • group_id (Integer)

    The id of existing group belonging to the account

  • subscriber (Integer)

    The id of existing subscriber belonging to the account

Returns:

  • (HTTP::Response)

    the response from the API



70
71
72
# File 'lib/mailerlite/groups/groups.rb', line 70

def assign_subscriber(group_id:, subscriber:)
  client.http.post("#{MAILERLITE_API_URL}/subscribers/#{subscriber}/groups/#{group_id}")
end

#create(name:) ⇒ HTTP::Response

create a Group

Parameters:

  • name (String)

    the name to update

Returns:

  • (HTTP::Response)

    the response from the API



36
37
38
39
# File 'lib/mailerlite/groups/groups.rb', line 36

def create(name:)
  params = { 'name' => name }
  client.http.post("#{MAILERLITE_API_URL}/groups", json: params.compact)
end

#delete(group_id) ⇒ HTTP::Response

Deletes the specified Groups.

Parameters:

  • group_id (String)

    the ID of the Groups to delete

Returns:

  • (HTTP::Response)

    the response from the API



86
87
88
# File 'lib/mailerlite/groups/groups.rb', line 86

def delete(group_id)
  client.http.delete("#{MAILERLITE_API_URL}/groups/#{group_id}")
end

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

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

Parameters:

  • filter (:name) (defaults to: {})

    the name of the Groups to include in the results

  • limit (Integer) (defaults to: nil)

    the maximum number of Groups to return

  • page (Integer) (defaults to: nil)

    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
30
# File 'lib/mailerlite/groups/groups.rb', line 21

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

#get_subscribers(group_id:, filter: {}, limit: nil, page: nil, sort: nil) ⇒ HTTP::Response

Get Subscribers assigned to the specified group.

Parameters:

  • group_id (Integer)

    The id of existing group belonging to the account

  • filter (:status) (defaults to: {})
    String

    Must be one of the possible statuses: active, unsubscribed, unconfirmed, bounced or junk. Defaults to active.

  • limit (Integer) (defaults to: nil)

    the maximum number of subscribers to return

  • page (Integer) (defaults to: nil)

    the page number of the results to return

Returns:

  • (HTTP::Response)

    the response from the API



57
58
59
60
61
62
63
64
# File 'lib/mailerlite/groups/groups.rb', line 57

def get_subscribers(group_id:, filter: {}, limit: nil, page: nil, sort: nil)
  params = {}
  params['filter[status]'] = filter[:status] if filter.key?(:status)
  params['limit'] = limit if limit
  params['sort'] = sort if sort
  params['page'] = page if page
  client.http.get("#{MAILERLITE_API_URL}/groups/#{group_id}/subscribers", json: params.compact)
end

#unassign_subscriber(group_id:, subscriber:) ⇒ HTTP::Response

Unassign Subscriber to the specified group.

Parameters:

  • group_id (Integer)

    The id of existing group belonging to the account

  • subscriber (Integer)

    The id of existing subscriber belonging to the account

Returns:

  • (HTTP::Response)

    the response from the API



78
79
80
# File 'lib/mailerlite/groups/groups.rb', line 78

def unassign_subscriber(group_id:, subscriber:)
  client.http.delete("#{MAILERLITE_API_URL}/subscribers/#{subscriber}/groups/#{group_id}")
end

#update(group_id:, name:) ⇒ HTTP::Response

Update the specified Group

Parameters:

  • group_id (String)

    the ID of the Groups to update

  • name (String)

    the name to update

Returns:

  • (HTTP::Response)

    the response from the API



46
47
48
49
# File 'lib/mailerlite/groups/groups.rb', line 46

def update(group_id:, name:)
  params = { 'name' => name }
  client.http.put("#{MAILERLITE_API_URL}/groups/#{group_id}", json: params.compact)
end