Class: MailerLite::Segments

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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

def client
  @client
end

Instance Method Details

#delete(segment_id) ⇒ HTTP::Response

Deletes the specified Segments.

Parameters:

  • segment_id (String)

    the ID of the Segments to delete

Returns:

  • (HTTP::Response)

    the response from the API



58
59
60
# File 'lib/mailerlite/segments/segments.rb', line 58

def delete(segment_id)
  client.http.delete("#{MAILERLITE_API_URL}/segments/#{segment_id}")
end

#get_subscribers(segment_id:, filter: {}, limit: nil, after: nil) ⇒ HTTP::Response

Get Subscribers assigned to the specified Segment.

Parameters:

  • segment_id (Integer)

    The id of existing Segment 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

  • after (Integer) (defaults to: nil)

    The last subscriber id, available in meta.last

Returns:

  • (HTTP::Response)

    the response from the API



44
45
46
47
48
49
50
51
52
# File 'lib/mailerlite/segments/segments.rb', line 44

def get_subscribers(segment_id:, filter: {}, limit: nil, after: nil)
  params = {}
  params['filter[status]'] = filter[:status] if filter.key?(:status)
  params['limit'] = limit if limit
  params['after'] = after if after
  uri = URI("#{MAILERLITE_API_URL}/segments/#{segment_id}/subscribers")
  uri.query = URI.encode_www_form(params.compact)
  client.http.get(uri)
end

#list(limit: nil, page: nil) ⇒ HTTP::Response

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

Parameters:

  • limit (Integer) (defaults to: nil)

    the maximum number of Segments to return

  • page (Integer) (defaults to: nil)

    the page number of the results to return

Returns:

  • (HTTP::Response)

    the response from the API



20
21
22
23
24
25
26
# File 'lib/mailerlite/segments/segments.rb', line 20

def list(limit: nil, page: nil)
  params = {}
  params['limit'] = limit if limit
  params['page'] = page if page

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

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

Update the specified Segment

Parameters:

  • segment_id (String)

    the ID of the Segments to update

  • name (String)

    the name to update

Returns:

  • (HTTP::Response)

    the response from the API



33
34
35
36
# File 'lib/mailerlite/segments/segments.rb', line 33

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