Class: MailerLite::Forms

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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

def client
  @client
end

Instance Method Details

#delete(form_id) ⇒ HTTP::Response

Deletes the specified forms.

Parameters:

  • form_id (String)

    the ID of the forms to delete

Returns:

  • (HTTP::Response)

    the response from the API



69
70
71
# File 'lib/mailerlite/forms/forms.rb', line 69

def delete(form_id)
  client.http.delete("#{MAILERLITE_API_URL}/forms/#{form_id}")
end

#fetch(form_id) ⇒ HTTP::Response

Returns the details of the specified Forms

Parameters:

  • form_id (String)

    the ID of the forms to fetch

Returns:

  • (HTTP::Response)

    the response from the API



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

def fetch(form_id)
  client.http.get("#{MAILERLITE_API_URL}/forms/#{form_id}")
end

#fetch_countHTTP::Response

Returns the total number of Forms in the MailerLite account.

Returns:

  • (HTTP::Response)

    the response from the API



61
62
63
# File 'lib/mailerlite/forms/forms.rb', line 61

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

#fetch_subscribers(form_id) ⇒ HTTP::Response

Returns the subscribers who signed up to a specific form

Parameters:

  • form_id (String)

    the ID of the forms to fetch

Returns:

  • (HTTP::Response)

    the response from the API



44
45
46
# File 'lib/mailerlite/forms/forms.rb', line 44

def fetch_subscribers(form_id)
  client.http.get("#{MAILERLITE_API_URL}/forms/#{form_id}/subscribers")
end

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

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

Parameters:

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

    the name of the Forms to include in the results

  • limit (Integer) (defaults to: nil)

    the maximum number of Forms 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/forms/forms.rb', line 21

def list(type:, 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}/forms/#{type}")
  uri.query = URI.encode_www_form(params.compact)
  client.http.get(uri)
end

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

Update the specified Forms

Parameters:

  • form_id (String)

    the ID of the forms to fetch

  • name (String)

    the name to update

Returns:

  • (HTTP::Response)

    the response from the API



53
54
55
56
# File 'lib/mailerlite/forms/forms.rb', line 53

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