Class: MailerLite::Fields

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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

def client
  @client
end

Instance Method Details

#create(type:, name:) ⇒ HTTP::Response

Update the specified Field

Parameters:

  • name (String)

    the name of the field to create

  • type (String)

    the type, can be text, number or date

Returns:

  • (HTTP::Response)

    the response from the API



38
39
40
41
# File 'lib/mailerlite/fields/fields.rb', line 38

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

#delete(field_id) ⇒ HTTP::Response

Deletes the specified Field.

Parameters:

  • field_id (String)

    the ID of the Field to delete

Returns:

  • (HTTP::Response)

    the response from the API



57
58
59
# File 'lib/mailerlite/fields/fields.rb', line 57

def delete(field_id)
  client.http.delete("#{MAILERLITE_API_URL}/fields/#{field_id}")
end

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

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

Parameters:

  • filter (:keyword, :type) (defaults to: {})

    Returns partial matches for fields

  • limit (Integer) (defaults to: nil)

    the maximum number of Fields 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
31
# File 'lib/mailerlite/fields/fields.rb', line 21

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

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

Update the specified Field

Parameters:

  • field_id (Integer)

    the field_id to update

  • name (String)

    the name to update

Returns:

  • (HTTP::Response)

    the response from the API



48
49
50
51
# File 'lib/mailerlite/fields/fields.rb', line 48

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