Skip to content

mailerlite/mailerlite-ruby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MailerLite Ruby SDK

MIT licensed

Getting started

For more information about MailerLite API, please visit the following link:

Authentication

API keys are a quick way to implement machine-to-machine authentication without any direct inputs from a human beyond initial setup.

For more information how to obtain an API key visit the following link

Table of Contents

Setup

gem install mailerlite-ruby

You will have to initalize it in your Ruby file with require "mailerlite-ruby".

Usage

This SDK requires that you either have .env file with MAILERLITE_API_TOKEN env variable or that your variable is enabled system wide (useful for Docker/Kubernetes). The example of how MAILERLITE_API_TOKEN should look like is in .env.example.

Subscribers

List all subscribers

require "mailerlite-ruby"

# Intialize the class
subscribers = MailerLite::Subscribers.new

subscribers.fetch(filter: { status: 'active' })

Create a subscriber

require "mailerlite-ruby"

# Intialize the class
subscribers = MailerLite::Subscribers.new

subscribers.create(email:'some@email.com', fields: {'name': 'John', 'last_name': 'Doe'}, ip_address:'1.2.3.4', optin_ip:'1.2.3.4')

Update a subscriber

require "mailerlite-ruby"

# Intialize the class
subscribers = MailerLite::Subscribers.new

subscribers.update(email:'some@email.com', fields: {'name': 'John', 'last_name': 'Doe'}, ip_address:'1.2.3.4', optin_ip:'1.2.3.4')

Get a subscriber

require "mailerlite-ruby"

# Intialize the class
subscribers = MailerLite::Subscribers.new

subscribers.get('some@email.com')

Delete a subscriber

require "mailerlite-ruby"

# Intialize the class
subscribers = MailerLite::Subscribers.new

subscriber_id = 1234567890

subscribers.delete(subscriber_id)

Groups

List all groups

require "mailerlite-ruby"

# Intialize the class
groups = MailerLite::Groups.new

groups.list(limit:10, page:1, filter:{'name': 'My'}, sort:'name')

Create a group

require "mailerlite-ruby"

# Intialize the class
groups = MailerLite::Groups.new

groups.create(name:'Group Name')

Update a group

require "mailerlite-ruby"

# Intialize the class
groups = MailerLite::Groups.new

groups.update(group_id:1234567, name:'My New Group')

Delete a group

require "mailerlite-ruby"

# Intialize the class
groups = MailerLite::Groups.new

group_id = 1234567

groups.delete(group_id)

Get subscribers belonging to a group

require "mailerlite-ruby"

# Intialize the class
groups = MailerLite::Groups.new

groups.get_subscribers(group_id:1234567, page:1, limit:10, filter:{'status': 'active'})

Assign subscriber to a group

require "mailerlite-ruby"

# Intialize the class
groups = MailerLite::Groups.new

groups.assign_subscriber(subscriber:111222, group_id:1234567)

Unassign subscriber from a group

require "mailerlite-ruby"

# Intialize the class
groups = MailerLite::Groups.new

groups.unassign_subscriber(subscriber:111222, group_id:1234567)

Segments

List all segments

require "mailerlite-ruby"

# Intialize the class
segments = MailerLite::Segments.new

segments.list(limit:10, page:1)

Update a segment

require "mailerlite-ruby"

# Intialize the class
segments = MailerLite::Segments.new

segments.update(segment_id: 123456, name:'My New Segment Name')

Delete a segment

require "mailerlite-ruby"

# Intialize the class
segments = MailerLite::Segments.new
segment_id = 123456

segments.delete(segment_id)

Get subscribers belonging to a segment

require "mailerlite-ruby"

# Intialize the class
segments = MailerLite::Segments.new

segments.get_subscribers(segment_id:123456, limit:10, filter:{'status': 'active'})

Fields

List all fields

require "mailerlite-ruby"

# Intialize the class
fields = MailerLite::Fields.new

fields.get(limit:10, page:1, sort:'name', filter:{'keyword': 'abc', 'type': 'text'})

Create a field

require "mailerlite-ruby"

# Intialize the class
fields = MailerLite::Fields.new

fields.create(name:'My Field', type:'text')

Update a field

require "mailerlite-ruby"

# Intialize the class
fields = MailerLite::Fields.new

fields.update(field_id:123345, name:'My New Field')

Delete a field

require "mailerlite-ruby"

# Intialize the class
fields = MailerLite::Fields.new

field_id = 123456

fields.delete(field_id)

Automations

List all automations

require "mailerlite-ruby"

# Intialize the class
automations = MailerLite::Automations.new

automations.get(limit:10, page:1, filter:{'status': true, 'name': 'some name', 'group': 123456})

Get an automation

require "mailerlite-ruby"

# Intialize the class
automations = MailerLite::Automations.new

automation_id = 123456

automations.fetch(automation_id)

Get subscribers activity for an automation

require "mailerlite-ruby"

# Intialize the class
automations = MailerLite::Automations.new

automations.get_subscriber_activity(automation_id:123456, page:1, limit:10, filter:{'status': 'active', 'date_from': '2022-12-20', 'date_to': '2022-12-31'})

Campaigns

List all campaigns

require "mailerlite-ruby"

# Intialize the class
campaigns = MailerLite::Campaigns.new

campaigns.get(limit:10, page:1, filter:{'status': 'ready', 'type': 'regular'})

Get a campaign

require "mailerlite-ruby"

# Intialize the class
campaigns = MailerLite::Campaigns.new

campaigns.fetch(campaign_id:123456)

Create a campaign

require "mailerlite-ruby"

# Intialize the class
campaigns = MailerLite::Campaigns.new

campaigns.create(
    name: "Test Campaign",
    language_id: 1,
    type: "regular",
    emails: [{
        "subject": "This is a test campaign",
        "from_name": "Test Man",
        "from": "testuser@mailerlite.com",
        "content": "Hi there, this is a test campaign!"
    }]
  )

Update a campaign

require "mailerlite-ruby"

# Intialize the class
campaigns = MailerLite::Campaigns.new

campaigns.update(
  campaign_id: 1233455, 
  name: "New Campaign Name",
  language_id: 2,
  emails: [{
      "subject": "This is a test campaign",
      "from_name": "Test Man",
      "from": "testuser@mailerlite.com",
      "content": "Hi there, this is a test campaign!"
  }]
  )

Schedule a campaign

require "mailerlite-ruby"

# Intialize the class
campaigns = MailerLite::Campaigns.new

campaigns.schedule(
  campaign_id: 123456,
  delivery: "scheduled",
  schedule: {
      "date": "2022-12-31",
      "hours": "22",
      "minutes": "00"
  }
)

Cancel a campaign

require "mailerlite-ruby"

# Intialize the class
campaigns = MailerLite::Campaigns.new

campaign_id = 123456

campaigns.cancel(campaign_id)

Delete a campaign

require "mailerlite-ruby"

# Intialize the class
campaigns = MailerLite::Campaigns.new

campaign_id = 123456

campaigns.delete(campaign_id)

Get subscribers activity for a campaign

require "mailerlite-ruby"

# Intialize the class
campaigns = MailerLite::Campaigns.new

campaign_id = 123456

campaigns.activity(campaign_id)

Forms

List all forms

require "mailerlite-ruby"

# Intialize the class
forms = MailerLite::Forms.new

forms.list(limit:10, page:1, sort:'name', filter:{'name': 'form name'})

Get a form

require "mailerlite-ruby"

# Intialize the class
forms = MailerLite::Forms.new

form_id = 123456

forms.fetch(form_id)

Update a form

require "mailerlite-ruby"

# Intialize the class
forms = MailerLite::Forms.new

forms.update(form_id:123456, name: 'My form Name')

Delete a form

require "mailerlite-ruby"

# Intialize the class
forms = MailerLite::Forms.new

form_id = 123456

forms.delete(form_id)

Get subscribers who signed up to a specific form

require "mailerlite-ruby"

# Intialize the class
forms = MailerLite::Forms.new

forms.fetch_subscribers(form_id:123345, page:1, limit:10, filter:{'status': 'active'})

Batching

Create a new batch

require "mailerlite-ruby"

# Intialize the class
batch = MailerLite::Batch.new

batch.request(
          requests: [
            { method: 'GET', path: 'api/subscribers/list' },
            { method: 'GET', path: 'api/campaigns/list' }
          ]
        )

Webhooks

List all webhooks

require "mailerlite-ruby"

# Intialize the class
subscribers = MailerLite::Subscribers.new

webhooks.list()

Get a webhook

require "mailerlite-ruby"

# Intialize the class
subscribers = MailerLite::Subscribers.new

webhook_id = 123456

webhooks.get(webhook_id)

Create a webhook

require "mailerlite-ruby"

# Intialize the class
webhooks = MailerLite::Webhooks.new

webhooks.create(
  events:[
    'subscriber.created',
    'subscriber.updated',
    'subscriber.unsubscribed'
  ], 
  url:'https://my-url.com',
  name: 'Webhook name'
)

Update a webhook

require "mailerlite-ruby"

# Intialize the class
webhooks = MailerLite::Webhooks.new

webhooks.update(
  webhook_id: 123456, 
  events:[
    'subscriber.created',
    'subscriber.updated',
    'subscriber.unsubscribed'
  ], 
  url:'https://my-url.com',
  name: 'Webhook name',
  enabled: false
)

Delete a webhook

require "mailerlite-ruby"

# Intialize the class
webhooks = MailerLite::Webhooks.new

webhook_id = 123456

webhooks.delete(webhook_id)

Timezones

Get a list of timezones

require "mailerlite-ruby"

# Intialize the class
timezones = MailerLite::Timezones.new

timezones.list()

Campaign languages

Get a list of languages

require "mailerlite-ruby"

# Intialize the class
campaigns = MailerLite::Campaigns.new

campaigns.languages()

Testing

bundle i 
bundle exec rspec spec/*rspec.rb

To run tests you would need to install gems using bundle and then run rspec via bundle to run all tests. The fixtures for the test have been recorded using vcr and are available in the ./fixtures directory

Generate Docs

bundle i 
bundle exec yardoc 'lib/**/*.rb'

This will generate html docs in the doc directory which can be opened up in any browser. Navigate to index.html and open it up.